POJ 1273 Drainage Ditches 最大流
生活随笔
收集整理的這篇文章主要介紹了
POJ 1273 Drainage Ditches 最大流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很裸的最大流問題,不過注意會有重邊,o(╯□╰)o,被陰了WA了一發 還有就是要用long long
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <queue> #include <stack>using namespace std;typedef long long LL; const int maxn = 205; const int maxm = 205; const int INF = INT_MAX / 2; LL flow[maxn][maxn],C[maxn][maxn]; int n,m;void input() {memset(C,0,sizeof(C));for(int i = 1;i <= m;i++) {int a,b,c; scanf("%d%d%d",&a,&b,&c);C[a][b] += c;} }LL alpha[maxn]; int pre[maxn]; int q[maxn + 20],qs,qe;void solve() {int s = 1,t = n;memset(flow,0,sizeof(flow));while(1) {for(int i = s + 1;i <= t;i++) pre[i] = -2;pre[s] = -1;qs = 0; qe = 1; q[qs] = s;alpha[s] = 1e13;while(qs < qe && pre[t] == -2) {int v = q[qs]; qs++;for(int i = s;i <= t;i++) {if(pre[i] == -2 && C[v][i] - flow[v][i] != 0) {alpha[i] = min(alpha[v],C[v][i] - flow[v][i]);pre[i] = v;q[qe++] = i;}}}if(pre[t] == -2) break;for(int i = t;pre[i] != -1;i = pre[i]) {flow[pre[i]][i] += alpha[t];flow[i][pre[i]] = -flow[pre[i]][i];}}LL ans = 0;for(int i = s;i < t;i++) ans += flow[i][t];printf("%lld\n",ans); }int main() {while(scanf("%d%d",&m,&n) != EOF) {input();solve();}return 0; }
轉載于:https://www.cnblogs.com/rolight/p/3869683.html
總結
以上是生活随笔為你收集整理的POJ 1273 Drainage Ditches 最大流的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaSE_base04_集合框架
- 下一篇: codechef LEMOVIE dp