lightoj 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 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 Mwhich 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 ith regulator 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

 

PROBLEM SETTER: MD. KAMRUZZAMAN
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)


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

思路:设置超级源点S和超级汇点T。把每个站拆点后,以流量限制为边的容量,最后S->T跑最大流。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define lson o<<1|1, l, mid
#define rson o<<1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define INF 0x3f3f3f3f
#define eps 1e-8
#define debug printf("1\n")
#define MAXN 210
#define MAXM 30000
#define LL long long
#define CLR(a, b) memset(a, (b), sizeof(a))
#define W(a) while(a--)
#define Ri(a) scanf("%d", &a)
#define Ri2(a, b) scanf("%d%d", &a, &b)
#define Pi(a) printf("%d\n", (a))
#define Pi2(a, b) printf("%d %d\n", a, b)
#define Rl(a) scanf("%lld", &a)
#define Rl2(a, b) scanf("%lld%lld", &a, &b)
#define Pl(a) printf("%lld\n", (a))
#define Pl2(a, b) printf("%lld %lld\n", a, b)
#define Rs(a) scanf("%s", a)
#define Rs2(a, b) scanf("%s%s", a, b)
#define Ps(a) printf("%s\n", (a))
#define Ps2(a, b) printf("%s %s\n", a, b)
#define FOR(i, l, r) for(int i = (l); i <= (r); i++)
#define FOR1(i, l, r) for(int i = (l); i < (r); i++)
#define MOD 1000000007
using namespace std;
struct MAXFLOW
{
    struct Edge{
        int from, to, cap, flow, next;
    };
    Edge edge[MAXM];
    int head[MAXN], edgenum;
    int dist[MAXN], cur[MAXN];
    bool vis[MAXN];
    void init(){
        edgenum = 0;
        CLR(head, -1);
    }
    void addEdge(int u, int v, int w)
    {
        Edge E1 = {u, v, w, 0, head[u]};
        edge[edgenum]= E1;
        head[u] = edgenum++;
        Edge E2 = {v, u, 0, 0, head[v]};
        edge[edgenum]= E2;
        head[v] = edgenum++;
    }
    bool BFS(int s, int t)
    {
        queue<int> Q;
        CLR(dist, -1); CLR(vis, false);
        vis[s] = true, dist[s] = 0;
        Q.push(s);
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = edge[i].next)
            {
                Edge E = edge[i];
                if(!vis[E.to] && E.cap > E.flow)
                {
                    dist[E.to] = dist[u] + 1;
                    if(E.to == t) return true;
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
        return false;
    }
    int DFS(int x, int a, int t)
    {
        if(x == t || a == 0) return a;
        int flow = 0, f;
        for(int &i = cur[x]; i != -1; i = edge[i].next)
        {
            Edge &E = edge[i];
            if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
            {
                edge[i].flow += f;
                edge[i^1].flow -= f;
                flow += f;
                a -= f;
                if(a == 0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s, int t)
    {
        int flow = 0;
        while(BFS(s, t))
        {
            memcpy(cur, head, sizeof(head));
            flow += DFS(s, INF, t);
        }
        return flow;
    }
};
MAXFLOW dinic;
int node[MAXN];
int S, T;
void getMap()
{
    int n; Ri(n);
    dinic.init();
    FOR(i, 1, n)
    {
        Ri(node[i]);
        dinic.addEdge(i, i+n, node[i]);
    }
    int m; Ri(m);
    W(m)
    {
        int a, b, c;
        Ri(a), Ri(b), Ri(c);
        dinic.addEdge(a+n, b, c);
    }
    int B, D; Ri2(B, D);
    S = 0, T = 2*n+1;
    W(B)
    {
        int id; Ri(id);
        dinic.addEdge(S, id, node[id]);
    }
    W(D)
    {
        int id; Ri(id);
        dinic.addEdge(id+n, T, node[id]);
    }
}
int main()
{
    int t, kcase = 1; Ri(t);
    W(t)
    {
        getMap();
        printf("Case %d: %d\n", kcase++, dinic.Maxflow(S, T));
    }
    return 0;
}


你可能感兴趣的:(lightoj 1155 - Power Transmission 【多源多汇 拆点网络流】)