HDU 2435 - There is a war

来源:互联网 发布:襄阳网站搜索引擎优化 编辑:程序博客网 时间:2024/05/17 08:23

Description

      There is a sea. 
      There are N islands in the sea. 
      There are some directional bridges connecting these islands. 
      There is a country called Country One located in Island 1. 
      There is another country called Country Another located in Island N. 
      There is a war against Country Another, which launched by Country One. 
      There is a strategy which can help Country Another to defend this war by destroying the bridges for the purpose of making Island 1 and Island n disconnected. 
      There are some different destroying costs of the bridges. 
      There is a prophet in Country Another who is clever enough to find the minimum total destroying costs to achieve the strategy. 
      There is an architecture in Country One who is capable enough to rebuild a bridge to make it unbeatable or build a new invincible directional bridge between any two countries from the subset of island 2 to island n-1. 
      There is not enough time for Country One, so it can only build one new bridge, or rebuild one existing bridge before the Country Another starts destroying, or do nothing if happy. 
      There is a problem: Country One wants to maximize the minimum total destroying costs Country Another needed to achieve the strategy by making the best choice. Then what’s the maximum possible result? 
 

Input

      There are multiple cases in this problem. 
      There is a line with an integer telling you the number of cases at the beginning. 
      The are two numbers in the first line of every case, N(4<=N<=100) and M(0<=M<=n*(n-1)/2), indicating the number of islands and the number of bridges. 
      There are M lines following, each one of which contains three integers a, b and c, with 1<=a, b<=N and 1<=c<=10000, meaning that there is a directional bridge from a to b with c being the destroying cost. 
      There are no two lines containing the same a and b. 
 

Output

      There is one line with one integer for each test case, telling the maximun possible result. 
 

Sample Input

44 04 21 2 23 4 24 31 2 12 3 13 4 104 31 2 52 3 23 4 3
 

Sample Output

0213
 

Solution

①首先,我们知道如果A没有修桥,则B需要破坏一些桥,得到原网络的一个割,要使割最小,
即可把每个桥的cost看成容量,求出最大流;
②现在问题是修桥以后的,那么,假设现在已经完成了第一步,得到原网络的两个分量P,Q,要修桥一定不会选择同在
一个分量中的两个点,应为这不会影响最小割;
③现在已经知道了,桥只能修在P,Q中间,修好桥后,这个桥的容量应该是无穷大(不可摧毁),要考虑的就是桥要
修在哪两个点之间;
④剩下的已经很好考虑了。。。
 

#include<cstdio>#include<vector>#include<queue>#include<cstring>#include<algorithm>using namespace std;const int N = 100 + 1;const int M = N * N;const int INF = 1 << 30;struct E{int v, c, f;E(int v, int c){this->v = v;this->c = c;f = 0;}};vector<int> G[N];int C[N][N], F[N][N];int able[N], p[N];queue<int> q;int folkson(const int s, const int t){int res = 0;while(true){memset(able, 0, sizeof(able));able[s] = INF;q.push(s);p[s] = s;while(!q.empty()){int u = q.front();q.pop();for(int i = 0; i < G[u].size(); i++){int v = G[u][i];if(able[v] == 0 && F[u][v] < C[u][v]){able[v] = min(able[u], C[u][v] - F[u][v]);q.push(v);p[v] = u;}}}if(able[t] == 0)break;for(int u = t; u != s; u = p[u]){F[p[u]][u] += able[t];F[u][p[u]] -= able[t];}res += able[t];}return res;}inline void trans(const int s, const int t){for(int i = s; i <= t; i++){for(int j = i; j <= t; j++){swap(F[i][j], F[j][i]);swap(C[i][j], C[j][i]);}}}int solve(const int s, const int t){int res = folkson(s, t);int maxa = 0, maxb = 0;folkson(s, t);for(int i = 2; i < t; i++)maxa = max(maxa, able[i]);trans(s, t);folkson(t, s);for(int i = 2; i < t; i++)maxb = max(maxb, able[i]);res += min(maxa, maxb);return res;}inline void initialize(){memset(F, 0, sizeof(F));memset(C, 0, sizeof(C));for(int i = 1; i < N; i++)G[i].clear();}int main(){int T;int n, m;scanf("%d", &T);while( T-- ){initialize();scanf("%d%d", &n, &m);for(int i = 0; i < m; i++){int u, v, c;scanf("%d%d%d", &u, &v, &c);C[u][v] = c;G[u].push_back(v);G[v].push_back(u);}printf("%d\n", solve(1, n));}return 0;}

0 0