UVA 10330 Power Transmission(网络最大流)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1271

 Power Transmission 

The Problem

DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal. The main aim of this project is to transfer Power in Dhaka. As Dhaka is a megacity with almost
10 million people DESA wants to transfer maximum amount of power through the network. But as always occurs in case of power transmission it is tough to resist loss. So they want to use some regulators whose main aim are to divert power through several outlets
without any loss.

Each such regulator has different capacity. It means if a regulator gets 100 unit power and it‘s capacity is 80 unit then remaining 20 unit power will be lost. Moreover each unidirectional link( Connectors
among regulators) has a certain capacity. A link with capacity 20 unit cannot transfer power more than 20 unit. Each regulator can distribute the input power among the outgoing links so that no link capacity is overflown. DESA wants to know the maximum amount
of power which can be transmitted throughout the network so that no power loss occurs. That is the job you have to do.

( Do not try to mix the above description with the real power transmission.)

The Input

The input will start with a postive integer N ( 1<=N<=100 ) indicates the number of regulators. The next few lines contain N positive integers indicating the capacity of each regulator from 1 to N. The
next line contains another positive integer M which is the number of links available among the regulators. Following M lines contain 3 positive integers ( i j C) each. ‘i‘ and ‘j‘ is the regulator index ( 1<=i,j<=N) and C is the capacity of the link. Power
can transfer from i‘th regulator to j‘th regulator. The next line contains another two positive integers B and D. B is the number of regulators which are the entry point of the network. Power generated in Barisal must enter in the network through these entry
points. Simmilarly D is the number of regulators connected to Dhaka. These links are special and have infinite capacity. Next line will contain B+D integers each of which is an index of regulator. The first B integers are the index of regulators connected
with Barisal. Regulators connected with Barisal are not connected with Dhaka.

Input is terminated by EOF.

The Output

For each test case show the maximum amount of power which can be transferred to Dhaka from Barisal. Use a seperate line for each test case.

Sample Input

4
10 20 30 40
6
1 2 5
1 3 10
1 4 13
2 3 5
2 4 7
3 4 20
3 1
1 2 3 4
2
50 100
1
1 2 100
1 1
1 2

Sample Output

37
50

Md. Kamruzzaman

题意:

电网中有n个变压器,每个变压器有容量限制,有m条电缆(有向),每条电缆也有容量限制;b个变压器和发电厂相连,d个和用户相连,求电网中最大输电流。

分析:

这是个顶点上也有限制的网络最大流,处理方法是将这个点拆成两个点,这两个点之间连一条边(注意方向:入—>出),边的容量是原来点的容量。发电站是源点,与b个变压器连容量是无穷大的边;用户是汇点,d个变压器和用户连容量是无穷大的边。然后用Dinic算法愉快地处理。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 50007
#define maxn 207

using namespace std;

int fir[maxn];
int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];
int e_max;
int q[maxn],lv[maxn],iter[maxn];

void add_edge(int _u,int _v,int _w)
{
    int e;
    e=e_max++;
    u[e]=_u;v[e]=_v;cap[e]=_w;
    nex[e]=fir[u[e]];fir[u[e]]=e;
    e=e_max++;
    u[e]=_v;v[e]=_u;cap[e]=0;
    nex[e]=fir[u[e]];fir[u[e]]=e;
}

void dinic_bfs(int s)
{
    int f,r;
    memset(lv,-1,sizeof lv);
    q[f=r=0]=s;
    lv[s]=0;
    while (f<=r)
    {
        int x=q[f++];
        for (int e=fir[x];~e;e=nex[e])
        {
            if (lv[v[e]]<0 && cap[e]>flow[e])
            {
                lv[v[e]]=lv[u[e]]+1;
                q[++r]=v[e];
            }
        }
    }
}

int dinic_dfs(int _u,int t,int _f)
{
    if (_u==t) return _f;
    for (int &e=iter[_u];~e;e=nex[e])
    {
        if (cap[e]>flow[e] && lv[_u]<lv[v[e]])
        {
            int _d=dinic_dfs(v[e],t,min(_f,cap[e]-flow[e]));
            if (_d>0)
            {
                flow[e]+=_d;
                flow[e^1]-=_d;
                return _d;
            }
        }
    }

    return 0;
}

