POJ2914 Minimum Cut —— 最小割

题目链接:http://poj.org/problem?id=2914


 

Minimum Cut
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 10117 Accepted: 4226
Case Time Limit: 5000MS

 

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

Source

Baidu Star 2006 Semifinal 
Wang, Ying (Originator) 
Chen, Shixi (Test cases)

 




代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int mod = 1e9+7;
17 const int MAXN = 500+10;
18 
19 int mp[MAXN][MAXN];
20 bool combine[MAXN];
21 int n, m;
22 
23 bool vis[MAXN];
24 int w[MAXN];
25 int prim(int times, int &s, int &t) //最大生成树?
26 {
27     memset(w,0,sizeof(w));
28     memset(vis,0,sizeof(vis));
29     for(int i = 1; i<=times; i++)   //times为实际的顶点个数
30     {
31         int k, maxx = -INF;
32         for(int j = 0; j<n; j++)
33             if(!vis[j] && !combine[j] && w[j]>maxx)
34                 maxx = w[k=j];
35 
36         vis[k] = 1;
37         s = t; t = k;
38         for(int j = 0; j<n; j++)
39             if(!vis[j] && !combine[j])
40                 w[j] += mp[k][j];
41     }
42     return w[t];
43 }
44 
45 int mincut()
46 {
47     int ans = INF;
48     memset(combine,0,sizeof(combine));
49     for(int i = n; i>=2; i--)   //每一次循环,就减少一个点
50     {
51         int s, t;
52         int tmp = prim(i, s, t);
53         ans = min(ans, tmp);
54         combine[t] = 1;
55         for(int j = 0; j<n; j++)   //把t点删掉,与t相连的边并入s
56         {
57             mp[s][j] += mp[t][j];
58             mp[j][s] += mp[j][t];
59         }
60     }
61     return ans;
62 }
63 
64 int main()
65 {
66     while(scanf("%d%d",&n,&m)!=EOF)
67     {
68         memset(mp,0,sizeof(mp));
69         for(int i = 1; i<=m; i++)
70         {
71             int u, v, w;
72             scanf("%d%d%d",&u,&v,&w);
73             mp[u][v] += w;
74             mp[v][u] += w;
75         }
76         cout<< mincut() <<endl;
77     }
78     return 0;
79 }
View Code

 


 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538608.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值