POJ2914 Minimum Cut —— 最小割
题目链接:http://poj.org/problem?id=2914
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 A, B and C (0 ≤ A, B < N, A ≠ B, C > 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
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = +; int mp[MAXN][MAXN];
bool combine[MAXN];
int n, m; bool vis[MAXN];
int w[MAXN];
int prim(int times, int &s, int &t) //最大生成树?
{
memset(w,,sizeof(w));
memset(vis,,sizeof(vis));
for(int i = ; i<=times; i++) //times为实际的顶点个数
{
int k, maxx = -INF;
for(int j = ; j<n; j++)
if(!vis[j] && !combine[j] && w[j]>maxx)
maxx = w[k=j]; vis[k] = ;
s = t; t = k;
for(int j = ; j<n; j++)
if(!vis[j] && !combine[j])
w[j] += mp[k][j];
}
return w[t];
} int mincut()
{
int ans = INF;
memset(combine,,sizeof(combine));
for(int i = n; i>=; i--) //每一次循环,就减少一个点
{
int s, t;
int tmp = prim(i, s, t);
ans = min(ans, tmp);
combine[t] = ;
for(int j = ; j<n; j++) //把t点删掉,与t相连的边并入s
{
mp[s][j] += mp[t][j];
mp[j][s] += mp[j][t];
}
}
return ans;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(mp,,sizeof(mp));
for(int i = ; i<=m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
mp[u][v] += w;
mp[v][u] += w;
}
cout<< mincut() <<endl;
}
return ;
}
POJ2914 Minimum Cut —— 最小割的更多相关文章
- POJ 2914 Minimum Cut 最小割图论
Description Given an undirected graph, in which two vertices can be connected by multiple edges, wha ...
- HDU 6214 Smallest Minimum Cut 最小割,权值编码
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这 ...
- HDU 6214 Smallest Minimum Cut (最小割且边数最少)
题意:给定上一个有向图,求 s - t 的最小割且边数最少. 析:设边的容量是w,边数为m,只要把每边打容量变成 w * (m+1) + 1,然后跑一个最大流,最大流%(m+1),就是答案. 代码如下 ...
- POJ 2914 Minimum Cut 最小割算法题解
最标准的最小割算法应用题目. 核心思想就是缩边:先缩小最大的边.然后缩小次大的边.依此缩小 基础算法:Prime最小生成树算法 只是本题測试的数据好像怪怪的,相同的算法时间执行会区别非常大,并且一样的 ...
- poj2914 Minimum Cut 全局最小割模板题
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 8324 Accepted: 3488 Case ...
- poj2914无向图的最小割模板
题意:给出无向图的点,边,权值.求最小割. 思路:根据题目规模,最大流算法会超时. 网上参考的模板代码. 代码: /*最小割集◎Stoer-Wagner算法:一个无向连通网络,去掉一个边集可以使其变成 ...
- poj2914无向图的最小割
http://blog.csdn.net/vsooda/article/details/7397449 //算法理论 http://www.cnblogs.com/ylfdrib/archive/20 ...
- HDU 6214.Smallest Minimum Cut 最少边数最小割
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU 6214 Smallest Minimum Cut(最少边最小割)
Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...
随机推荐
- Spoj-BITDIFF Bit Difference
Given an integer array of N integers, find the sum of bit differences in all the pairs that can be f ...
- LA 2218 半平面交
题目大意:n名选手参加铁人三项赛,比赛按照选手在三个赛段中所用的总时间排定名次.已知每名选手在三个项目中的速度Ui.Vi.Wi.问对于选手i,能否通过适当的安排三个赛段的长度(但每个赛段的长度都不能 ...
- Python中%r与%s的区别
%r是rper()方法处理的对象 %s是str()方法处理的对象 其实有些情况下,两者处理的结果是一样的,比如说处理数据类型为int型对象: 例如1: print ('I am %d year old ...
- T1008 选数 codevs
http://codevs.cn/problem/1008/ 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整 ...
- Jetson TK1 五:移植工控机程序到板上
1.gazebo xml 2.王 chmod 777 chmod 777 /home/robot2/bzrobot_ws/src/bzrobot/bzrobot_control/bzrobot_con ...
- pandaboard安装ubuntu14.04系统遇到的问题
按照该网址步骤安装https://www.eewiki.net/display/linuxonarm/PandaBoard 在linux kernel的./build_kernel.sh时,自动安装交 ...
- SPOJ 26108 TRENDGCD - Trending GCD
Discription Problem statement is simple. Given A and B you need to calculate S(A,B) . Here, f(n)=n, ...
- 如何稳定地使用 Google 搜索https://encrypted.google.com/
方法很简单.用记事本打开 hosts 文件(Windows Vista 和 Windows 7 用户请先使用管理员权限打开记事本,然后将 hosts 文件拖进记事本中),在最下面添加如下内容: 203 ...
- DICOM医学图像显示算法改进与实现——LUT
引言 随着Ul(超声成像).CT(计算机断层成像).MRI(核磁共振成像).CR(计算机X线成像).电子内窥镜.盯(正电子发射断层成像)和MI(分子影像)等医学影像设备不断涌现,利用计算机对医学影像设 ...
- 【转】DevOps原则
DevOps的出现有其必然性.在软件开发生命周期中,遇到了两次瓶颈. 第一次瓶颈是在需求阶段和开发阶段之间,针对不断变化的需求,对软件开发者提出了高要求,后来出现了敏捷方法论,强调适应需求.快速迭代. ...