POJ 1459 EK算法

题意:

2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条;(0,1)20 (1,0)10 代表(起点,终点)最大传递的能量

(0)15 (产生能量的点)产生的最大能量(1)20 (消费能量的点)消费的最大能量

初学网络流,我想从基础练起;就先用EK算法写一遍

这道题看似很难,但其实只要加一个源点以及汇点,让所有的产生能量的点指向源点,让所有的消费能量的点指向汇点;
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 110
#define INF 0x3f3f3f3f
#define Min(a, b) a<b?a:b
int G[N][N], pre[N];

int EK(int s, int e);
bool BFS(int s, int e);

int main()
{
    int point, source, dest, edge;
    while(scanf("%d%d%d%d", &point, &source, &dest, &edge)!=EOF)
    {
        int a, b, flow, in, inflow, out, outflow;
        char ch;
        memset(G, 0, sizeof(G));
        for(int i=0; i<edge; i++)
        {
            //scanf("(%d,%d)%d", &a, &b, &flow);
            cin>>ch>>a>>ch>>b>>ch>>flow;
            G[a+1][b+1]+=flow;
        }

        for(int i=0; i<source; i++)
        {
            //scanf("(%d)%d", &out, &outflow);
            cin>>ch>>out>>ch>>outflow;
            G[0][out+1]+=outflow;
        }

        for(int i=0; i<dest; i++)
        {
            //scanf("(%d)%d", &in, &inflow);
            cin>>ch>>in>>ch>>inflow;
            G[in+1][point+1]+=inflow;
        }

        int ans=EK(0, point+1);
        printf("%d\n", ans);

    }
    return 0;
}

int EK(int s, int e)
{
    int maxflow=0;

    while(BFS(s, e))
    {
        int minflow=INF;

        for(int i=e; i!=s; i=pre[i])
            minflow=Min(minflow, G[pre[i]][i]);

        for(int j=e; j!=s; j=pre[j])
        {
            G[pre[j]][j]-=minflow;
            G[j][pre[j]]+=minflow;
        }
        maxflow+=minflow;
    }

    return maxflow;
}

bool BFS(int s, int e)
{
    memset(pre, -1, sizeof(pre));

    queue<int>Q;
    Q.push(s);

    while(Q.size())
    {
        int i=Q.front(); Q.pop();

        if(i==e)
            return true;

        for(int j=0; j<=e; j++)
        {
            if(G[i][j]&&pre[j]==-1)
            {
                pre[j]=i;
                Q.push(j);
            }
        }
    }
    return false;
}
 
时间: 2024-07-29 23:06:35

POJ 1459 EK算法的相关文章

POJ 1459(EK)

这题是学着小媛学姐写的.. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<queue> 5 #include <climits> 6 using namespace std; 7 #define N 120 8 9 int n, np, nc, m; 10 int cap[N][N]; 11 int EK(int s, int t) 12 { 13 q

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line

POJ 1273 Drainage Ditches(我的EK算法模板)

题意:给你n条边,目标位置t:接下来是每条边,包括起点,终点,容量: 感想:第一道最大流的代码,这道题是我更深地理解了Ek算法,不过这道题有个超坑的情况,那就是出现重边的情况==! 思路:EK算法 AC代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 #define N

POJ 1459 &amp; ZOJ 1734 Power Network (网络最大流)

http://poj.org/problem?id=1459 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22674   Accepted: 11880 Description A power network consists of nodes (power s

POJ 1459 Power Network(多源点/汇点最大流问题)

题目链接:http://poj.org/problem?id=1459 题目: Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0

初涉网络流 POJ 1459 Power Network

怒搞一下午网络流,又去我一块心病. 从2F到SAP再到Dinic终于过掉了.可是书上说Dinic的时间复杂度为v*v*e.感觉也应该超时的啊,可是过掉了,好诡异. 后两种算法都是在第一种的基础上进行优化.第一种方法就是不停的寻找增广路,后两种引进了层次网络的概念,第三种又改善了寻找增广路的方法. 现在只能理解到这里了... #include <algorithm> #include <iostream> #include <cstring> #include <c

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

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

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100