[USACO4.2.1]Drainage Ditches

xiaoxiao2021-02-28  68

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 题意:裸的最大流问题 分析: 这大概是我第3次成功做最大流(好像也只会最大流) 第一次是POJ上,也是这道题 第二次是codevs上,也是这道题 第三次是USACO上,还是这道题。

ISAP具体可以看

http://blog.csdn.net/qq_37665716/article/details/76998546

这里给出

DINIC:

/* ID:cqz15311 LANG:C++ PROG:ditch */ #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<cstdlib> using namespace std; const int oo = 1<<20; const int maxv = 205; const int maxe = 405; struct Edge{ int to,nxt,cap,flow; }edge[maxe]; int first[maxv],work[maxv]; int dist[maxv]; int q[maxv]; int nume,n,m,s,t; void add_edge(int a,int b,int c){ edge[nume] . to = b; edge[nume] . cap = c; edge[nume] . flow = 0; edge[nume] . nxt = first[a]; first[a] = nume++; edge[nume] . to = a; edge[nume] . cap = 0; edge[nume] . flow = 0; edge[nume] . nxt = first[b]; first[b] = nume++; } void init(){ memset(first,-1,sizeof(first)); scanf("%d%d",&n,&m); s = 1; t = m; for (int i=1;i<=n;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); } } bool dinic_bfs(){ int front = 0,rear = 0; memset(dist,-1,sizeof(dist)); q[rear++] = s;dist[s] = 0; while (front < rear){ int u = q[front++]; for (int e=first[u];e!=-1;e=edge[e].nxt){ if ((edge[e].cap > edge[e].flow) && (dist[edge[e].to] < 0)){ dist[edge[e].to] = dist[u] + 1; q[rear++] = edge[e].to; } } } return dist[t] >= 0; } int dinic_dfs(int u,int c){ if (u == t) return c; for (int &e=work[u];e!=-1;e=edge[e].nxt) if ((edge[e].cap > edge[e].flow) && (dist[edge[e].to] == dist[u] + 1)){ int tmp = dinic_dfs(edge[e].to,min(c,edge[e].cap-edge[e].flow)); if (tmp > 0){ edge[e] . flow += tmp; edge[e ^ 1].flow -= tmp; return tmp; } } return 0; } int dinic_flow(){ int rec = 0; while (dinic_bfs()){ for (int i=1;i<=m;i++) work[i] = first[i]; while (1){ int d = dinic_dfs(s,oo); if (d == 0) break; rec += d; } } return rec; } int main(){ freopen("ditch.in","r",stdin); freopen("ditch.out","w",stdout); init(); printf("%d\n",dinic_flow()); fclose(stdin); fclose(stdout); return 0; } /* Executing... Test 1: TEST OK [0.000 secs, 4188 KB] Test 2: TEST OK [0.000 secs, 4188 KB] Test 3: TEST OK [0.000 secs, 4188 KB] Test 4: TEST OK [0.000 secs, 4188 KB] Test 5: TEST OK [0.000 secs, 4188 KB] Test 6: TEST OK [0.000 secs, 4188 KB] Test 7: TEST OK [0.000 secs, 4188 KB] Test 8: TEST OK [0.000 secs, 4188 KB] Test 9: TEST OK [0.000 secs, 4188 KB] Test 10: TEST OK [0.000 secs, 4188 KB] Test 11: TEST OK [0.000 secs, 4188 KB] Test 12: TEST OK [0.000 secs, 4188 KB] All tests OK. YOUR PROGRAM ('ditch') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations. */
转载请注明原文地址: https://www.6miu.com/read-62132.html

最新回复(0)