題目鏈接
Problem Description
Consider a network G = ( V , E ) G=(V,E) with source s s and sink t t . An s-t cut is a partition of nodes set V V into two parts such that s s and t t belong to different parts. The cut set is the subset of E 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.
The input contains several test cases and the first line is the total number of cases T ? ( 1 ≤ T ≤ 300 ) T(1≤T≤300) .Each case describes a network G G , and the first line contains two integers n ( 2 ≤ n ≤ 200 ) n (2≤n≤200) and m ? ( 0 ≤ m ≤ 1000 ) m(0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 1 to n n .The second line contains two different integers s s and t ( 1 ≤ s , t ≤ n ) t (1≤s,t≤n) corresponding to the source and sink.Each of the next m m lines contains three integers u , v u,v and w ? ( 1 ≤ w ≤ 255 ) w(1≤w≤255) describing a directed edge from node u u to v v with capacity w w <script type="math/tex" id="MathJax-Element-21">w</script>.
Output
For each test case, output the smallest size of all minimum cuts in a line.
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
AC
方法一:先跑一次最大流,將滿流邊的流量加1反向流量不變,不滿流的邊流量變為inf,反向流量不變,然后再跑一次最大流 方法二:建圖的時候把流量C, 改為C * (M + 1) + 1; 然后跑一個最大流,最大流 = maxflow / (M + 1) 最小割邊 = maxflow % (M + 1)
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 205
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std ;
struct ac{
int v, c, pre;
}edge[
8000001 ];
int head[N], dis[N], curedge[N], cnt;
int inf =
0x3f3f3f3f ;
void addedge(
int u,
int v,
int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c =
0 ;edge[cnt].pre = head[u];head[u] = cnt++;
}
bool bfs(
int s,
int e) {
queue <int > que;que.push(s);
memset (dis,
0 ,
sizeof (dis));dis[s] =
1 ;
while (!que.empty()) {
int t = que.front();que.pop();
for (
int i = head[t]; i != -
1 ; i = edge[i].pre) {
if (dis[edge[i].v] || edge[i].c ==
0 )
continue ;dis[edge[i].v] = dis[t] +
1 ;que.push(edge[i].v);} }
return dis[e] !=
0 ;
}
int dfs(
int s,
int e,
int flow) {
if (s == e)
return flow;
for (
int &i = curedge[s]; i != -
1 ; i = edge[i].pre) {
if (dis[edge[i].v] == dis[s] +
1 && edge[i].c) {
int d = dfs(edge[i].v, e, min(flow, edge[i].c));
if (d >
0 ) {edge[i].c -= d;edge[i ^
1 ].c += d;
return d; }}}
return 0 ;
}
int n, d, m;
int solve(
int s,
int e) {
int sum =
0 ;
while (bfs(s, e)) {
for (
int i =
1 ; i < N ; ++i) {curedge[i] = head[i];}
int d;
while (d = dfs(s, e, inf)) {sum += d;}}
return sum;
}
int main() {
#ifndef ONLINE_JUDGE freopen(
"in.txt" ,
"r" , stdin);
#endif int t;
scanf (
"%d" , &t);
while (t--) {
memset (head, -
1 ,
sizeof (head));cnt =
0 ;
int n, m, s, e;
scanf (
"%d%d%d%d" , &n, &m, &s, &e);
for (
int i =
0 ; i < m; ++i) {
int x, y, c;
scanf (
"%d%d%d" , &x, &y, &c);addedge(x, y, c);}solve(s, e);
for (
int i =
0 ; i < cnt; i +=
2 ) {
if (edge[i].c ==
0 ) edge[i].c =
1 ;
else edge[i].c = inf;}
int ans = solve(s, e);
printf (
"%d\n" , ans);}
return 0 ;
}
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 205
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std ;
struct ac{
int v, c, pre;
}edge[
8000001 ];
int head[N], dis[N], curedge[N], cnt;
int inf =
0x3f3f3f3f ;
void addedge(
int u,
int v,
int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c =
0 ;edge[cnt].pre = head[u];head[u] = cnt++;
}
bool bfs(
int s,
int e) {
queue <int > que;que.push(s);
memset (dis,
0 ,
sizeof (dis));dis[s] =
1 ;
while (!que.empty()) {
int t = que.front();que.pop();
for (
int i = head[t]; i != -
1 ; i = edge[i].pre) {
if (dis[edge[i].v] || edge[i].c ==
0 )
continue ;dis[edge[i].v] = dis[t] +
1 ;que.push(edge[i].v);} }
return dis[e] !=
0 ;
}
int dfs(
int s,
int e,
int flow) {
if (s == e)
return flow;
for (
int &i = curedge[s]; i != -
1 ; i = edge[i].pre) {
if (dis[edge[i].v] == dis[s] +
1 && edge[i].c) {
int d = dfs(edge[i].v, e, min(flow, edge[i].c));
if (d >
0 ) {edge[i].c -= d;edge[i ^
1 ].c += d;
return d; }}}
return 0 ;
}
int n, d, m;
int solve(
int s,
int e) {
int sum =
0 ;
while (bfs(s, e)) {
for (
int i =
1 ; i < N ; ++i) {curedge[i] = head[i];}
int d;
while (d = dfs(s, e, inf)) {sum += d;}}
return sum;
}
int main() {
#ifndef ONLINE_JUDGE freopen(
"in.txt" ,
"r" , stdin);
#endif int t;
scanf (
"%d" , &t);
while (t--) {
memset (head, -
1 ,
sizeof (head));cnt =
0 ;
int n, m, s, e;
scanf (
"%d%d%d%d" , &n, &m, &s, &e);
for (
int i =
0 ; i < m; ++i) {
int x, y, c;
scanf (
"%d%d%d" , &x, &y, &c);addedge(x, y, c * (m +
1 ) +
1 );}
int ans = solve(s, e) % (m +
1 );
printf (
"%d\n" , ans);}
return 0 ;
}
總結
以上是生活随笔 為你收集整理的HDU Problem - 6214 Smallest Minimum Cut(最小割边,两种方法) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。