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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

USACO Section 4.2 Drainage Ditches(最大流)

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 USACO Section 4.2 Drainage Ditches(最大流) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最大流問題。ISAP算法。注意可能會有重邊,不過我用的數據結構支持重邊。距離d我直接初始化為0,也可以用BFS逆向找一次。

-----------------------------------------------------------------------

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<queue>#define rep(i,l,r) for(int i=l;i<r;i++)#define dow(i,l,r) for(int i=l;i>r;i--)#define clr(x,c) memset(x,c,sizeof(x))using namespace std;const int inf=0x3f3f3f3f,maxn=200+5;struct edge {? ? int from,to,cap,flow;};struct ISAP {? ? int n,m,s,t;? ? vector<edge> edges;? ? vector<int> g[maxn];? ? int d[maxn];? ? int cur[maxn];? ? int p[maxn];? ? int num[maxn];? ? void init(int n) {? ? ? ? this->n=n;? ? ? ? rep(i,0,n) g[i].clear();? ? ? ? edges.clear();? ? }? ? void addEdge(int from,int to,int cap) {? ? ? ? edges.push_back((edge){from,to,cap,0});? ? ? ? edges.push_back((edge){to,from,0,0});? ? ? ? m=edges.size();? ? ? ? g[from].push_back(m-2);? ? ? ? g[to].push_back(m-1);? ? }? ? int augment() {? ? ? ? int x=t,a=inf;? ? ? ? while(x!=s) {? ? ? ? ? ? edge& e=edges[p[x]];? ? ? ? ? ? a=min(a,e.cap-e.flow);? ? ? ? ? ? x=edges[p[x]].from;? ? ? ? }? ? ? ? x=t;? ? ? ? while(x!=s) {? ? ? ? ? ? edges[p[x]].flow+=a;? ? ? ? ? ? edges[p[x]^1].flow-=a;? ? ? ? ? ? x=edges[p[x]].from;? ? ? ? }? ? ? ? return a;? ? }? ? int maxFlow(int s,int t) {? ? ? ? this->s=s; this->t=t;? ? ? ? int flow=0;? ? ? ? clr(d,0);? ? ? ? clr(num,0);? ? ? ? rep(i,0,n) num[d[i]]++;? ? ? ? int x=s;? ? ? ? clr(cur,0);? ? ? ? while(d[s]<n) {? ? ? ? ? ? if(x==t) {? ? ? ? ? ? ? ? flow+=augment();? ? ? ? ? ? ? ? x=s;? ? ? ? ? ? }? ? ? ? ? ? int ok=0;? ? ? ? ? ? rep(i,cur[x],g[x].size()) {? ? ? ? ? ? ? ? edge& e=edges[g[x][i]];? ? ? ? ? ? ? ? if(e.cap>e.flow && d[x]==d[e.to]+1) {? ? ? ? ? ? ? ? ? ? ok=1;? ? ? ? ? ? ? ? ? ? p[e.to]=g[x][i];? ? ? ? ? ? ? ? ? ? cur[x]=i;? ? ? ? ? ? ? ? ? ? x=e.to;? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? if(!ok) {? ? ? ? ? ? ? ? int m=n-1;? ? ? ? ? ? ? ? rep(i,0,g[x].size()) {? ? ? ? ? ? ? ? ? ? edge& e=edges[g[x][i]];? ? ? ? ? ? ? ? ? ? if(e.cap>e.flow) m=min(m,d[e.to]);? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? if(--num[d[x]]==0) break;? ? ? ? ? ? ? ? num[d[x]=m+1]++;? ? ? ? ? ? ? ? cur[x]=0;? ? ? ? ? ? ? ? if(x!=s) x=edges[p[x]].from;? ? ? ? ? ? }? ? ? ? }? ? ? ? return flow;? ? }} isap;int s() {? ? int n,m;? ? cin>>m>>n;? ? isap.init(n);? ? rep(i,0,m) {? ? ? ? int from,to,cap,pd=1;? ? ? ? scanf("%d%d%d",&from,&to,&cap);? ? ? ? isap.addEdge(from-1,to-1,cap);? ? }? ? return isap.maxFlow(0,n-1);}int main() {? ? freopen("ditch.in","r",stdin);? ? freopen("ditch.out","w",stdout);? ? cout<<s()<<endl;? ? return 0;}

-----------------------------------------------------------------------?

Drainage Ditches
Hal Burch

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.

Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. Note however, that there can be more than one ditch between two intersections.

Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

PROGRAM NAME: ditch

INPUT FORMAT

Line 1:Two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream.
Line 2..N+1:Each of N lines contains three integers, Si, Ei, and Ci. Si?and Ei?(1 <= Si, Ei?<= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si?to Ei. Ci?(0 <= Ci?<= 10,000,000) is the maximum rate at which water will flow through the ditch.

SAMPLE INPUT (file ditch.in)

5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10

OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)

50

轉載于:https://www.cnblogs.com/JSZX11556/p/4295472.html

總結

以上是生活随笔為你收集整理的USACO Section 4.2 Drainage Ditches(最大流)的全部內容,希望文章能夠幫你解決所遇到的問題。

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