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相连,边的权值是每个用户能接受的最大流量。从而转化成了一般的最大网络流问题,然后求解。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define N 1100
#define inf 0x3f3f3f3f
typedef int ll;
using namespace std;
ll n,np,nc,m,tt,dis[N],head[N];
struct node
{
    ll x,y,w,next;
} eg[N*N];
void init()
{
    tt=0;
    memset(head,-1,sizeof(head));
}
void add(int xx,int yy,int ww)
{
    eg[tt].x=xx;
    eg[tt].y=yy;
    eg[tt].w=ww;
    eg[tt].next=head[xx];
    head[xx]=tt++;
    eg[tt].x=yy;
    eg[tt].y=xx;
    eg[tt].w=0;
    eg[tt].next=head[yy];
    head[yy]=tt++;
}
bool bfs(int s,int e)
{
    memset(dis,-1,sizeof(dis));
    dis[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int fa=q.front();
        q.pop();
        for(int i=head[fa]; i!=-1; i=eg[i].next)
        {
            int v=eg[i].y;
            if(dis[v]==-1&&eg[i].w)
            {
                dis[v]=dis[fa]+1;
                q.push(v);
            }
        }
    }
    if(dis[e]>0)
        return true;
    return false;
}
int dinic(int s,int maxt)
{
    if(s==n+1) return maxt;
    int a,sum=maxt;
    for(int i=head[s]; i!=-1; i=eg[i].next)
    {
        int v=eg[i].y;
        if(dis[v]==dis[s]+1&&eg[i].w>0)
        {
            a=dinic(v,min(sum,eg[i].w));
            eg[i].w-=a;
            eg[i+1].w+=a;
            sum-=a;
        }
    }
    return maxt-sum;
}
int main()
{
    ll xx,yy,ww;
    while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
    {
        init();
        while(m--)
        {
            while(getchar()!=‘(‘) ;
            scanf("%d,%d)%d",&xx,&yy,&ww);
            add(xx+1,yy+1,ww);
        }
        for(int i=0; i<np; i++)
        {
            while(getchar()!=‘(‘) ;
            scanf("%d)%d",&xx,&yy);
            add(0,xx+1,yy);
        }
        for(int i=0; i<nc; i++)
        {
            while(getchar()!=‘(‘) ;
            scanf("%d)%d",&xx,&yy);
            add(xx+1,n+1,yy);
        }
        ll ans=0;
        while(bfs(0,n+1))
        {
            ans+=dinic(0,inf);
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-11 21:20:33

POJ1459:Power Network(dinic)的相关文章

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

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 --- 最大流 EK/dinic

求从电站->调度站->消费者的最大流,给出一些边上的容量,和电站和消费者可以输入和输出的最大量. 添加一个超级源点和汇点,建边跑模板就可以了.两个模板逗可以. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector&

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

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

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(多源点/汇点最大流问题)

题目链接: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

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

Power Network(网络流最大流)

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

POJ1459Power Network(dinic模板)

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