poj1459最大流

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
int n,p,c,m;
const int maxn=1111;
int Map[maxn][maxn];
int s;int e;
int level[maxn];
const int INF=0xfffffff;
int Min(int a,int b)
{
    return a>b?b:a;
}
int bfs()
{
    memset(level,0,sizeof(level));
    queue<int > q;
    q.push(s);
    level[s]=1;
    while(!q.empty()){
        int cur=q.front() ;q.pop();
        for(int i=0;i<n+2;i++){
            if(Map[cur][i]&&!level[i]){
                level[i]=level[cur]+1;
                q.push(i);
            }
        }
    }
    return level[e];
}

int dfs(int x,int val)
{
    int tem=val;
    if(x==e) return val;
    for(int i=0;i<n+2&&tem;i++){
        if(Map[x][i]&&level[i]==level[x]+1){
            int t=dfs(i,Min(tem,Map[x][i]));
            Map[x][i]-=t;Map[i][x]+=t;
            tem-=t;
        }
    }
    return val-tem;
}

int  dinic()
{
    int t;int ans=0;
    while(bfs()){
        while(t=dfs(s,INF)){
            ans+=t;
        }
    }
    return ans;
}

int main()
{
    char str[1000];
    while(scanf("%d%d%d%d",&n,&p,&c,&m)!=EOF){
        s=n;e=n+1;
        for(int i=0;i<n+2;i++)
            for(int j=0;j<n+2;j++)
            Map[i][j]=0;
        for(int i=0;i<m;i++){
            int a;int b;int d;
            while(getchar()!=‘(‘);
            scanf("%d,%d)%d",&a,&b,&d);
            Map[a][b]=d;
        }
        for(int i=0;i<p;i++){
            int a;int b;
            while(getchar()!=‘(‘);
            scanf("%d)%d",&a,&b);
            Map[s][a]=b;
        }
        for(int i=0;i<c;i++){
            int a;int b;
            while(getchar()!=‘(‘) ;
            scanf("%d)%d",&a,&b);
            Map[a][e]=b;
        }
        printf("%d\n",dinic());
    }
    return 0;
}

poj1459最大流

时间: 2024-08-28 17:28:46

poj1459最大流的相关文章

poj1459 最大流Dinic

比较简单. #include<stdio.h> #include<string.h> #include<queue> #define maxn 110 #define INF 99999999 using namespace std; int vis[maxn],n,map[maxn][maxn]; int min(int x,int y) {return x<y?x:y;} int dfs(int u,int low) { int i,a; if(u==n+1)

网络流-最大流:两枚[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 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

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: 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 题意:在一个网络图中有n个点,其中有np个发电站,nc个用户,m条电线;每个发电站,用户,和电线都对应有一个最大的电流;让求出该网络中最大的电流. 分析:最大流裸题,增加一个源点0和汇点n+1后直接跑最大流即可. #pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <str

poj1459,网络流,最大流,多源点多汇点

题意: 给几个发电站,给几个消耗站,再给几个转发点. 发电站只发电,消耗站只消耗电,转发点只是转发电,再给各个传送线的传电能力. 问你消耗站能获得的最多电是多少. 思路:增加一个超级源点,和超级汇点..把所给的发电站都和超级源点相连,把所给的消耗战都和超级汇点相连..用EK求最大流. 模板有几个地方要注意. 1:start是编号最前的点,last是编号最后的点 模板默认last是m,根据需要要把m都改成编号最后的点的号码 #include<stdio.h> #include<iostre

poj-1459(网络流-最大流)

题意:给你n个点的电网系统,有一些点是电站,能提供p的电能,有些点是用户,能消耗c的电能,有些是过渡站,不消耗不产生(等于没用),然后m条电线(x,y,w),代表x可以向y运输w的电能,问你这个电网系统最多消耗多少电能 解题思路:题目好乱...其实就是一个最大流的板子题,电站提供的作为初始流量,用户消耗的和电线运输的代表容量,然后建立一个源点,汇点跑最大流就可以了:也就输入看的烦人: #include<iostream> #include<algorithm> #include&l