博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ Minimum Cut
阅读量:4492 次
发布时间:2019-06-08

本文共 2918 字,大约阅读时间需要 9 分钟。

Minimum Cut
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 9302   Accepted: 3902
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 Mintegers 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 30 1 11 2 12 0 14 30 1 11 2 12 3 18 140 1 10 2 10 3 11 2 11 3 12 3 14 5 14 6 14 7 15 6 15 7 16 7 14 0 17 3 1

Sample Output

212

Source

 
Wang, Ying (Originator) 
Chen, Shixi (Test cases)

[]   [Go Back]   []   []

 

无向图的边连通度,有一个神奇的算法,还有我蒟蒻的模板。

 

1 #include 
2 #include
3 4 inline int min(int a, int b) 5 { 6 return a < b ? a : b; 7 } 8 9 const int inf = 2e9;10 const int maxn = 505;11 12 int n, m;13 int vis[maxn];14 int wet[maxn];15 int com[maxn];16 int G[maxn][maxn];17 18 inline int search(int &S, int &T)19 {20 memset(vis, 0, sizeof(vis));21 memset(wet, 0, sizeof(wet));22 23 S = -1, T = -1;24 25 int id, maxi, ret = 0;26 27 for (int i = 0; i < n; ++i)28 {29 maxi = -inf;30 31 for (int j = 0; j < n; ++j)32 if (!com[j] && !vis[j] && wet[j] > maxi)33 id = j, maxi = wet[j];34 35 if (id == T)36 return ret;37 38 S = T;39 T = id;40 ret = maxi;41 vis[id] = 1;42 43 for (int j = 0; j < n; ++j)44 if (!com[j] && !vis[j])45 wet[j] += G[id][j];46 }47 }48 49 inline int StoerWagner(void)50 {51 int ret = inf, S, T;52 53 memset(com, 0, sizeof(com));54 55 for (int i = 0; i < n - 1; ++i)56 {57 ret = min(ret, search(S, T));58 59 if (!ret)return 0;60 61 com[T] = 1;62 63 for (int j = 0; j < n; ++j)64 if (!com[j])65 G[S][j] += G[T][j],66 G[j][S] += G[j][T];67 }68 69 return ret;70 }71 72 signed main(void)73 {74 while (~scanf("%d%d", &n, &m))75 {76 memset(G, 0, sizeof(G));77 78 for (int i = 1; i <= m; ++i)79 {80 int x, y, w;81 scanf("%d%d%d", &x, &y, &w);82 G[x][y] += w;83 G[y][x] += w;84 }85 86 printf("%d\n", StoerWagner());87 }88 }

 

@Author: YouSiki

转载于:https://www.cnblogs.com/yousiki/p/6230556.html

你可能感兴趣的文章
linux如何挂载windows下的共享文件
查看>>
常用正则表达式
查看>>
Houdini 节点参数读取输入节点的数据列表
查看>>
初识Linq to Entity
查看>>
Linux vmstat命令实战详解
查看>>
FastDFS在centos上的安装配置与使用
查看>>
HDU 1709 The Balance
查看>>
2016/7/7 设置wamp2.5 mysql密码 重点是mysql版本
查看>>
简介几种负载均衡原理
查看>>
micropython logging文档
查看>>
Webform(分页、组合查询)
查看>>
Foundation - NSDate
查看>>
geatpy - 遗传和进化算法相关算子的库函数(python)
查看>>
iOS 线程安全
查看>>
mysql 分组之后统计记录条数
查看>>
New STL Algorithms That Will Make A More Productive Developer
查看>>
js 对象 浅拷贝 和 深拷贝
查看>>
初识 python
查看>>
PCL Examples
查看>>
spring boot
查看>>