hdu 1532 Drainage Ditches 增广路 ford

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=1024;
const int inf=1<<24;
int abs(int x)
{
    if(x<0) return -x;
    else return x;
}

struct
{
    int c,f;
} g[N][N];

int n,m,u,v,c,f,i,j,ans;
int flag[N],p[N],a[N];
queue<int>q;
void ford()
{
    while(1)
    {
        memset(flag,0xff,sizeof(flag));
        memset(p,0xff,sizeof(p));
        memset(a,0xff,sizeof(a));
        a[0]=inf;
        p[0]=0;
        flag[0]=0;
        while(!q.empty()) q.pop();
        q.push(0);
        while(!q.empty()&&flag[n-1]==-1)
        {
            u=q.front();
            q.pop();
            for(i=0; i<n; i++)
            {
                if(flag[i]==-1)
                {
                    if(g[u][i].c<inf&&g[u][i].f<g[u][i].c)
                    {
                        flag[i]=0;
                        p[i]=u;
                        a[i]=min(a[u],g[u][i].c-g[u][i].f);
                        q.push(i);
                    }
                    else if(g[i][u].c<inf&&g[i][u].f>0)
                    {
                        flag[i]=0;
                        p[i]=-u;
                        a[i]=min(a[u],g[i][u].f);
                        q.push(i);
                    }
                }
            }
            flag[u]=1;
        }

        if(a[n-1]==0||flag[n-1]==-1) break;
        int k1=n-1,k2=abs(p[k1]);
        int add=a[n-1];
        //printf("%d\n",add);
        while(1)
        {
            if(g[k2][k1].f<inf)
                g[k2][k1].f+=add;
            else
                g[k1][k2].f-=add;
            if(k2==0) break;
            k1=k2;
            k2=abs(p[k2]);
        }
    }
    ans=0;
    for(i=0; i<n; i++)
        if(g[0][i].f<inf)
            ans+=g[0][i].f;

    printf("%d\n",ans);
}
int main()
{
    while(~scanf("%d%d",&m,&n))
    {
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
                g[i][j].c=g[i][j].f=inf;
        for(i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            u--;
            v--;
            if(g[u][v].c==inf)
                g[u][v].c=c;
            else
                g[u][v].c+=c;
            g[u][v].f=0;
        }
        ford();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-14 05:51:53

hdu 1532 Drainage Ditches 增广路 ford的相关文章

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 Drainage Ditches

http://acm.hdu.edu.cn/showproblem.php?pid=1532 基础题. 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 int n, m, flow; 9 int vis[205]; 10 //路径记录 11 i

poj 1149 pigs 增广路 ford

题目关键是建模,之后就是简单地增广路. #include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #include<queue> using namespace std; const int N=1024; const int inf=1<<24; struct { int c,f; }g[1024][1024]; int n,m,s,t; qu

HDU 1532 Drainage Ditches EK算法

题意:输入m n, m是边数,n是点数. 接下来m行: 起点,终点,容量.求以 1 为源点, n为汇点的最大流. #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0xfffffff; const int MAXN = 200 + 10; //邻接矩阵存放图. int flow[MAXN][

HDU 1532 Drainage Ditches 排水渠(网络流,最大流,入门)

题意:给出一个有向图,以及边上的容量上限,求最大流.(有重边,要将容量上限叠加) 思路:用最简单的EK+BFS解决.每次搜到一条到达终点的路径,就立刻退出,更新ans,然后再回头修改图中的当前flow状况(这就得靠记录路径了).当当前图没有到达终点的路径图,流已经饱和,可以结束程序了. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define INF 0x7f7f7

HDU - 1532 - Drainage Ditches &amp;&amp; 3549 - Flow Problem (网络流初步)

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

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches 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 drai

HDU 1532 Drainage Ditches (网络流)

A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

HDU 1532 Drainage Ditches (最大网络流)

Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Every time it rains on