2014
02-26

# King of Destruction

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.

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.

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.

2 1
0 1 50
3 2
0 1 50
1 2 10

50
10

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 110;
const int INF = 0x3fffffff;
int n, m;
int g[N][N], v[N], d[N];

int Stoer_wagner()
{
bool vis[N];
int i, j, res = INF;
for ( i = 0; i < n; ++i ) v[i] = i;
while ( n > 1 ) {
int maxp = 1, prev = 0;
for ( i = 1; i < n; ++i ) {
d[v[i]] = g[v[0]][v[i]];
if ( d[v[i]] > d[v[maxp]] ) maxp = i;
}
memset( vis, 0, sizeof(vis) );
vis[v[0]] = true;
for ( i = 1; i < n; i++ ) {
if ( i == n-1 ) {
res = min( res, d[v[maxp]] );
for ( j = 0; j < n; ++j ) {
g[v[prev]][v[j]] += g[v[j]][v[maxp]];
g[v[j]][v[prev]] = g[v[prev]][v[j]];
}
v[maxp] = v[--n];
}
vis[v[maxp]] = true;
prev = maxp;
maxp = -1;
for ( j = 1; j < n; ++j )
if ( !vis[v[j]] ) {
d[v[j]] += g[v[prev]][v[j]];
if ( maxp == -1 || d[v[maxp]] < d[v[j]] ) maxp = j;
}
}
}
return res;
}

int main()
{
while ( scanf("%d%d", &n, &m ) == 2 ) {
memset( g, 0, sizeof(g) );
int x, y, z;
while ( m-- ) {
scanf("%d%d%d", &x, &y, &z);
g[x][y] += z;
g[y][x] += z;
}
printf("%d\n", Stoer_wagner());
}
}