King of Destruction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 975 Accepted Submission(s): 381
Problem Description
Zhou xingxing is the successor of one style of kung fu called "Karate Kid".he is falling love with a beautiful judo student,after being humiliated by her boyfriend,a Taekwando master from Japan,Zhou is going to fight with his rival in love.The way they fight is to destroy the wooden plank between some wooden pegs,in order to cut these wooden pegs into two disconnected parts,and destroy each piece of plank need consume different energy.However Zhou xingxing is beginner after all,so he is turn to you for help,please calculate the minimum energy he need to destroy the wooden plank.
Input
The input consists of multiple test cases.
Each test case starts with two integers n (0 < n <= 100) and m in one line, where n、m are the number of wooden pegs and wooden plank.
Following are m lines, each line contains three integers s, e and q (0 <= s, e < n,q > 0), meaning that there need q energy to destroy the wooden plank between s and e.
Each test case starts with two integers n (0 < n <= 100) and m in one line, where n、m are the number of wooden pegs and wooden plank.
Following are m lines, each line contains three integers s, e and q (0 <= s, e < n,q > 0), meaning that there need q energy to destroy the wooden plank between s and e.
Output
There is only one line for each test case, which contains the minimum energy they need to complete this fight.
Sample Input
2 1 0 1 50 3 2 0 1 50 1 2 10
Sample Output
50 10题意:求无向图的最小割。思路:无向图的最小割又叫全局最小割,枚举汇点求最大流效率很低,因而普遍使用StoerWagner算法,时间复杂度为O(n^3)。AC代码:#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll __int64 using namespace std; const int INF = 1000000000; const int maxn = 105; int G[maxn][maxn]; int n, m; int StoerWagner(){ int cnt = n, s, t; int cut = INF; bool vis[maxn], use[maxn]; int dis[maxn]; memset(use, false, sizeof(use)); while(--cnt > 0) { memset(vis, false, sizeof(vis)); memset(dis, 0, sizeof(dis)); int u = 0; while(use[u]) u++; vis[u] = true; for(int v = 0; v < n; v++) if(!use[v] && !vis[v]) dis[v] = G[v][u]; s = t = u; for(int i = u; i < n; i++) { int max = 0, x = u; for(int j = 0; j < n; j++) if(!use[j] && !vis[j] && dis[j] > max) max = dis[x = j]; if(!max) break; vis[x] = true; for(int j = 0; j < n; j++) if(!use[j] && !vis[j]) dis[j] += G[j][x]; s = t, t = x; } cut = min(cut, dis[t]); use[t] = true; for(int i = 0; i < n; i++) if(!use[i]) { G[s][i] += G[t][i]; G[i][s] += G[i][t]; } } return cut; } int main() { int a, b, c; while(~scanf("%d%d", &n, &m)) { memset(G, 0, sizeof(G)); while(m--) { scanf("%d%d%d", &a, &b, &c); G[a][b] += c; G[b][a] += c; } printf("%d\n", StoerWagner()); } return 0; }