int max_flow(int s,int t)
{
    memset(flow,0,sizeof flow);
    int total_flow=0;

    for (;;)
    {
        dinic_bfs(s);
        if (lv[t]<0)    return total_flow;
        memcpy(iter,fir,sizeof fir);
        int _f;
        while ((_f=dinic_dfs(s,t,INF))>0)
            total_flow+=_f;
    }

    return total_flow;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    int n,m,b,d,_u,_v,_w;

    while (~scanf("%d",&n))
    {
        memset(fir,-1,sizeof fir);
        e_max=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&_w);
            add_edge(i,i+n,_w);
        }

        scanf("%d",&m);
        for (int i=0;i<m;i++)
        {
            scanf("%d %d %d",&_u,&_v,&_w);
            add_edge(_u+n,_v,_w);
        }

        scanf("%d %d",&b,&d);
        int s=0,t=n<<1|1;
        for (int i=0;i<b;i++)
        {
            scanf("%d",&_u);
            add_edge(s,_u,INF);
        }
        for (int i=0;i<d;i++)
        {
            scanf("%d",&_u);
            add_edge(_u+n,t,INF);
        }

        printf("%d\n",max_flow(s,t));

    }

    return 0;
}

UVA 10330 Power Transmission(网络最大流)

时间: 2024-10-12 23:05:20

UVA 10330 Power Transmission(网络最大流)的相关文章

uva 10330 Power Transmission (最大流 + 拆点)

uva 10330 Power Transmission 如果对最大流不熟悉的话可以先去看看这个 题目大意:先来讲解一下INPUT.首先读入一个正整数N, 接下来N个数据是N个调节器的容量:然后读入一个正整数M, 接下来M组数据代表的是M条调节器与调解器之间的线路(调节器u, 调节器v, 容量);最后的一组数据先是两个正整数a和b, 接下来的a个数据代表的是初始的调节器,最后的b个数据代表的是终结的调节器. 综上,求最大流. 解题思路:源点或汇点不唯一的时候,记得要增加超级源点或超级汇点来使得源

UVA 10330 Power Transmission

题意:懒得打了.LUCKY CAT 里有 http://163.32.78.26/homework/q10330.htm 第一个网络流题目.每个节点都有一个容量值.需要拆点.拆成i - > i + N  边权为容量值 另外注意B个点 连接方式:s - 集合B D个点 链接方式 集合D + N -> t汇点 其他看处理就好 #include <map> #include <set> #include <list> #include <cmath>

POJ 1459 &amp; ZOJ 1734 Power Network (网络最大流)

http://poj.org/problem?id=1459 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22674   Accepted: 11880 Description A power network consists of nodes (power s

light oj 1155 - Power Transmission【拆点网络流】

1155 - Power Transmission   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal. The main aim of this project is to tr

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

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

算法模板——Dinic网络最大流 2

实现功能:同Dinic网络最大流 1 这个新的想法源于Dinic费用流算法... 在费用流算法里面,每次处理一条最短路,是通过spfa的过程中就记录下来,然后顺藤摸瓜处理一路 于是在这个里面我的最大流也采用这种模式,这样子有效避免的递归,防止了爆栈么么哒 1 type 2 point=^node; 3 node=record 4 g,w:longint; 5 next,anti:point; 6 end; 7 var 8 i,j,k,l,m,n,s,t,flow:longint; 9 a,e:a

UVa 10298 - Power Strings

题目:求一个串的最大的循环次数. 分析:dp,KMP,字符串.这里利用KMP算法. KMP的next函数是跳跃到最近的串的递归结构位置(串元素取值0 ~ len-1): 由KMP过程可知: 如果存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配: 则S[next[len] ~ len-1]就是循环节(且最小),否则next[len]为0: 因此,最大循环次数为len/(len-next[len]),最小循环节为S[next[len] ~

POJ--3308--Paratroopers【Dinic】二分图顶点覆盖+网络最大流

链接:http://poj.org/problem?id=3308 题意:未来世界火星人要入侵地球,他们要派一些伞兵来摧毁地球的兵工厂,兵工厂可以视为一个m*n的矩阵,现在知道了他们每个伞兵的降落位置.为了粉碎火星人的阴谋,我们需要在某行或某列来架一个机关枪来消灭一整行或一整列的火星人,但是在这需要一定的花费,告诉每行及每列架机关枪的花费,总花费是每行及每列的花费相乘.求使得火星人全部被消灭的最小花费. 思路:需要消灭所有敌人,是二分图最小点权覆盖问题,要覆盖所有的边并使花费最小,即求最小割,根

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