最大網絡流PKU1273


題目:大意是 john 想抱住他最愛的三葉草不被水淹沒,他要用渠道對水進行引流(引到河流中去),水源爲 1 ,河流匯點爲 n 爲 ,問渠道最多能引流多少水

本題的注意點:


1:存在重邊

如:1->2 = 3

        1->2 = 4;

       1->2 = 5;

那麼  map[1][2] =3+4+5;

開始提交錯誤了好多次,主要是對於存在重邊 沒多加思索,定性思維以爲是取最大值,其實不是。應該將其值求和。(…………………………………………)

代碼如下:

#include<iostream>
#define Max 201
#define inf 1<<30

using namespace std;

int map[Max][Max];
int que[Max*10];

void max_flow(int map[][Max], int s,int t,int n){
	int visit[Max];
	int sum=0;
	int flow[Max][Max];
	int i,pre[Max];
	int min = inf;
	bool flage = true;
	memset(pre,-1,sizeof(pre));
	memset(visit,0,sizeof(visit));
	int p=0,q=1;
	que[p] = s;
	while(true){
    memset(visit,0,sizeof(visit));
		flage = true;
		p=0;q=1;
		while(p !=q){// 廣度優先搜素
			int temp = 0;
			int st = que[p];	p++;
			visit[st] = 1;
			for(i=1;i<=n;i++){
				if(map[st][i]>0 && visit[i]==0){
					visit[i] = 1;
					if(i==t){
						flage = false;
						min = inf;
						pre[i]=st;
						for(int u=t; u!=s;u=pre[u]){
							if(map[pre[u]][u] < min){
								min = map[pre[u]][u];
							}
						}
						sum+=min;
						temp=1;
						while(i!=s){
						    //sum +=min;
							map[pre[i]][i] -=min;
							map[i][pre[i]] = min;
							flow[pre[i]][i] =min;
							i=pre[i];
						}
						break;
					}
					else{
					pre[i]=st;
					que[q]=i;
					q++;
					}
				}
				if(temp==1){
					break;
				}
			}
		}
		if(flage){
			break;
		}
	}
	cout<<sum<<endl;
}

int main(void){
	int m,n;
	while(cin>>m>>n){
		memset(map,0,sizeof(map));
	for(int i=0; i<m; i++){
		int x,y,z;
		cin>>x>>y>>z;
		map[x][y]+=z;// 重邊
		if( x==y){
			map[x][y]=0;
		}
	}
	max_flow(map,1,n,n);
	}
	return 0;
}

測試數據(大牛提供的)

199 50
1 46 250104
1 24 899262
1 15 80055
1 37 50603
1 49 37800
37 38 320870
37 6 86811
15 2 22244
1 45 181373
49 7 3544
15 12 403604
46 8 690720
37 28 263412
1 40 71370
12 48 28404
8 21 7326
15 43 332310
12 11 690228
21 27 75225
46 3 442950
2 25 297960
15 4 267467
45 5 13432
12 35 141492
27 23 215600
3 34 369600
37 9 4905
12 14 300744
14 19 364040
27 20 382396
49 10 641823
2 41 81153
9 32 167510
5 44 271219
4 16 11184
23 29 275502
11 42 432078
14 18 16965
48 33 273456
23 39 139384
40 31 97567
4 26 188246
16 47 138980
8 22 172577
33 36 111230
7 30 21054
3 17 41181
5 13 528858
29 50 240760
43 47 277514
36 41 842506
13 33 186456
34 9 21403
43 45 459825
25 37 336474
24 17 141200
11 50 513535
34 17 504672
36 7 4802
33 36 867825
23 46 438080
33 38 472428
27 37 137200
4 14 12768
40 38 559647
2 48 738568
7 14 164388
39 29 200506
35 1 197524
33 30 1466
32 43 47244
26 34 146772
22 14 375396
4 23 362168
49 17 155016
27 22 231660
5 25 367584
11 44 781568
39 36 356481
22 50 597300
14 27 8168
19 5 5796
12 24 107141
10 2 3480
8 20 13870
5 31 537591
46 50 682864
41 12 36900
14 31 385176
46 32 438240
38 44 278784
23 33 24855
20 37 124640
23 17 56100
43 19 22176
4 49 434241
21 28 137952
5 18 161568
18 37 176190
22 4 309675
10 31 619548
46 5 206796
17 30 195552
48 14 479076
25 25 100744
33 6 308913
34 16 70252
32 34 60096
40 9 58312
24 25 199800
48 20 940975
39 35 604435
13 27 437852
42 13 82490
16 42 60030
1 16 695520
22 29 544479
25 34 347529
15 28 559302
5 22 77670
17 44 334730
12 34 172676
38 33 640533
42 18 245232
9 49 134067
39 13 59784
39 45 452956
47 5 156144
15 35 62434
40 40 66185
22 4 72220
44 38 9345
2 30 52908
29 23 775795
38 14 561150
38 46 221040
29 12 122920
1 28 754444
31 23 297570
14 50 729150
45 35 133163
23 49 618300
26 29 312698
21 31 127428
28 39 259644
32 24 84576
27 10 3081
27 1 125928
30 27 465660
23 4 198066
11 35 536400
7 22 763152
25 29 31218
46 27 197547
16 37 176700
33 20 46629
43 22 486783
1 28 797534
44 30 483055
28 9 144676
28 49 172692
26 15 273916
48 27 72267
49 8 107793
16 37 648375
47 35 450730
28 11 97118
38 8 128280
41 17 16264
21 42 7362
22 3 38222
13 45 25545
24 41 87945
25 18 641704
5 30 865890
11 13 91368
15 2 294
5 39 502860
27 42 207162
34 37 649272
40 3 797760
17 46 673816
14 37 171133
40 41 397432
43 26 209860
20 13 377578
3 22 129602
40 16 113982
30 46 324170
24 14 226278
40 37 588795
38 38 50015
15 10 1438
28 46 585600
29 22 144485
30 41 410167
9 43 522180
20 44 328968
16 34 88950

2763609




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章