标签:style blog http color io os ar java for
Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs?
Input contains multiple test cases. Each test case starts with two integers N and M (2 ≤ N ≤ 500, 0 ≤ M ≤ N × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following areM lines, each line contains M integers A, B and C (0 ≤ A, B < N, A ≠ B, C > 0), meaning that there C edges connecting vertices A and B.
There is only one line for each test case, which contains the size of the minimum cut of the graph. If the graph is disconnected, print 0.
3 3 0 1 1 1 2 1 2 0 1 4 3 0 1 1 1 2 1 2 3 1 8 14 0 1 1 0 2 1 0 3 1 1 2 1 1 3 1 2 3 1 4 5 1 4 6 1 4 7 1 5 6 1 5 7 1 6 7 1 4 0 1 7 3 1
2 1 2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 510; 18 int e[maxn][maxn],n,m; 19 bool comb[maxn]; 20 int Find(int &s,int &t){ 21 bool vis[maxn]; 22 int w[maxn]; 23 memset(vis,false,sizeof(vis)); 24 memset(w,0,sizeof(w)); 25 int tmp = INF; 26 for(int i = 0; i < n; ++i){ 27 int theMax = -INF; 28 for(int j = 0; j < n; j++) 29 if(!vis[j] && !comb[j] && w[j] > theMax) 30 theMax = w[tmp = j]; 31 if(t == tmp) break; 32 s = t; 33 vis[t = tmp] = true; 34 for(int j = 0; j < n; j++) 35 if(!vis[j] && !comb[j]) 36 w[j] += e[t][j]; 37 } 38 return w[t]; 39 } 40 int solve(){ 41 int ans = INF,s,t; 42 memset(comb,0,sizeof(comb)); 43 for(int i = 1; i < n; i++){ 44 s = t = -1; 45 ans = min(ans,Find(s,t)); 46 for(int j = 0; j < n; j++){ 47 e[s][j] += e[t][j]; 48 e[j][s] += e[j][t]; 49 } 50 comb[t] = true; 51 } 52 return ans; 53 } 54 int main() { 55 int u,v,w; 56 while(~scanf("%d %d",&n,&m)){ 57 memset(e,0,sizeof(e)); 58 while(m--){ 59 scanf("%d %d %d",&u,&v,&w); 60 e[u][v] += w; 61 e[v][u] += w; 62 } 63 printf("%d\n",solve()); 64 } 65 return 0; 66 }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/crackpotisback/p/4003897.html