Description

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

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 are M lines, each line contains M integers AB and C (0 ≤ AB < NA ≠ BC > 0), meaning that there C edges connecting vertices A and B.

Output

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.

Sample Input

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

Sample Output

2
1
2

一道联系无向图最小割的模板题……
没什么多说的
粘模版即可
#include<cstdio>
#include<cstring>
#define nn 501
#define InF 0x7f7f7f7f
using namespace std;
int vis[nn],dis[nn],b[nn],a[nn][nn],s,t,sum,n;
void dfs()
{
     int Max,tmp;
     memset(vis,0,sizeof(vis));
     memset(dis,0,sizeof(dis));
     s=t=-1;
     for(int i=0;i<n;i++)
	 {
         Max=-InF;
         for(int j=0;j<n;j++)
             if(!b[j]&&!vis[j] && dis[j]>Max)
			 {
                tmp=j;
                Max=dis[j];
             }
         if(t==tmp) return;
         s=t;
		 t=tmp;
         sum=Max;
         vis[tmp]=1;
         for(int j=0;j<n;j++)
             if(!b[j]&&!vis[j])
                dis[j]+=a[tmp][j];
     }
}

int cut()
{
    memset(b,0,sizeof(b));
    int ans=InF;
    for(int i=0;i<n-1;i++)
	{
        dfs();
        if(sum<ans) ans=sum;
        if(ans==0) return 0;
        b[t]=1;
        for(int j=0;j<n;j++)
            if(!b[j])
			{
               a[s][j]+=a[t][j];
               a[j][s]+=a[j][t];
            }
    }
    return ans;
}

int main()
{

    int x,y,z,M;
    while(scanf("%d%d",&n,&M) != EOF)
	{
       memset(a,0,sizeof(a));
       while(M--)
	   {
          scanf("%d%d%d",&x,&y,&z);
          a[x][y]+=z;
          a[y][x]+=z;
       }
       printf("%d\n",cut());
    }
    return 0;
}