poj 2914 全局最小割入门+Stoer_Wagner模板
2017-03-10 22:03
344 查看
题目:
Minimum Cut
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
Sample Output
全局最小割 直接上模板...
板子用的时候注意顶点下标需要从1到n
代码:
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn
4000
=510;
const int inf=0x3f3f3f3f;
int n,m,s,v[maxn],mat[maxn][maxn],dis[maxn];
bool vis[maxn];
int Stoer_Wagner(int n){
int i, j;
int res = inf;
for (i = 0; i < n; i++)
v[i] = i+1;//初始化第i个结点就是i
while (n > 1)
{
int maxp = 1,prev = 0;
for (i = 1; i < n; i++) //初始化到已圈集合的割大小,并找出最大距离的顶点
{
dis[v[i]] = mat[v[0]][v[i]];
if (dis[v[i]] > dis[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,dis[v[maxp]]);
for (j = 0; j < n; j++) //合并最后一个点以及推出它的集合中的点
{
mat[v[prev]][v[j]] += mat[v[j]][v[maxp]];
mat[v[j]][v[prev]] = mat[v[prev]][v[j]];
}
v[maxp] = v[--n];//第maxp个节点去掉,第n个节点变成第maxp个
}
vis[v[maxp]] = true;
prev = maxp;
maxp = -1;
for (j = 1; j < n; j++)
if (!vis[v[j]]) //将上次求的maxp加入集合,合并与它相邻的边到割集
{
dis[v[j]] += mat[v[prev]][v[j]];
if (maxp == -1 || dis[v[maxp]] < dis[v[j]])
maxp = j;
}
}
}
return res;
}
int main(){//904MS 1928K
while (scanf("%d%d",&n,&m)==2){
memset(mat,0,sizeof (mat));
int x,y,z;
while(m--){
scanf("%d%d%d",&x,&y,&z);
mat[x+1][y+1] += z;
mat[y+1][x+1] += z;
}
printf("%d\n",Stoer_Wagner(n));
}
}
Minimum Cut
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 9496 | Accepted: 3981 | |
Case Time Limit: 5000MS |
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
全局最小割 直接上模板...
板子用的时候注意顶点下标需要从1到n
代码:
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn
4000
=510;
const int inf=0x3f3f3f3f;
int n,m,s,v[maxn],mat[maxn][maxn],dis[maxn];
bool vis[maxn];
int Stoer_Wagner(int n){
int i, j;
int res = inf;
for (i = 0; i < n; i++)
v[i] = i+1;//初始化第i个结点就是i
while (n > 1)
{
int maxp = 1,prev = 0;
for (i = 1; i < n; i++) //初始化到已圈集合的割大小,并找出最大距离的顶点
{
dis[v[i]] = mat[v[0]][v[i]];
if (dis[v[i]] > dis[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,dis[v[maxp]]);
for (j = 0; j < n; j++) //合并最后一个点以及推出它的集合中的点
{
mat[v[prev]][v[j]] += mat[v[j]][v[maxp]];
mat[v[j]][v[prev]] = mat[v[prev]][v[j]];
}
v[maxp] = v[--n];//第maxp个节点去掉,第n个节点变成第maxp个
}
vis[v[maxp]] = true;
prev = maxp;
maxp = -1;
for (j = 1; j < n; j++)
if (!vis[v[j]]) //将上次求的maxp加入集合,合并与它相邻的边到割集
{
dis[v[j]] += mat[v[prev]][v[j]];
if (maxp == -1 || dis[v[maxp]] < dis[v[j]])
maxp = j;
}
}
}
return res;
}
int main(){//904MS 1928K
while (scanf("%d%d",&n,&m)==2){
memset(mat,0,sizeof (mat));
int x,y,z;
while(m--){
scanf("%d%d%d",&x,&y,&z);
mat[x+1][y+1] += z;
mat[y+1][x+1] += z;
}
printf("%d\n",Stoer_Wagner(n));
}
}
相关文章推荐
- POJ 2914 Minimum Cut 最小割集Stoer-Wagner算法(全局最小割)
- POJ 2914 Minimum Cut Stoer-Wagner(全局最小割)
- poj 2914 全局最小割 stoer wagner算法 最小割模板
- poj 2914 Minimum Cut 【无向图全局最小割 Stoer-wagner算法】
- poj 2914&&hdu 3002 全局最小割Stoer-Wagner算法模板
- Poj 2914 无向图的全局最小割
- POJ 2914 Minimum Cut (全局最小割)
- POJ 1258 Agri-Net 【最小生成树入门题目】【prime模板】
- poj 2914(stoer_wanger算法求全局最小割)
- POJ 2914 Minimum Cut(求全图的最小割Stoer-Wagner)
- 无向带权图最小割stoer-wagner算法(poj 2914 Minimum cut)
- POJ 2914 无向图的最小割,模板题。
- POJ 2914 Minimum Cut 全局最小割
- POJ 2914 Minimun Cut (Stoer-Wagner, 无向图最小割)
- POJ 2914 Minimum Cut (最小割模板题)
- poj 2914(stoer_wanger算法求全局最小割)
- poj2914——Minimum Cut//最小割Stoer_Wagner
- POJ_P2914 Minimum Cut(Stoer-Wagner算法 全局最小割)
- POJ 2914 Minimum Cut 全局最小割
- POJ2914 Minimum Cut 【全局最小割Stoer-Wagner模板题】