UVa11506 - Angry Programmer(ISAP)

You, a programmer of an important software house, have been fired because you didn't solve an important problem that was assigned to you. You are very furious and want to take revenge on your boss, breaking the communication between his computer and the central server.


The computer of your boss and the central server are in the same network, which is composed of many machines (computers) and wires linking pairs of those machines. There is at most one wire between any pair of machines and there can be pairs of machines without a wire between them.


To accomplish your objective, you can destroy machines and wires, but you can't destroy neither the computer of your boss nor the central server, because those machines are monitored by security cameras. You have estimated the cost of blowing up each machine and the cost of cutting each wire in the network.


You want to determine the minimum cost of interrupting the communication between your boss' computer and the central server. Two computers A and B can communicate if there is a sequence of undestroyed machines x1,...,xn such that x1 = A , xn = B and xi is linked with xi+1 with an uncut wire (for each $ \leq$ i $ \leq$ n - 1 ).

Input 

The input consists of several test cases. Each test case is represented as follows:

  • A line with two integers M and W ( $ \leq$ M $ \leq$ 50 , $ \leq$ W $ \leq$ 1000 ), representing (respectively) the number of machines and the number of wires in the network.
  • M - 2 lines, one per machine (different from the boss' machine and the central server), containing the following information separated by spaces:
    • An integer i ( $ \leq$ i $ \leq$ M - 1 ) with the identifier of the machine. Assume that the boss' machine has id 1 and that the central server has id M .
    • An integer c ( $ \leq$ c $ \leq$ 100000 ) specifying the cost of destroying the machine.
  • W lines, one per wire, containing the following information separated by spaces:
    • Two integers j and k ( $ \leq$ j < k $ \leq$ M ) specifying the identifiers of the machines linked by the wire. Remember that the wire is bidirectional.
    • An integer d ( $ \leq$ d $ \leq$ 100000 ) specifying the cost of cutting the wire.


The end of the input is specified by a line with the string ``0 0''.


Suppose that the machines have distinct identifiers.

Output 

For each test case, print a line with the minimum cost of interrupting the communication between the computer of your boss and the central server.

Sample Input 

4 4
3 5
2 2
1 2 3
1 3 3
2 4 1
3 4 3
4 4
3 2
2 2
1 2 3
1 3 3
2 4 1
3 4 3
0 0

Sample Output 

4
3


#include <cstdio>  
#include <queue>  
#include <cstring>  
#include <algorithm>  
  
using namespace std;  
  
const int N = 110;  
const int INF = 0x3f3f3f3f;  
   
int m, w;   
int d[N], num[N];  
int cur[N], p[N];
int cap[N][N], flow[N][N];
  
bool input();  
void rev_bfs();
int max_flow();
int Augment();
int Retreat(int &i);

int main()  
{  
    #ifndef ONLINE_JUDGE  
        freopen("d:\\OJ\\uva_in.txt", "r", stdin);  
    #endif  
      
    while (input()) {  
		 int ans = max_flow();
        printf("%d\n", ans);  
    }  
    return 0;  
}  
  
bool input()  
{  
    if (scanf("%d%d", &m, &w) != 2 || (m == 0 && w == 0)) return false;  
      
    int u, v, c;  
	
	memset(cap, 0x00, sizeof(cap));
	memset(flow, 0x00, sizeof(flow));
    for (int i = 0; i < m - 2; i++) {  
        scanf("%d%d", &u, &c);  
		 cap[u][u + m] = cap[u + m][u] = c;
    }  
      
    for (int i = 0; i < w; i++) {  
        scanf("%d%d%d", &u, &v, &c);  
		if (u == 1 && v == m) {
			  cap[u][v] = cap[v][u] = c;
        } else if (u == 1 && v != m) {  
			  cap[u][v] = c;
			  cap[v + m][u] = c;
        } else if (u == m && v == 1) {  
			  cap[u][v] = cap[v][u] = c;
        } else if (u == m && v != 1) {   
			cap[u][v] = c;
			cap[v + m][u] = c;
        } else {  
			cap[u + m][v] = c;
			cap[v + m][u] = c;
        }  
    }  
    return true;  
}  

void rev_bfs()
{
	queue<int> q;
	
	memset(num, 0x00, sizeof(num));
	for (int i = 1; i <= 2 * m; i++) {
		num[d[i] = 2 * m]++;
	}
	
	num[2 * m]--;
	d[m] = 0;
	num[0]++;
	q.push(m);
	
	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (int u = 1; u < 2 * m; u++) {
			if (d[u] < 2 * m || cap[u][v] == 0) continue;
			
			q.push(u);
			num[d[u]]--;
			d[u] = d[v] + 1;
			num[d[u]]++;
		}
	}
}

int max_flow()
{
	int f = 0, i, j;
	
	memset(d, 0x00, sizeof(d));
	memset(p, 0x00, sizeof(p));
	rev_bfs();
	
	for (i = 1; i <= 2 * m; i++) cur[i] = 1;
	i = 1;
	
	for (; d[1] < 2 * m; )
	{
		for (j = cur[i]; j <= 2 * m; j++) {
			if (cap[i][j] > flow[i][j] && d[i] == d[j] + 1) break;
		}
	
		if (j <= 2 * m) {
			cur[i] = j;
			p[j] = i;
			i = j;
	
			if (i == m) {
				f += Augment();
				i = 1;
			}
		} else {
			cur[i] = 1;
			if (Retreat(i) == 0) break;
		}
	}
	return f;
}

int Augment()
{
	int ans = INF;
	for (int i = m, j = p[i]; i != 1; i = j, j = p[j]) {
		ans = min(ans, cap[j][i] - flow[j][i]);
	}
	
	for (int i = m,  j = p[i]; i != 1; i = j, j = p[j]) {
		flow[j][i] += ans;
		flow[i][j] -= ans;
	}
	
	return ans;
}

int Retreat(int &i)
{
	int mind = 2 * m - 1;
	
	for (int j = 1; j <= 2 * m; j++) {
		if (cap[i][j] > flow[i][j] && d[j] < mind) {
			mind = d[j];
		}
	}
	
	int tmp = d[i];
	num[d[i]]--;
	d[i] = mind + 1;
	num[d[i]]++;
	
	if (i != 1) i = p[i];
	
	return num[tmp];
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值