poj1459

链接:点击打开链接

题意:输入n,np,nc,m,分别代表节点个数,发电站个数,消耗站个数,电线个数,依次按要求输入问消耗站最多能获得的电量是多少

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int s[505][505],dis[505],q[2005];
int n,np,nc,m;
int bfs(){
    int i,u,l,h;
    memset(dis,-1,sizeof(dis));
    dis[0]=0;q[0]=1;
    l=0;h=1;
    while(l<h){
        u=q[++l];
        for(i=0;i<=n+1;i++)
        if(s[u][i]&&dis[i]==-1){
        dis[i]=dis[u]+1;
        q[++h]=i;
        }
    }
    if(dis[n+1]!=-1)
    return 1;
    return 0;
}
int dinic(int x,int sum){
    int i,a;
    if(x==n+1)
    return sum;
    for(i=0;i<=n+1;i++)
    if(s[x][i]>0&&(dis[i]==dis[x]+1)&&(a=dinic(i,min(sum,s[x][i])))){
        s[x][i]-=a;
        s[i][x]+=a;
        return a;
    }
    dis[x]=-1;
    return 0;
}                                       //dinic算法模板
int main(){
    int u,v,w,ans,temp;
    while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF){
        memset(s,0,sizeof(s));
        while(m--){
            while(getchar()!=‘(‘);
            scanf("%d,%d)%d",&u,&v,&w);
            s[u+1][v+1]+=w;
        }
        while(np--){                    //将0和n+1分别设为源点和汇点
            while(getchar()!=‘(‘);
            scanf("%d)%d",&u,&v);
            s[0][u+1]+=v;
        }
        while(nc--){
            while(getchar()!=‘(‘);
            scanf("%d)%d",&u,&v);
            s[u+1][n+1]+=v;
        }
        ans=0;
        while(bfs()){
            while(temp=dinic(0,99999999))
            ans+=temp;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-12-07 02:17:54

poj1459的相关文章

poj1459(Power Network)

题目地址:Power Network 题目大意: 输入分别为m个点,a个发电站,b个用户,n条边:接下去是n条边的信息(u,v)cost,cost表示边(u,v)的最大流量:a个发电站的信息(u)cost,cost表示发电站u能提供的最大流量:b个用户的信息(v)cost,cost表示每个用户v能接受的最大流量. 求发电站流向用户的最大流量. 解题思路: 最大流问题 ,首先建图,我让m+1这个节点为源点S,让m这个点为汇点T.求流入T的最大流即可. 代码: 1 #include <algorit

网络流-最大流:两枚[poj1459&amp;poj3436]

说说建图吧- poj1459: 增加超级源点,超级汇点,跑一遍即可. #include <cstdio> #include <cstring> #include <vector> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> using namespace std; const int MAX = 107; const i

poj1459最大流

#include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <qu

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

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

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 &

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

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

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

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

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

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