POJ1273 Drainage Ditches 最大流模板题(dinic)

最大流的模板题

给出边数M,顶点数N 以及每条边的容量 求1到N的最大流

注意可以有重边

邻接矩阵模板:

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxx 0x3f3f3f
#define M   205
using namespace std;
int arc[M][M];   //弧的剩余流量
int level[M];
int n;
int min(int a,int b)
{
    return a<b?a:b;
}
int bfs()
{
    int q[M];
    int head=0,tail=0,v;
    for(int i=1; i<=n; i++)
        level[i]=-1;
    level[1]=0;
    q[tail++]=1;
    while(head<tail)
    {
        v=q[head++];
        for(int i=1; i<=n; i++)
            if(level[i]==-1&&arc[v][i])
            {
                level[i]=level[v]+1;
                q[tail++]=i;
            }
    }
    return level[n]!=-1;
}

int dfs(int loc,int flow)
{
    int unused=flow;
    if(loc==n||flow==0)
        return flow;
    for(int i=1; i<=n; i++)
        if(arc[loc][i]&&level[loc]+1==level[i])
        {
            int used=dfs(i,min(unused,arc[loc][i]));
            arc[loc][i]-=used;
            arc[i][loc]+=used;
            unused-=used;
        }
    return flow-unused;
}

int main()
{
    int m;
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=i; j++)
                arc[i][j]=arc[j][i]=0;
        int a,b,f;
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&f);
            arc[a][b]+=f;
        }
        int ans=0;
        while(bfs())
            ans+=dfs(1,maxx);

        cout<<ans<<endl;
    }
    return 0;
}

邻接表模板:

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxx 0x3f3f3f
#define M   205
using namespace std;
struct arc{
    int to,value;
    int pre;
}edge[45555];
int level[M];
int table[M];
int n;
int sum;
int min(int a,int b)
{
    return a<b?a:b;
}
void addedge(int a,int b,int v)
{
    edge[sum]={b,v,table[a]};
    table[a]=sum++;
    edge[sum]={a,0,table[b]};
    table[b]=sum++;
    return;
}
int bfs()
{
    int q[M];
    int head=0,tail=0,v;
    for(int i=1; i<=n; i++)
        level[i]=-1;
    level[1]=0;
    q[tail++]=1;
    while(head<tail)
    {
        v=q[head++];
        for(int i=table[v]; i!=-1; i=edge[i].pre)
            if(level[edge[i].to]==-1&&edge[i].value)
            {
                level[edge[i].to]=level[v]+1;
                q[tail++]=edge[i].to;
            }
    }
    return level[n]!=-1;
}
int ss=0;
int dfs(int loc,int flow)
{
    int unused=flow;
    if(loc==n||flow==0)
        return flow;
    for(int i=table[loc]; i!=-1; i=edge[i].pre)
        if(edge[i].value&&level[loc]+1==level[edge[i].to])
        {
            int used=dfs(edge[i].to,min(unused,edge[i].value));
            edge[i].value-=used;
            edge[i^1].value+=used; //正向边反向边的的序号的奇偶性不同且差一
            unused-=used;
        }
    return flow-unused;
}

int main()
{
    int m;
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=1;i<=n;i++)
            table[i]=-1;
        sum=0;
        int a,b,f;
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&f);
            addedge(a,b,f);
        }
        int ans=0;
        while(bfs())
            ans+=dfs(1,maxx);

        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-08-03 06:50:28

POJ1273 Drainage Ditches 最大流模板题(dinic)的相关文章

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

http://poj.org/problem?id=1273 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 Jo

HDU 1532Drainage Ditches(最大流模板题 ISAP)

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

poj 1273 Drainage Ditches 最大流入门题

题目链接:http://poj.org/problem?id=1273 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

POJ 1273 Drainage Ditches | 最大流模板

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 #define N 210 6 #define INF 1234567897 7 using namespace std; 8 int cnt=2,head[N],n,m,ans,lev[N]; 9 queue <int> q; 10 struct edge 11 { 12 int u,

poj1273 Drainage Ditches(裸最大流)

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Every time it rains on Farmer Joh

Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... 1 #include<cstdio> 2 #include<vector> 3 #include<queue> 4 #include<cstring> 5 #include<set> 6 #include<algorithm> 7 #define CLR(a,b) memset((a),(b),si

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input

[ACM] hdu 3549 Flow Problem (最大流模板题)

Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input contains an integer T, denoting the nu