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

题意:给你n个点的电网系统,有一些点是电站,能提供p的电能,有些点是用户,能消耗c的电能,有些是过渡站,不消耗不产生(等于没用),然后m条电线(x,y,w),代表x可以向y运输w的电能,问你这个电网系统最多消耗多少电能

解题思路:题目好乱。。。其实就是一个最大流的板子题,电站提供的作为初始流量,用户消耗的和电线运输的代表容量,然后建立一个源点,汇点跑最大流就可以了;也就输入看的烦人;

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#define inf 0x3f3f3f3f
#define maxn 100500
using namespace std;
struct Edge
{
    int fa;
    int next;
    int to;
    int w;
}edge[maxn];
int n,m;
int head[maxn];
int cnt,Start,End;
int depth[maxn];
void add(int u,int v,int w)
{
    edge[cnt].next=head[u];edge[cnt].fa=u;
    edge[cnt].to=v;edge[cnt].w=w;head[u]=cnt++;
    edge[cnt].next=head[v];edge[cnt].fa=v;
    edge[cnt].to=u;edge[cnt].w=0;head[v]=cnt++;
}
bool bfs()//分层;
{
    memset(depth,0,sizeof(depth));
    queue<int>q;
    q.push(Start);
    depth[Start]=1;
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=head[temp];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(depth[v]||edge[i].w<=0)
                continue;
            depth[v]=depth[temp]+1;
            q.push(v);
        }
    }
    return depth[End];//若为0表示没法到达也就是没有路径了;
}
int dfs(int u,int maxflow)
{
    if(u==End)
        return maxflow;
    int add=0;
    for(int i=head[u];i!=-1&&add<maxflow;i=edge[i].next)
    {
        int v=edge[i].to;
        if(depth[v]!=depth[u]+1)
            continue;
        if(edge[i].w==0)
            continue;
        int tempflow=dfs(v,min(edge[i].w,maxflow-add));
        edge[i].w-=tempflow;
        edge[i^1].w+=tempflow;
        add+=tempflow;
    }
    return add;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(Start,0x3f3f3f3f);
    }
    return ans;
}
int main()
{
    int x,y,w;
    int np,nc;

    char s[20];
    while(cin>>n>>np>>nc>>m)
    { Start=n+1;End=n+2;
        memset(head,-1,sizeof(head));
        cnt=0;
        for(int i=1;i<=m;i++)
        {
            cin>>s[1];cin>>x;cin>>s[2];cin>>y;cin>>s[3];cin>>w;
           // cout<<x<<y<<w<<endl;
            add(x,y,w);
        }
        for(int i=1;i<=np;i++)
        {
            cin>>s[1];cin>>x;cin>>s[2];cin>>w;
           // cout<<Start<<x<<w<<endl;
            add(Start,x,w);
        }
        for(int i=1;i<=nc;i++)
        {
            cin>>s[1];cin>>x;cin>>s[2];cin>>w;
           // cout<<x<<End<<w<<endl;
            add(x,End,w);
        }
        int ans=dinic();
        cout<<ans<<endl;
    }
}

  

代码:

原文地址:https://www.cnblogs.com/huangdao/p/9313605.html

时间: 2024-08-10 05:17:45

poj-1459(网络流-最大流)的相关文章

Power Network (poj 1459 网络流)

Language: Default Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23407   Accepted: 12267 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node

poj 1459(网络流)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 26688   Accepted: 13874 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 网络流问题`EK

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24930   Accepted: 12986 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: 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 经典网络流构图问题 最大流,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训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ训练计划2516_Minimum Cost(网络流/费用流)

解题报告 题意: 有n个商店,m个提供商,k种商品</span> n*k的矩阵,表示每个商店需要每个商品的数目: m*k矩阵,表示每个提供商拥有每个商品的个数 然后对于每个物品k,都有n*m的矩阵 i行j列表示 从j提供商向i商店运送一个k商品的代价是多少 判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用 思路: 建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用, 不过这样

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

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