poj1459 Power Network --- 最大流 EK/dinic

求从电站->调度站->消费者的最大流,给出一些边上的容量,和电站和消费者可以输入和输出的最大量。

添加一个超级源点和汇点,建边跑模板就可以了。两个模板逗可以。

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll __int64
const int maxn=110;
using namespace std;

int n,m,np,nc,s,t;
int level[maxn],c[maxn][maxn];

bool makelevel()
{
    memset(level,0,sizeof level);
    level[s]=1;
    int q[maxn];
    int fro=0,iq=0;
    q[iq++]=s;
    int i,v;
    while(fro!=iq)
    {
        v=q[fro++];
        for(i=1;i<=n+2;i++)//注意点的编号
        {
            if(!level[i]&&c[v][i])
            {
                level[i]=level[v]+1;
                q[iq++]=i;
            }
        }
    }
    if(!level[t]) return 0;
    return 1;
}

int dfs(int now,int maxf)
{
    if(now==t) return maxf;
    int ret=0;
    for(int i=1;maxf&&i<=n+2;i++)//注意点的编号
    {
        if(c[now][i]&&level[now]+1==level[i])
        {
            int tmp=dfs(i,min(maxf,c[now][i]));
            c[now][i]-=tmp;
            c[i][now]+=tmp;
            ret+=tmp;
            maxf-=tmp;
        }
    }
    return ret;
}

int dinic()
{
    int ans=0;
    while(makelevel()) ans+=dfs(s,inf);
    return ans;
}

/*
int c[maxn][maxn],p[maxn];

bool bfs()
{
    queue<int> q;
    bool vis[maxn];
    memset(vis,0,sizeof vis);
    memset(p,-1,sizeof p);
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        int e=q.front();
        if(e==t) return 1;
        q.pop();
        for(int i=1;i<=n+2;i++)//注意点的范围
        {
            if(c[e][i]&&!vis[i])
            {
                vis[i]=1;
                p[i]=e;
                q.push(i);
            }
        }
    }
    return 0;
}

int ek()
{
    int u,ans=0,mn;
    while(bfs())
    {
        mn=inf;
        u=t;
        while(p[u]!=-1)
        {
            mn=min(mn,c[p[u]][u]);
            u=p[u];
        }
        ans+=mn;
        u=t;
        while(p[u]!=-1)
        {
            c[p[u]][u]-=mn;
            c[u][p[u]]+=mn;
            u=p[u];
        }
    }
    return ans;
}*/

int main()
{
    int i,a,b,cc;
    while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
    {
        s=n+1,t=n+2;
        memset(c,0,sizeof c);
        for(i=0;i<m;i++)
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&a,&b,&cc);
            c[a+1][b+1]=cc;
        }
        for(i=0;i<np;i++)
        {
            while(getchar()!='(');
            scanf("%d)%d",&a,&b);
            c[s][a+1]=b;
        }
        for(i=0;i<nc;i++)
        {
            while(getchar()!='(');
            scanf("%d)%d",&a,&b);
            c[a+1][t]=b;
        }
        printf("%d\n",dinic());
    }
    return 0;
}

poj1459 Power Network --- 最大流 EK/dinic

时间: 2024-08-01 23:24:23

poj1459 Power Network --- 最大流 EK/dinic的相关文章

Poj1459 Power Network 预流推进

Poj1459 Power Network 预流推进 问题描述: 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 <= p(u) <= p ma

POJ-1459 Power Network(最大流模板)

题目链接:POJ-1459 Power Network 题意 有$np$个发电站,$nc$个消费者,$m$条有向边,给出每个发电站的产能上限,每个消费者的需求上限,每条边的容量上限,问最大流量. 思路 很裸的最大流问题,源点向发电站连边,边权是产能上限,消费者向汇点连边,边权是需求上限,其余的连边按给出的$m$条边加上去即可. 代码实现 #include <iostream> #include <cstdio> #include <cstring> #include &

Power Network(最大流基础_增广路算法:多源多汇,自建总源点和总汇点)

 Power NetworkCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A power network consists of nodes (power stations, consumers and dispatchers) connected by p

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

POJ1459 Power Network(网络最大流)

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

poj1459 Power Network

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

POJ1459 Power Network【最大流】【Edmond-Karp】

第一道网络流题,纪念下~~~ 题目链接: http://poj.org/problem?id=1459 题目大意: 一个电力网络包含很多节点(发电站.消费者以及中转站)和电力传输线.所有发电站不消耗电力, 所有消费者不产生电力,所有中转站不产生也不消耗电力.在网络中,任意两点u和v之间最多只 有一条传输线的存在,且能够从u望v传输最多w单位容量.计算整个网络的最大电力消耗. 思路: 一道非常基础.非常典型的网络流题目.每个发电站当做一个源点,每个消费者当做一个汇点.但 是这样子并不适合任何一种求

POJ1459:Power Network(dinic)

题目链接:http://poj.org/problem?id=1459 题意:有n个结点,np个发电站,nc个消费者,m个电力运输线.接下去是m条边的信息(u,v)cost,cost表示边(u,v)的最大流量:a个发电站的信息(u)cost,cost表示发电站u能提供的最大流量:b个用户的信息(v)cost,cost表示每个用户v能接受的最大流量. 思路:在图中添加1个源点S和汇点T,将S和每个发电站相连,边的权值是发电站能提供的最大流量:将每个用户和T相连,边的权值是每个用户能接受的最大流量.

《网络流学习笔记03&amp;&amp;POJ1459 Power Network》

题目链接:click here 题意:一个电力网络有n个点,有np个发电站,nc个消耗点,其余的为中转站.m条电缆,中转站既不发电也不耗电.每条电缆都有一个最大容量. 思路:设置一个超级源点和一个超级汇点,将所有的源点和汇点分别放进去,Dinic 算法实现. 注意括号的处理. 代码: #include <math.h> #include <queue> #include <deque> #include <vector> #include <stack