HDU 3691 Nubulsa Expo(全局最小割Stoer-Wagner算法)

Problem Description
You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa is an undeveloped country and it is threatened by the rising of sea level. Scientists predict that Nubulsa will disappear by the year of 2012. Nubulsa government wants to host the 2011 Expo in their country so that even in the future, all the people in the world will remember that there was a country named “Nubulsa”.

As you know, the Expo garden is made up of many museums of different countries. In the Expo garden, there are a lot of bi-directional roads connecting those museums, and all museums are directly or indirectly connected with others. Each road has a tourist capacity which means the maximum number of people who can pass the road per second. 

Because Nubulsa is not a rich country and the ticket checking machine is very expensive, the government decides that there must be only one entrance and one exit. The president has already chosen a museum as the entrance of the whole Expo garden, and it’s the Expo chief directory Wuzula’s job to choose a museum as the exit.

Wuzula has been to the Shanghai Expo, and he was frightened by the tremendous “people mountain people sea” there. He wants to control the number of people in his Expo garden. So Wuzula wants to find a suitable museum as the exit so that the “max tourists flow” of the Expo garden is the minimum. If the “max tourist flow” is W, it means that when the Expo garden comes to “stable status”, the number of tourists who enter the entrance per second is at most W. When the Expo garden is in “stable status”, it means that the number of people in the Expo garden remains unchanged.

Because there are only some posters in every museum, so Wuzula assume that all tourists just keep walking and even when they come to a museum, they just walk through, never stay.
 
Input
There are several test cases, and the input ends with a line of “0 0 0”.
For each test case:
The first line contains three integers N, M and S, representing the number of the museums, the number of roads and the No. of the museum which is chosen as the entrance (all museums are numbered from 1 to N). For example, 5 5 1 means that there are 5 museums and 5 roads connecting them, and the No. 1 museum is the entrance.
The next M lines describe the roads. Each line contains three integers X, Y and K, representing the road connects museum X with museum Y directly and its tourist capacity is K.
Please note:
1<N<=300, 0<M<=50000, 0<S,X,Y<=N, 0<K<=1000000
 
Output
For each test case, print a line with only an integer W, representing the “max tourist flow” of the Expo garden if Wuzula makes the right choice.
 
题目大意:给一个无向连通图,给定一个起点,你可以任意确定一个终点,使得这两个点之间的最大流最大,求这个最大流。
思路:由最大流最小割定理,而又因为不管最小割长什么样,起点必然在一个集合,只要随意在另一个集合找一个终点,即可得到所求最大流。题目转化为求全局的最小割,采用Stoer-Wagner算法,复杂度为(O(n^3))。
 
代码(1765MS):
 1 #include <cstdio>

 2 #include <algorithm>

 3 #include <cstring>

 4 #include <iostream>

 5 using namespace std;

 6 typedef long long LL;

 7 

 8 const int MAXN = 310;

 9 

10 LL mat[MAXN][MAXN];

11 LL weight[MAXN];

12 bool del[MAXN], vis[MAXN];;

13 int n, m, st;

14 

15 void init() {

16     memset(mat, 0, sizeof(mat));

17     memset(del, 0, sizeof(del));

18 }

19 

20 LL StoerWagner(int &s, int &t, int cnt) {

21     memset(weight, 0, sizeof(weight));

22     memset(vis, 0, sizeof(vis));

23     for(int i = 1; i <= n; ++i)

24         if(!del[i]) {t = i; break; }

25     while(--cnt) {

26         vis[s = t] = true;

27         for(int i = 1; i <= n; ++i) if(!del[i] && !vis[i]) {

28             weight[i] += mat[s][i];

29         }

30         t = 0;

31         for(int i = 1; i <= n; ++i) if(!del[i] && !vis[i]) {

32             if(weight[i] >= weight[t]) t = i;

33         }

34     }

35     return weight[t];

36 }

37 

38 void merge(int s, int t) {

39     for(int i = 1; i <= n; ++i) {

40         mat[s][i] += mat[t][i];

41         mat[i][s] += mat[i][t];

42     }

43     del[t] = true;

44 }

45 

46 LL solve() {

47     LL ret = -1;

48     int s, t;

49     for(int i = n; i > 1; --i) {

50         if(ret == -1) ret = StoerWagner(s, t, i);

51         else ret = min(ret, StoerWagner(s, t, i));

52         merge(s, t);

53     }

54     return ret;

55 }

56 

57 int main() {

58     while(scanf("%d%d%d", &n, &m, &st) != EOF) {

59         if(n == 0 && m == 0 && st == 0) break;

60         init();

61         while(m--) {

62             int x, y, z;

63             scanf("%d%d%d", &x, &y, &z);

64             mat[x][y] += z;

65             mat[y][x] += z;

66         }

67         cout<<solve()<<endl;

68     }

69 }
View Code

 

你可能感兴趣的:(exp)