HDU 1532 Dinic

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10242    Accepted Submission(s): 4867

Problem Description

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.
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.

Input

The
input includes several cases. For each case, the first line contains
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. Each of the following 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.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

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

Sample Output

50

Source

USACO 93

Recommend

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int inf=0x3fffffff;
const int maxn=207;
struct Edge
{
    int cap,flow;
};
int n,m,s,t;
Edge edges[maxn][maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init()
{
          memset(edges,0,sizeof(edges));
}
bool BFS()
{
    memset(vis,0,sizeof(vis));
    queue<int>Q;
    Q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!Q.empty()){
        int x=Q.front();Q.pop();
        for(int i=1;i<=n;i++){
            Edge &e=edges[x][i];
            if(!vis[i]&&e.cap>e.flow){
                vis[i]=1;
                d[i]=d[x]+1;
                Q.push(i);
            }
        }
    }
    return vis[t];
}
int DFS(int u,int cp)//进行增广
{
    int tmp=cp;
    int v,t;
    if(u==n)
        return cp;
    for(v=1;v<=n&&tmp;v++)
    {
        if(d[u]+1==d[v])
        {
            if(edges[u][v].cap>edges[u][v].flow)
            {
                t=DFS(v,min(tmp,edges[u][v].cap-edges[u][v].flow));
                edges[u][v].flow+=t;
                edges[v][u].flow-=t;
                tmp-=t;
            }
        }
    }
    return cp-tmp;
}
int Maxflow()
{
    int flow=0;
    while(BFS()){
        memset(cur,0,sizeof(cur));
        flow+=DFS(s,inf);
    }
    return flow;
}
int main()
{
   //freopen("in.txt","r",stdin);
    int a,b, c;
    while(~scanf("%d%d",&m,&n)){
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            edges[a][b].cap+=c;
        }
        s=1;t=n;
        int ans=Maxflow();
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-07 18:02:00

HDU 1532 Dinic的相关文章

【网络流#3】hdu 1532 - Dinic模板题

输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/article/details/7913061 建议大家去这个页面看看,博主也很良心地添加了很多注释 关于这个模板:Edge为前向星的边数,所以需要初始化Edge和head数组 n表示有n个点,这个版无所谓点从0开始还是从1开始,s表示源点,t表示汇点很好的一个是,这个版的DFS使用的是模拟栈,防止爆栈 1 #

HDU 1532||POJ1273:Drainage Ditches(最大流)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8574    Accepted Submission(s): 3991 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's

HDU 1532 Drainage Ditches 最大排水量 网络最大流 Edmonds_Karp算法

题目链接:HDU 1532 Drainage Ditches 最大排水量 Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9641    Accepted Submission(s): 4577 Problem Description Every time it rains on Farmer John

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 Description 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

HDU 3987 &amp;&amp; DINIC

很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流,但有可能是最少边数最少割的边呢?否.因为按照DINIC的求法,每次都是增广容量最少的路,若当前不满流,则必定不是最小割的边,所以只需在满流的边,即可组成最小割的边寻找最少边就可以了. 1 #include <iostream> 2 #include <cstring> 3 #incl

HDU 1532 --&amp;&amp;-- POJ1273 dinic 算法

学长的代码#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int MAXN = 1005; const int oo = 1e9+7; struct Edge { int v, flow, next; }edge[MAXN]; int Head[MAXN], cnt; int Layer[MAXN]; voi

最大流算法 Dinic HDU 1532

#include <iostream> #include <cstring> #include <queue> #include <cstdio> using namespace std; #define V 205 #define E 205 #define INF 0x7ffffff struct Edge { int u, v, c, next; } e[E<<2]; int n, m, s, t; int d[V], head[V], c

HDU 1532

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题意: 三叶草是这个人的最喜欢的植物,结果下雨淹没了他家里,要排水,一个点到一个点的排水速度已知,求最大排水能力. 我仔细看了题面,好像是没有具体说明起点和终点. 所以我用最大流,枚举起点终点,并且清流,当然我知道还是可能超时的. 看了一下网上的解答,硬说起点是 1 ,终点 m,我也是很迷啊~~~ 1 #include <bits/stdc++.h> 2 3 using namespace

hdu - 1532 Drainage Ditches (最大流)

http://acm.hdu.edu.cn/showproblem.php?pid=1532 求最大的流量,用dinic算法就好. 1 // Rujia Liu 2 // 因为图较大,所以采用Dinic而不是EdmondsKarp 3 // 得益于接口一致性,读者无须理解Dinic就能使用它. 4 #include<cstdio> 5 #include<cstring> 6 #include<queue> 7 #include<algorithm> 8 us