Ligthoj 1155--Power Transmission【最大流 && 拆点】

来源:互联网 发布:gartner 云计算 2017 编辑:程序博客网 时间:2024/05/19 03:28
1155 - Power Transmission
PDF (English)StatisticsForum
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 from1 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 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+Dintegers 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个汇点,问你从起点出发到达汇点的
最大流量。

解析:很常规的网络流的问题,我们题目中虽然有多个源点汇点,我们可以设置一个超级源点,超级汇点,这样就转化为常规的最大流的问题了,这里要进行拆点处理,。
具体建图方法:

(1)每个站点拆成左点和右点,左点到右点建边,容量为站点的流量限制
(2)每条路线的起点到终点建边,容量为每条路线的流量限制
(3)超级源点到每个起点的左点建边,容量为起点的流量限制
(4)每个终点的右点到超级汇点建边,容量为终点的流量限制
建好图后跑一遍最大流即可

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define maxn 1000#define maxm 400000#define INF 0x3f3f3f3fusing namespace std;int n, m;int outset, inset;int power[maxn];struct node {    int u, v, cap, flow, next;};node edge[maxm];int head[maxn], cnt, cur[maxn];int vis[maxn], dist[maxn];void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void add(int u, int v, int w){    edge[cnt] = {u, v, w, 0, head[u]};    head[u] = cnt++;    edge[cnt] = {v, u, 0, 0, head[v]};    head[v] = cnt++;}void getmap(){    scanf("%d", &n);    outset = 0;    inset = 2 * n + 1;    for(int i = 1; i <= n; ++i){        scanf("%d", &power[i]);        add(i, i + n, power[i]);    }    int a, b, c;    scanf("%d", &m);    for(int i = 1; i <= m; ++i){        scanf("%d%d%d",&a, &b, &c);        add(a + n, b, c);    }    int B, D;    scanf("%d%d", &B, &D);    while(B--){        scanf("%d", &a);        add(outset, a, power[a]);    }    while(D--){        scanf("%d", &a);        add(a + n, inset, power[a]);    }}bool BFS(int st ,int ed){    queue<int>q;    memset(vis, 0 ,sizeof(vis));    memset(dist, -1, sizeof(dist));    vis[st] = 1;    dist[st] = 0;    q.push(st);    while(!q.empty()){        int u = q.front();        q.pop();        for(int i = head[u]; i != -1; i = edge[i].next){            node E = edge[i];            if(!vis[E.v] && E.cap > E.flow){                vis[E.v] = 1;                dist[E.v] = dist[u] + 1;                if(E.v == ed)                    return true;                q.push(E.v);            }        }    }    return false;}int DFS(int x, int ed, int a){    if(x == ed || a == 0)        return a;    int flow = 0, f;    for(int &i = cur[x]; i != -1; i = edge[i].next){        node &E = edge[i];        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){            E.flow += f;            edge[i ^ 1].flow -= f;            a -= f;            flow += f;            if(a == 0)                break;        }    }    return flow;}int maxflow(int st, int ed){    int flowsum = 0;    while(BFS(st,ed)){        memcpy(cur, head, sizeof(head));        flowsum += DFS(st, ed, INF);    }    return flowsum;}int kcase = 1;int main (){    int T;    scanf("%d", &T);    while(T--){        init();        getmap();        printf("Case %d: %d\n", kcase++, maxflow(outset, inset));    }    return 0;}




1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 如果飞机不支持婴儿票怎么办 报志愿登不上去怎么办 微单自拍是反的怎么办 蜡笔弄到桌子上怎么办 油画颜料干透了怎么办 数字油画颜料干了怎么办 数字油画的颜料干了怎么办 丙烯颜料画错了怎么办 油画的油干了怎么办 数字油画没画完颜料干了怎么办? 涂完口红很干怎么办 吃鸡匹配不到人怎么办 电脑吃鸡更新慢怎么办 苹果手机吃鸡更新不了怎么办 吃鸡更新硬盘不够怎么办 吃鸡链接不到更新服务器怎么办 6s吃鸡更新不了怎么办 凌美钢笔刮纸怎么办 毕加索钢笔不出水怎么办妙招 打印机总显示墨水已用完怎么办 樱花勾线笔干了怎么办 枣核卡在喉咙里怎么办 马桶被玉米棒堵了怎么办 雪糕棒掉马桶里了怎么办 食用色素吃多了怎么办 蜡笔弄到指甲里怎么办 小孩把彩笔吃了怎么办 宝宝吃了水彩笔怎么办 宝宝不小心吃了蜡笔怎么办 小孩把蜡笔吃了怎么办 宝宝吃了记号笔怎么办 二年级的孩子不太爱看书怎么办 数字油画涂错了怎么办 白墙上被蜡笔画起来怎么办 衣服上的油画棒怎么办 蜡笔要是涂错了怎么办 蜡笔颜色涂错了怎么办 书被蜡笔涂了怎么办 白灰墙上被蜡笔涂了怎么办 电器上涂了彩色蜡笔怎么办 3d渲大图慢怎么办