poj 1273 裸 网络流 (dinic)

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

裸的 dinic  代码如下...  可做模板 orzzzz
#include<iostream>
#include<cstring>
#include<cstdio>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
#include<vector>
#define INFINFE 999999999
#define N 300
using namespace std;
int  G[300][300];
bool visited[300];
int  layer[300];
int  n,m;
bool countlayer()
{
   // cout<<"***"<<endl;
    //int Layer=0;
    deque<int>q;
    memset(layer,0xff,sizeof(layer));
    layer[1]=0;
    q.push_back(1);
    while(!q.empty())
    {
        int v=q.front();
        q.pop_front();
        for(int j=1; j<=m; j++)
        {
            if(G[v][j]>0&&layer[j]==-1)
            {
            layer[j]=layer[v]+1;
            if(j==m)
                return true;
            else
                q.push_back(j);
        }
      }
    }
    return false;
}
int Dinic()
{
    int i;
    //int s;
    int nmaxflow=0;
    deque<int>q;
    while(countlayer())
    {
        while(!q.empty())
            q.pop_back();
        q.push_back(1);
        memset(visited,0,sizeof(visited));
        visited[1]=1;

        while(!q.empty())
        {
            int nd=q.back();
            if(nd==m)
            {
                int nminc=INFINFE;
                int nminc_vs;
                for(unsigned int i=1; i<q.size(); i++)
                {
                    int vs=q[i-1];
                    int ve=q[i];
                    if(G[vs][ve]>0)
                    {
                        if(nminc>G[vs][ve])
                        {
                            nminc=G[vs][ve];
                            nminc_vs=vs;
                        }
                    }
                }
                nmaxflow+=nminc;
                for(unsigned int i=1; i<q.size(); i++)
                {
                    int vs=q[i-1];
                    int ve=q[i];
                    G[vs][ve]-=nminc;
                    G[ve][vs]+=nminc;
                }
                while(!q.empty()&&q.back()!=nminc_vs)
                {
                    visited[q.back()]=0;
                    q.pop_back();
                }
            }
            else
            {
                for(i=1; i<=m; i++)
                {
                    if(G[nd][i]>0&&layer[i]==layer[nd]+1&&!visited[i])
                    {
                        visited[i]=1;
                        q.push_back(i);
                        break;
                    }
                }
                if(i>m)
                    q.pop_back();
            }
        }
    }
    return nmaxflow;
}
int main()
{
    while(cin>>n>>m)
    {
        int i;
        int s,e,c;
        memset(G,0,sizeof(G));
        for(i=0; i<n; i++)
        {
            cin>>s>>e>>c;
            G[s][e]+=c;
        }
        cout<<Dinic()<<endl;
    }
    return 0;
}
时间: 2024-11-05 15:55:24

poj 1273 裸 网络流 (dinic)的相关文章

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

POJ 1273 最大流 Dinic

Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56802 Accepted: 21824 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 wate

Drainage Ditches - poj 1273(网络流模板)

题意:1是源点,m是汇点,求出来最大流量,没什么好说的就是练习最大流的模板题 ************************************************************** 先用Edmonds-Karp的算法做一下试试吧 重边贡献了 1W,要加上所有的重边才算是两点间最大流量 ************************************************************************* /*********************

[POJ 1273]Drainage Ditches(Edmond-Krap算法和Dinic算法求最大流)

自NOIP 2014结束之后将近一个星期没撸题了,现在开始搞省选,发个水水的裸网络流题解吧. 题目链接:http://poj.org/problem?id=1273 裸网络流,模板题. 1.Edmond_Karp算法 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <que

POJ 1273 Drainage Ditches(网络流dinic算法模板)

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

POJ 1273 Drainage Ditches (网络流Dinic模板)

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

POJ 1273 Drainage Ditches(网络流 最大流)

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

POJ 2135 Farm Tour (dinic算法,网络流)

构图方法: 注意题目中的边为无向边.新建源点s 和 汇点t 每两条道路连一条容量为1,费用为w的边.s到1连一条容量为1,费用为0 的边,n到 t 连一条容量为1,费用为0 的边,求最大流. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include

POJ 1273 Drainage Ditches 网络流基础

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