Smallest Minimum Cut

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2281    Accepted Submission(s): 913

Problem Description
Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.
 
Input
The input contains several test cases and the first line is the total number of cases T (1≤T≤300).
Each case describes a network G, and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1≤s,t≤n) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w.
 
Output
For each test case, output the smallest size of all minimum cuts in a line.
 
Sample Input
2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 3
 
Sample Output
2
3
 
就是求最小割的边的集合
 
最小割的边就等于满流的边
1、将每条边的权值改为w*(m+1)+1, 最后求的最大流除以(m+1)就是原图的最大流,模上(m+1)就是最小割的边
2、求得最大流之后,将所有的满流的边权设为1,不满流的边权设为INF,然后跑一边最大流就是最小割的边
 
 
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], cnt;
int d[maxn], vis[maxn], cur[maxn];
int s, t;
struct node
{
int u, v, c, next;
}Node[maxn<<]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} void init()
{
mem(head, -);
cnt = ;
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = , V;
if(u==t || cap == )
return cap;
for(int &i=cur[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[e.u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int dinic(int u)
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} int main()
{
int T, n, m;
rd(T);
while(T--)
{
int u, v, c;
init();
rd(n); rd(m);
rd(s), rd(t);
rep(i, , m)
{
rd(u), rd(v), rd(c);
add(u, v, c*(m+) + );
} int res = dinic(s);
cout<< res % (m+) <<endl; } return ;
}

最新文章

  1. Torch7学习笔记(四)StochasticGradient
  2. word2vec + transE 知识表示模型
  3. python pip install
  4. Retrofit
  5. 版本控制 - Git
  6. UVa 1252 (状压DP + 记忆化搜索) Twenty Questions
  7. [html]js打开指定页面
  8. 初识pngdrive
  9. 关于typedef的用法总结
  10. eclispe远程调试tomcat
  11. redis菜鸟教程
  12. PHP实现微信模板消息发送给指定用户
  13. shiro源码篇 - 疑问解答与系列总结,你值得拥有
  14. ubuntu安装和分区方案
  15. json转换学习
  16. [20180423]flashback tablespace与snapshot standby.txt
  17. 2018-2019-2 20175105王鑫浩 实验二《Java面向对象程序设计》实验报告
  18. 如何在Anaconda中实现多版本python共存
  19. FastReport&quot;Text&quot;对象中的HTML标签介绍以及使用
  20. SolrServer SolrRequest

热门文章

  1. python中的运算符的分类以及使用方法
  2. 16节实用性爆棚的Ps课:零基础秒上手,让你省钱也赚钱
  3. 我在华为,软件测试人员在工作中如何运用Linux?
  4. c语言数字图像处理(十):阈值处理
  5. c语言数字图像处理(四):灰度变换
  6. Python自动化运维
  7. Discuz3.3精仿小米风格整站模板制作——1、新建模板方案
  8. trustbox文件破解
  9. node上的__dirname和./的区别
  10. React.js - 入门