hdu 1532(最大流)

Drainage Ditches

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

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

网络流入门题,理解剩余网络,反向边,求源点到汇点最大流,网络流推荐博客:http://www.cnblogs.com/zsboy/archive/2013/01/27/2878810.html

不过这个算法(Edmond-Karp)的速度不算太快,最快的是DINIC算法,见下面。

#include <iostream>
#include <queue>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int N = 205;
const int INF = 999999999;
int flow[N]; ///标记从源点到当前节点实际还剩多少流量可用
int cap[N][N]; ///记录残留网络的容量
int pre[N]; ///记录前结点,为寻找路径做准备
int n,m;

int BFS(int src,int des)
{
    queue<int> q;
    for(int i=1; i<=m; i++)
    {
        pre[i]=-1;
    }
    pre[src] = 0;
    flow[src] = INF;
    q.push(src);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        if(u==des) break;
        for(int i=1; i<=m; i++)
        {
            if(i!=src&&cap[u][i]>0&&pre[i]==-1)
            {
                pre[i] = u;
                flow[i] = min(cap[u][i],flow[u]);
                q.push(i);
            }
        }
    }
    if(pre[des]==-1) return -1;///没有增广路了
    return flow[des];
}
void max_flow(int src,int des)
{
    int increaseRoad = 0;
    int MAXFLOW = 0;
    while((increaseRoad=BFS(src,des))!=-1)
    {
        int k = des;
        while(k!=src)
        {
            cap[pre[k]][k]-=increaseRoad; ///正向边的容量
            cap[k][pre[k]]+=increaseRoad; ///反向边的容量
            k = pre[k];
        }
        MAXFLOW+=increaseRoad;
    }
    printf("%d\n",MAXFLOW);
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(cap,0,sizeof(cap));
        for(int i=0; i<n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(u==v) continue;
            cap[u][v]+=w;
        }
        max_flow(1,m);
    }
    return 0;
}

Dinic算法模板:http://blog.csdn.net/wall_f/article/details/8207595

#include <iostream>
#include <queue>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int N = 205;
const int INF = 999999999;
struct Edge{
    int v,w,next;
}edge[N*N];
int head[N];
int level[N];
int n,m;
void addEdge(int u,int v,int w,int &k){
    edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
    edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++; ///建立反向边
}
int BFS(int src,int des){
    queue<int >q;
    memset(level,0,sizeof(level));
    level[src]=1;
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(u==des) return 1;
        for(int k = head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v,w=edge[k].w;
            if(level[v]==0&&w!=0){
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }
    return -1;
}
int dfs(int u,int des,int increaseRoad){
    if(u==des) return increaseRoad;
    int ret=0;
    for(int k=head[u];k!=-1;k=edge[k].next){
        int v = edge[k].v,w=edge[k].w;
        if(level[v]==level[u]+1&&w!=0){
            int MIN = min(increaseRoad-ret,w);
            w = dfs(v,des,MIN);
            edge[k].w -=w;
            edge[k^1].w+=w;
            ret+=w;
            if(ret==increaseRoad) return ret;
        }
    }
    return ret;
}
int Dinic(int src,int des){
    int ans = 0;
    while(BFS(src,des)!=-1) ans+=dfs(src,des,INF);
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(head,-1,sizeof(head));
        int tot = 0;
        for(int i=0;i<n;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(u==v) continue;
            addEdge(u,v,w,tot);
        }
        printf("%d\n",Dinic(1,m));
    }
    return 0;
}
时间: 2024-11-01 12:04:52

hdu 1532(最大流)的相关文章

HDU 1532 最大流模板题

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blog.csdn.net/smartxxyx/article/details/9293665/ 1 #include<stdio.h> 2 #include<string.h> 3 #include<vector> 4 #define maxn 222 5 #define in

hdu 1532(最大流)

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

poj 1273 ---&amp;&amp;--- hdu 1532 最大流模板

最近在换代码布局,因为发现代码布局也可以引起一个人的兴趣这个方法是算法Edmonds-Karp 最短增广路算法,不知道的话可以百度一下,基于Ford-Fulkerson算法的基础上延伸的 其实我不是很透彻的领悟这个算法的精髓,只知道怎样实现,现在的任务就是多刷几道题,见识见识题型,就可以更透彻领悟为什么这么做,之后再拐回来研究算法,这样就可以学习和实践相结合! 详解 : 就是每次广搜后都让走过的边减去这条通路的最小的通路,逆向通路加上这条通路的最小通路,也就是最大容纳量,形成新的通路之后就记录最

hdu 1532(最大流裸题)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9557    Accepted Submission(s): 4534 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||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 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 4888 最大流给出行列和求矩阵

第一步,考虑如何求是否有解.使用网络流求解,每一行和每一列分别对应一个点,加上源点和汇点一共有N+M+2个点.有三类边: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对应的点 -> 每一列对应的点,流量限制为 K 3. 每一列对应的点 -> 汇点,流量限制为该列的和 对上图做最大流,若源点出发的边和到达汇点的边全都满流,则有解,否则无解.若要求构造方案,则 (i,j) 对应的整数就是行 i–> 列 j 的流量. 第二步,考虑解是否唯一.显然,解唯一的充分必要条

hdu 4975最大流与4888类似但是有很吊的优化最大流

//来自潘神的优化 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0x3fffffff #define N 1100 struct node { int u,v,w,next; }bian[N*N*4]; int head[N],yong,dis[N],work[N]; void init(){ yong=0; memset(head,-1,si