Sabotage

题目

The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebellion. Because of the enormous disturbances this is causing in world economy, an imperialist military super power has decided to invade the country and reinstall the old regime. For this operation to be successful, communication between the capital and the largest city must be completely cut. This is a difficult task, since all cities in the country are connected by a computer network using the Internet Protocol, which allows messages to take any path through the network. Because of this, the network must be completely split in two parts, with the capital in one part and the largest city in the other, and with no connections between the parts. There are large differences in the costs of sabotaging different connections, since some are much more easy to get to than others. Write a program that, given a network specification and the costs of sabotaging each connection, determines which connections to cut in order to separate the capital and the largest city to the lowest possible cost. Input Input file contains several sets of input. The description of each set is given below. The first line of each set has two integers, separated by a space: First one the number of cities, n in the network, which is at most 50. The second one is the total number of connections, m, at most 500. The following m lines specify the connections. Each line has three parts separated by spaces: The first two are the cities tied together by that connection (numbers in the range 1 − n). Then follows the cost of cutting the connection (an integer in the range 1 to 40000000). Each pair of cites can appear at most once in this list. Input is terminated by a case where values of n and m are zero. This case should not be processed. For every input set the capital is city number 1, and the largest city is number 2. Output For each set of input you should produce several lines of output. The description of output for each set of input is given below: The output for each set should be the pairs of cities (i.e. numbers) between which the connection should be cut (in any order), each pair on one line with the numbers separated by a space. If there is more than one solution, any one of them will do. Print a blank line after the output for each set of input.

样例

 

这道题的意思要把一个图分成两部分,要把点1和点2分开。隔断每条边都有一个花费,求最小花费的情况下,应该切断那些边。
这题很明显是最小割,也就是最大流。把1当成源点,2当成汇点。其实就是求要割哪几个边!看了kuangbin的模板才发现原来还有这么简洁的模板!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
typedef long long ll;
using namespace std;
#define maxn 105
#define inf 0x3f3f3f3f
#define maxm 555
int g[maxn][maxn];
int flow[maxn][maxn];
int path[maxn];
int a[maxn];
int bg, ed;
int n;
int maxflow() {
	queue<int> q;
	memset(flow, 0, sizeof(flow));
	int max_flow = 0;
	while (1) {
		memset(a, 0, sizeof(a));
		a[bg] = inf;
		while (!q.empty())q.pop();
		q.push(bg);
		while (!q.empty()) {
			int u = q.front();
			q.pop();
			for (int v = 1; v <= n; ++v) {
				if (!a[v] && flow[u][v] < g[u][v]) {
					path[v] = u;
					a[v] = min(a[u], g[u][v] - flow[u][v]);
					q.push(v);
				}
			}
		}
		if (a[ed] == 0)break;
		for (int u = ed; u != bg; u = path[u]) {
			flow[path[u]][u] += a[ed];
			flow[u][path[u]] -= a[ed];
		}
		max_flow += a[ed];
	}
	return max_flow;
}

int x[maxm], y[maxm];
int m;
int main() {
	while (scanf("%d%d", &n, &m) == 2) {
		if (n == 0 && m == 0)break;
		memset(g, 0, sizeof(g));
		for (int i = 0; i < m; ++i) {
			scanf("%d%d", &x[i], &y[i]);
			scanf("%d", &g[x[i]][y[i]]);
			g[y[i]][x[i]] = g[x[i]][y[i]];
		}
		bg = 1, ed = 2;
		maxflow();
		for (int i = 0; i < m; ++i) {
			if ((!a[x[i]] && a[y[i]]) || (a[x[i]] && !a[y[i]]))
				printf("%d %d\n", x[i], y[i]);
		}
		printf("\n");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值