Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 8853 | Accepted: 3746 | |
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 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
Wang, Ying (Originator)
Chen, Shixi (Test cases)
题目意思:
N个点,M行,每行给出A点到B点之间有C条路径,求最小割。
解题思路:
求最小割的裸题,必会stoer_wagner最小割算法。
算法实现思路:
1. 设最小割cut=INF, 任选一个点s到集合A中, 定义W(A, p)为A中的所有点到A外一点p的权总和.
2. 对刚才选定的s, 更新W(A,p)(该值递增).
3. 选出A外一点p, 且W(A,p)最大的作为新的s, 若A!=G(V), 则继续2.
4. 把最后进入A的两点记为s和t, 用W(A,t)更新cut.
5. 新建顶点u, 边权w(u, v)=w(s, v)+w(t, v), 删除顶点s和t, 以及与它们相连的边.
6. 若|V|!=1则继续1.
相同的或者是:1.min=MAXINT,固定一个顶点P
2.从点P用“类似”prim的s算法扩展出“最大生成树”,记录最后扩展的顶点和最后扩展的边
3.计算最后扩展到的顶点的切割值(即与此顶点相连的所有边权和),若比min小更新min
4.合并最后扩展的那条边的两个端点为一个顶点(当然他们的边也要合并,这个好理解吧?)
5.转到2,合并N-1次后结束
6.min即为所求,输出min
prim本身复杂度是O(n^2),合并n-1次,算法复杂度即为O(n^3)
传送门:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX_N 505
#define INF 0x3f3f3f3f
int G[MAX_N][MAX_N];//原图
int v[MAX_N];//v[i]代表节点i合并到的顶点
int w[MAX_N];//定义w(A,x) = ∑w(v[i],x),v[i]∈A
bool visited[MAX_N];//用来标记是否该点加入了A集合
int s,t;//源点与汇点
int stoer_wagner(int n)//stoer_wagner最小割算法
{
int min_cut = INF;//最小割cut=INF
int i, j, k;
for (i = 0; i < n; ++i)
v[i] = i;//初始还未合并所以都代表节点本身
while (n > 1)
{
int pre = 0;//pre用来表示之前加入A集合的点(在t之前一个加进去的点)
memset(visited, 0, sizeof(visited));
memset(w, 0, sizeof(w));
/******************从点s开始扩展出一棵最大生成树**********************/
for (i = 1; i < n; ++i)//任选一个点s到集合A中, 定义W(A, p)为A中的所有点到A外一点p的权总和
{
k = -1;
for (j = 1; j < n; ++j)//选取V-A中的w(A,x)最大的点x加入集合
if (!visited[v[j]])//该点还没有加入A集合
{
w[v[j]] += G[v[pre]][v[j]];//对刚才选定的s, 更新W(A,p)(该值递增)
if (k == -1 || w[v[k]] < w[v[j]])//选出A外一点p, 且W(A,p)最大的作为新的s
k = j;
}
visited[v[k]] = true;//标记该点x已经加入A集合
/****************************************/
if(i == n-1)//若|A|=|V|(所有点都加入了A),结束
{
min_cut = min(min_cut, w[t]);//把最后进入A的两点记为s和t,则s-t最小割为w(A,t),用其更新min_cut
for (j = 0; j < n; ++j) //Contract(s, t), 合并st,即新建顶点u, 边权w(u, v)=w(s, v)+w(t, v), 删除顶点s和t, 以及与它们相连的边
{
G[s][v[j]] += G[v[j]][t];
G[v[j]][s] += G[v[j]][t];
}
v[k] = v[--n];//删除最后一个点(即删除t,也即将t合并到s),若|V|!=1则继续
s = v[pre], t = v[k];//令倒数第二个加入A的点(v[pre])为s,最后一个加入A的点(v[k])为t
}
pre = k;//继续
}
}
return min_cut;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
while (cin>>n>>m)
{
memset(G, 0, sizeof(G));
while (m--)
{
int u, v, w;
cin>>u>>v>>w;
G[u][v] += w;
G[v][u] += w;
}
cout<<stoer_wagner(n)<<endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return 0;
}