题目:https://acm.hdu.edu.cn/showproblem.php?pid=6214
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
跑一个dinic的板子竟然花了两个小时。。。一直找不到问题,重新敲了一遍又可以了。。。
建边的时候每条边权 w = w * (E + 1) + 1;
这样得到最大流 maxflow / (E + 1) ,最少割边数 maxflow % (E + 1)
AC代码:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 10010,M = 200020;
const ll inf = ll(1e18);
struct edge{
int next,to;
ll w;
}edges[M];
int head[N];
int cnt = 1;
int cur[N],lv[N];
int n,m,s,t;
void add(int u,int v,ll w){
edges[++cnt].next = head[u];
edges[cnt].to = v;
edges[cnt].w = w;
head[u] = cnt;
}
void init(){
cnt = 1;
memset(head,0,sizeof(head));
}
bool bfs(){
memset(lv,0,sizeof(lv));
lv[s] = 1;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; i; i = edges[i].next){
int to = edges[i].to;
ll w = edges[i].w;
if(w > 0 && lv[to] == 0){
lv[to] = lv[u] + 1;
if(to == t) return true;
q.push(to);
}
}
}
return false;
}
ll dfs(int p = s,ll flow = inf){
if(p == t || !flow) return flow;
ll ret = 0;
for(int& i = cur[p]; i; i = edges[i].next){
int to = edges[i].to;
ll w = edges[i].w;
if(w > 0 && lv[to] == lv[p] + 1){
ll f = dfs(to,min(flow - ret,w));
edges[i].w -= f;
edges[i^1].w += f;
ret += f;
if(ret == flow) return ret;
}
}
return ret;
}
ll Dinic(){
ll ans = 0;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs();
}
return ans;
}
int main(){
int k;
scanf("%d",&k);
while(k--){
init();
scanf("%d%d",&n,&m);
scanf("%d%d",&s,&t);
int u,v;
ll w;
for(int i = 1;i<=m;i++){
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w*(m+1)+1);
add(v,u,0);
}
ll ans = Dinic();
printf("%lld\n",ans%(m+1));
}
}