关闭

10330 - Power Transmission (最大流)

852人阅读 评论(0) 收藏 举报
分类:

 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
题意:给定n个输送点,有流量上限,然后从一个源点要输送到汇点,求最多输出多少流量
思路:最大流 拆点。
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define INF 0x3f3f3f3f
using namespace std;

const int N = 205;

int n, limit, m, A, B, V, sn, tn, line, g[N][N], f[N][N], a[N], p[N];
queue<int>q;

void init() {
    memset(g, 0, sizeof(g));
    for (int i = 1; i <= n; i ++) {
	scanf("%d", &limit);
	g[i][i + n] = limit;
    }
    scanf("%d", &m);
    while (m --) {
	scanf("%d%d%d", &A, &B, &V);
	g[A + n][B] = V;
    }
    scanf("%d%d", &sn, &tn);
    for (int i = 0; i < sn; i ++) {
	scanf("%d", &line);
	g[0][line] = INF;
    }
    for (int i = 0; i < tn; i ++) {
	scanf("%d", &line);
	g[line + n][2 * n + 1] = INF;
    }
}

int solve(int s, int t) {
    init();
    int ans = 0;
    memset(f, 0, sizeof(f));
    while (1) {
	memset(a, 0, sizeof(a));
	a[s] = INF;
	q.push(s);
	while (!q.empty()) {
	    int u = q.front(); q.pop();
	    for (int v = 1; v <= t; v ++) {
		if (!a[v] && g[u][v] > f[u][v]) {
		    p[v] = u; q.push(v);
		    a[v] = min(a[u], g[u][v] - f[u][v]);
		}
	    }
	}
	if (!a[t]) break;
	for (int u = t; u != s; u = p[u]) {
	    f[p[u]][u] += a[t];
	    f[u][p[u]] -= a[t];
	}
	ans += a[t];
    }
    return ans;
}

int main() {
    while (~scanf("%d", &n)) {
	printf("%d\n", solve(0, 2 * n + 1));
    }
    return 0;
}


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1357942次
    • 积分:28746
    • 等级:
    • 排名:第192名
    • 原创:1481篇
    • 转载:8篇
    • 译文:0篇
    • 评论:343条
    文章分类
    最新评论