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

题目链接:http://poj.org/problem?id=1459

题目给你一大段解释,其实就是废话。还给了一张解释图,其实就是误导。

题目大意:对于一个电力网来说,既有发电站,也有用电方,还有输电线路。其中发电站是有限度的,用电方也是有限度的,输电线更是有限度的,所以明显一个网络流问题。先给出线路和限度,再给出用电方,最后后出发电站。

因为是多源点(多个发电站),多汇点(多个用电方),所以需要超级源处理。

多为超级源,就是假设有一个源,连向所有的源点(发电站),其线路容量就是发电站的限度,那么就可以把发电站当做普通点处理。再假设一个超级汇点,那么就可以把所有汇点(用电方)连向这个超级汇点,其线路容量是用电方限度,那么,就变成一个单纯的单源点,单汇点的最大流问题。用dinic便可以解决。

#include <iostream>
#include<cstdio>
#include <cstdlib>
#include <cstring>
#include<queue>
#include<algorithm>
#define MAX 999999

using namespace std;

int map_[200][200];
int dis[200];
int bfs(int s,int t)
{
    int now;
    memset(dis,-1,sizeof (dis));
    dis[s] = 0;
    queue<int> que;
    que.push(s);

    while(!que.empty())
    {
        now = que.front();
        que.pop();

        for (int i = 0;i <= t;i++)
            if (dis[i] == -1 && map_[now][i] > 0)
            {
                dis[i] = dis[now] + 1;

                que.push(i);
            }
    }

    if (dis[t] != -1)
        return 1;

    return 0;
}

int dinic(int s,int t,int x)
{
    if (s == t)
        return x;

    int tmp = x;
    for (int i = 0;i <= t;i++)
    {
        if (dis[i] == dis[s] + 1 && map_[s][i] > 0)
        {
            int imin = dinic(i,t,min(map_[s][i],x));

            map_[s][i] -= imin;
            map_[i][s] += imin;
            x -= imin;
        }
    }

    return tmp - x;
}

int main()
{
    int n,np,nc,m;

    while (~scanf ("%d%d%d%d",&n,&np,&nc,&m))
    {
        int i,k;
        int u,v,c;
        memset(map_,0,sizeof(map_));
        for (i = 0;i < m;i++)
        {
            scanf(" (%d,%d)%d",&u,&v,&c);
            map_[u + 1][v + 1] += c;    //0是超级源点,其他点后移
        }

         for(i = 0;i < np;i++)
        {
            scanf(" (%d)%d",&v,&c);
            map_[0][v + 1] += c;
        }

        for(i = 0;i < nc;i++)
        {
            scanf(" (%d)%d",&u,&c);
            map_[u + 1][n + 1] += c;
        }

        int ans = 0;

        while (bfs(0,n + 1))
            ans += dinic(0,n + 1,MAX);

        printf ("%d\n",ans);
    }
    return 0;
}

不忘初心,方得始终

时间: 2024-10-28 15:41:13

POJ 1459 Power Network (多源点/汇点最大流问题)的相关文章

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, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点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

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 最大流

建模不难,就读入有点麻烦,无脑拍完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 经典网络流构图问题 最大流,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(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

怒搞一下午网络流,又去我一块心病. 从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 题目: 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 (网络流最大流基础 多源点多汇点 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【建立超级源点,超级汇点】

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