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 = 100000000;
struct Edge{
    int from, to, cap, flow;
};

struct Dinic{
    int n, m, s, t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];

    void init(int n)
    {
        this->n = n;
        for(int i=0; i<=n; ++i) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from, int to, int cap)
    {
        edges.push_back((Edge){from, to, cap, 0});
        edges.push_back((Edge){to, from, 0, 0});
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        memset(vis, 0, sizeof vis );
        queue<int> Q;
        Q.push(s);
        vis[s] = 1;
        d[s] = 0;
        while(!Q.empty())
        {
            int x = Q.front(); Q.pop();
            for(int i=0; i<G[x].size(); ++i)
            {
                Edge& e = edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow)
                {
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x, int a)
    {
        if(x == t || a == 0) return a;
        int flow = 0, f;
        for(int& i = cur[x]; i<G[x].size(); ++i)
        {
            Edge& e = edges[G[x][i]];
            if(d[x] + 1 == d[e.to] && (f=DFS(e.to, min(a,e.cap-e.flow)))>0)
            {
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                a -= f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int Maxflow(int s, int t)
    {
        this->s = s; this->t =t;
        int flow = 0;
        while(BFS()){
            memset(cur, 0, sizeof cur );
            flow += DFS(s, INF);
        }
        return flow;
    }
};

Dinic solver;

int main()
{
    int n, np, nc, m;
    char ch;
    int x, y, z;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m))
    {
        int s = 0, t = n+1;
        solver.init(t+2);
        for(int i=1; i<=m; ++i)
        {
            cin>>ch>>x>>ch>>y>>ch>>z;
            if(x==y) continue;
            solver.AddEdge(x+1, y+1, z);
        }
        for(int i=1; i<=np; ++i)
        {
            cin>>ch>>x>>ch>>z;
            solver.AddEdge(s, x+1, z);
        }
        for(int i=1; i<=nc; ++i)
        {
            cin>>ch>>x>>ch>>z;
            solver.AddEdge(x+1, t, z);
        }
        int ans = solver.Maxflow(s,t);
        printf("%d\n", ans);
    }
    return 0;
}

poj 1459 Power Network, 最大流,多源多汇,布布扣,bubuko.com

时间: 2024-10-14 18:16:22

poj 1459 Power Network, 最大流,多源多汇的相关文章

POJ 1459 Power Network 最大流

建模不难,就读入有点麻烦,无脑拍完dinic 1A happy- #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

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

POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459 注意输入格式就行,还是ISAP #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 300; const int ma

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 1459 Power Network (dinic)

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

初涉网络流 POJ 1459 Power Network

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

poj 1459 Power Network 多源多汇网络流

题目链接:http://poj.org/problem?id=1459 题意: 总共有N个点,M条边,有np个点是加油站即源点,有nc个点是消耗点,即汇点. 给出M条边的容量,每个源点最大的流出量和每个汇点的最大流入量. 求最多可以消耗多少. 思路: 多源多汇网络流就是增加一个超级源点和超级汇点. 把超级源点向每个源点连一条边,容量为该源点流出的最大量. 把每个汇点向超级汇点连一条边,容量为该汇点规定的最大流入量. 然后求最大流. 1 //#include <bits/stdc++.h> 2

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

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

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