您的位置:首页 > 理论基础 > 计算机网络

HDU - 6214 Smallest Minimum Cut (2017 ACM-ICPC 亚洲区 (青岛赛区) 网络赛 1009)

2017-09-17 20:14 453 查看



2017 ACM/ICPC Asia Regional Qingdao Online 1009


Smallest Minimum Cut

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

 

Source

2017 ACM/ICPC Asia Regional Qingdao Online

 

题意:最小最小割。带起点和终点。

解题思路:套了份模板……也忘了是谁了的了……总之感谢该大佬的博客……

#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
using namespace std;

typedef long long LL;
const LL INF = 100000000000000000;
const int MAXN = 5010;
const int MAXM = 1000010;
struct Edge{
LL flow, cap;
int to, next, pair;
}edge[MAXM<<1];
int cnt, box[MAXN];
void init(){
cnt = 1;
memset(box,-1,sizeof(box));
}
void addedge(int from, int to, LL cap1, LL cap2=0){
edge[cnt].to = to;
edge[cnt].next = box[from];
edge[cnt].cap = cap1;
edge[cnt].flow = 0;
edge[cnt].pair = cnt + 1;
box[from] = cnt++;

edge[cnt].to = from;
edge[cnt].next = box[to];
edge[cnt].cap = cap2;
edge[cnt].flow = 0;
edge[cnt].pair = cnt - 1;
box[to] = cnt++;
}

LL SAP(int st, int ed, int n){
int u, tmp, neck, i;
LL max_flow, cur_flow;
int numb[MAXN], dist[MAXN], curedge[MAXN], pre[MAXN];
memset(dist, 0, sizeof(dist));
memset(numb, 0, sizeof(numb));
memcpy(curedge, box, sizeof(box));
numb
= n;
max_flow = 0;
u = st;
while(dist[st] < n){
if(u == ed){
cur_flow = INF + 1;
for(i = st; i != ed; i = edge[curedge[i]].to)
if(cur_flow > edge[curedge[i]].cap){
neck = i;
cur_flow = edge[curedge[i]].cap;
}
for(i = st; i != ed; i = edge[curedge[i]].to){
tmp = curedge[i];
edge[tmp].cap -= cur_flow;
edge[tmp].flow += cur_flow;
tmp = edge[tmp].pair;
edge[tmp].cap += cur_flow;
edge[tmp].flow -= cur_flow;
}
max_flow += cur_flow;
u = neck;
}
for(i = curedge[u]; i != -1; i = edge[i].next)
if(edge[i].cap > 0 && dist[u] == dist[edge[i].to]+1)
break;
if(i != -1){
curedge[u] = i;
pre[edge[i].to] = u;
u = edge[i].to;
}else{
if(0 == --numb[dist[u]])
break;
curedge[u] = box[u];
for(tmp = n,i = box[u]; i != -1; i = edge[i].next)
if(edge[i].cap > 0)
tmp = tmp<dist[edge[i].to]?tmp:dist[edge[i].to];
dist[u] = tmp + 1;
++numb[dist[u]];
if(u != st)
u = pre[u];
}
}
return max_flow;
}
int main(){
int n, m, t;
int u, v;
LL c;
scanf("%d", &t);
while (t--){
scanf("%d%d", &n, &m);
init();
int S,T;
scanf("%d%d",&S,&T);
while (m--){
scanf("%d%d%lld", &u, &v, &c);
addedge(u, v, c*255001+1);
}
LL ans = SAP(S, T, n);
printf("%lld\n",ans % 255001);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