日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 6214 Smallest Minimum Cut(最小割边,两种方法)

發(fā)布時間:2024/4/18 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 6214 Smallest Minimum Cut(最小割边,两种方法) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

Consider a network G=(V,E)G=(V,E) with source ss and sink tt. An s-t cut is a partition of nodes set VV into two parts such that ss and tt belong to different parts. The cut set is the subset of EE 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?(1T300)T(1≤T≤300).Each case describes a network GG, and the first line contains two integers n (2n200)n (2≤n≤200) and m?(0m1000)m(0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 11 to nn.The second line contains two different integers ss and t (1s,tn)t (1≤s,t≤n) corresponding to the source and sink.Each of the next mm lines contains three integers u,vu,v and w?(1w255)w(1≤w≤255) describing a directed edge from node uu to vv with capacity ww<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.

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

AC

  • 方法一:先跑一次最大流,將滿流邊的流量加1反向流量不變,不滿流的邊流量變?yōu)閕nf,反向流量不變,然后再跑一次最大流
  • 方法二:建圖的時候把流量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_JUDGEfreopen("in.txt", "r", stdin); #endifint 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_JUDGEfreopen("in.txt", "r", stdin); #endifint 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(最小割边,两种方法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。