hdu 3549 Flow Problem 增广路ford-fullkerson算法

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1024*2;
const int inf=1<<24;

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

int n,m,i,j,u,v,c,f;

int abs(int x)
{
    if(x<0) return -x;
    else return x;
}

void ford()
{
    int p[N],a[N],flag[N];
    queue<int>q;
    while(1)
    {
        memset(p,0xff,sizeof(p));
        memset(a,0xff,sizeof(a));
        memset(flag,0xff,sizeof(flag));
        while(!q.empty()) q.pop();
        q.push(0);
        flag[0]=0;
        p[0]=0;
        a[0]=inf;
        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;
        }
        //printf("%d %d\n",flag[n-1],a[n-1]);
        if(flag[n-1]==-1||a[n-1]==0) break;
        int k1=n-1,k2=abs(p[k1]);
        int add=a[n-1];
        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]);
        }
    }

    int flow=0;
    for(i=0; i<n; i++)
        if(g[0][i].f<inf)
            flow+=g[0][i].f;

    printf("%d\n",flow);
}

int main()
{
    int _;
    scanf("%d",&_);
    for(int k=1; k<=_; k++)
    {
        scanf("%d%d",&n,&m);
        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;
        }
        printf("Case %d: ",k);
        ford();
    }
    return 0;
}

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

时间: 2024-10-12 07:49:28

hdu 3549 Flow Problem 增广路ford-fullkerson算法的相关文章

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

网络流 HDU 3549 Flow Problem

网络流 HDU 3549 Flow Problem 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法进行求解,注意的问题有两个: 1. 每次增广的时候,反向流量也要进行更行,一开始没注意,WA了几次 ORZ 2. 对于输入的数据,容量要进行累加更新. // 邻接矩阵存储 #include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const i

hdu 3549 Flow Problem (网络最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6674    Accepted Submission(s): 3112 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, yo

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

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

hdu 3549 Flow Problem (最大流入门题)

增广路: 1 /************************************************************* 2 题目: Flow Problem(HDU 3549) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 4 题意: 给一个单向图,求从1到n的最大流 5 算法: 最大流之增广路(入门) 6 算法思想: 不断用BFS找通路,没每找一条路,记录这条路的最小流, 7 再给这条路上的所有流量减去这个最小值.

HDU 3549 Flow Problem(最大流模板)

http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 int n, m,

hdu 3549 Flow Problem

Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp算法即可:下面是对增广路的一些理解和代码的解释: 残量:容量-流量: 增广:求出从源点到汇点的一条道路中所有残量的最小值d,把对应的所有边上的流量增加d,反向边(t->s)流量减少d(反向边的cap其实一直是0,只是flow为负了); 技巧:这次的ins的标号是从0开始的,即tot++,之前我都是++t