LightOJ 1156 Power Transmission(网络流)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82153013

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 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 aims are to divert power through several outlets without any loss.

Each such regulator has different capacity. It means if a regulator gets 100 units of power and its capacity is 80 units then remaining 20 units of power will be lost. Moreover each unidirectional link (connectors among regulators) has a certain capacity. A link with capacity 20 units cannot transfer power more than 20 units. Each regulator can distribute the input power among the outgoing links so that no link capacity is over flown. 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.)

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

The input will start with a positive integer N (1 ≤ N ≤ 100) indicates the number of regulators. The next line contains N positive integers indicating the capacity of each regulator from 1 to N. All the given capacities will be positive and not greater than 1000. The next line contains another positive integer M which is the number of links available among the regulators. Each of the following M lines contains three positive integers i j C'i' and 'j' are the regulator index (1 ≤ i, j ≤ N, i ≠ j, 1 ≤ C ≤ 1000) and C is the capacity of the link. Power can be transferred from ithregulator to jth regulator. From a regulator i to another regulator j, there can be at most one link.

The next line contains two positive integers B and D (1 ≤ B, D and B + D ≤ N)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. Similarly 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.

Output

For each case of input, print the case number and the maximum amount of power which can be transferred from Barisal to Dhaka.

Sample Input

Output for Sample Input

2

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

Case 1: 37

Case 2: 50

 题意:给你n个站点以及m条单向线路,又给出每个站点的流量限制和每条线路的起点、终点、流量限制。现在有B个起点,D个汇点,问你从起点出发到达汇点的最大流量。

网络流的多源多汇问题,最短路也有类似的问题,解决的方法都是一样的,建立一个超级源点,超级汇点即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1010;
const int maxm=10010;
const int inf=0x3f3f3f3f;
struct Node
{
    int to;
    int capa;
    int next;
}edge[maxm];
int source,sink;
int cnt;
int head[maxn];
bool vis[maxn];
int num[maxn];
int dep[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    return;
}
void add(int u,int v,int capa)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool bfs()
{
    queue<int> que;
    que.push(source);
    memset(dep,-1,sizeof(dep));
    dep[source]=0;
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa>0&&dep[v]==-1)
            {
                dep[v]=dep[node]+1;
                if(v==sink) return true;
                que.push(v);
            }
        }
    }
    return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
    if(node==sink||minn==0)
    {
        return minn;
    }
    int r=0;
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(dep[v]==dep[node]+1&&edge[i].capa>0)
        {
            int tmp=dfs(v,min(edge[i].capa,minn));
            if(tmp>0)
            {
                edge[i].capa-=tmp;
                edge[i^1].capa+=tmp;
                r+=tmp;
                minn-=tmp;
                if(!minn) break;
            }
        }
    }
    if(!r) dep[node]=-1;
    return r;
}
int dinic()
{
    int maxflow=0;
    while(bfs())
    {
        maxflow+=dfs(source,inf);
    }
    return maxflow;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int test;
    scanf("%d",&test);
    for(int cas=1;cas<=test;cas++)
    {
        init();
        int n;
        scanf("%d",&n);
        source=0;
        sink=n*2+1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            add(i,i+n,num[i]);
        }
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u+n,v,w);
        }
        int b,d;
        scanf("%d%d",&b,&d);
        for(int i=1;i<=b;i++)
        {
            int x;
            scanf("%d",&x);
            add(source,x,inf);
        }
        for(int i=1;i<=d;i++)
        {
            int x;
            scanf("%d",&x);
            add(x+n,sink,inf);
        }
        printf("Case %d: %d\n",cas,dinic());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82153013