HDU - 3488 Tour(KM)

In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way roads connecting them. You are lucky enough to have a chance to have a tour in the kingdom. The route should be designed as: The route should contain one or more loops. (A loop is a route like: A->B->……->P->A.) 
Every city should be just in one route. 
A loop should have at least two cities. In one route, each city should be visited just once. (The only exception is that the first and the last city should be the same and this city is visited twice.) 
The total distance the N roads you have chosen should be minimized. 

Input

An integer T in the first line indicates the number of the test cases. 
In each test case, the first line contains two integers N and M, indicating the number of the cities and the one-way roads. Then M lines followed, each line has three integers U, V and W (0 < W <= 10000), indicating that there is a road from U to V, with the distance of W. 
It is guaranteed that at least one valid arrangement of the tour is existed. 
A blank line is followed after each test case.

Output

For each test case, output a line with exactly one integer, which is the minimum total distance.

Sample Input

1
6 9
1 2 5
2 3 5
3 1 10
3 4 12
4 1 8
4 6 11
5 4 7
5 6 9
6 5 4

Sample Output

42

 

     让你找出几条路线,路线中成环,在所有的路线中只能经过每个城市一次(环的起点不算),求最短的环的总路程;

     然后我们再想一想完备匹配后的结果:你从每个点开始,都可以到达另一个点,并从另一个点出发:即可以表示成一个又一个的环。。

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 310;
int n, m;
int G[maxn][maxn], use[maxn], lx[maxn], ly[maxn];
int slack[maxn];
bool visx[maxn], visy[maxn];
bool find(int x) {
	visx[x] = 1;
	for (int y = 1; y <= m; y++) {
		if (visy[y])continue;
		int tmp = lx[x] + ly[y] - G[x][y];
		if (tmp == 0) {
			visy[y] = 1;
			if (use[y] == -1 || find(use[y])) {
				use[y] = x; return 1;
			}
		}
		else if (slack[y] > tmp) 
			slack[y] = tmp;
	}
	return 0;
}
int km() {
	memset(use, -1, sizeof(use));
	memset(ly, 0, sizeof(ly));
	for (int s = 1; s <= n; s++) {
		lx[s] = -inf;
		for (int w = 1; w <= m; w++)
			if (G[s][w] > lx[s])
				lx[s] = G[s][w];
	}
	for (int x = 1; x <= n; x++) {
		for (int s = 1; s <= m; s++)
			slack[s] = inf;
		while (1) {
			memset(visx, 0, sizeof(visx));
			memset(visy, 0, sizeof(visy));
			if (find(x)) break;
			int d = inf;
			for (int s = 1; s <= m; s++)
				if (!visy[s] && d > slack[s])
					d = slack[s];
			for (int s = 1; s <= n; s++)
				if (visx[s])
					lx[s] -= d;
			for (int s = 1; s <= m; s++)
				if (visy[s])ly[s] += d;
				else slack[s] -= d;
		}
	}
	int res = 0;
	for (int s = 1; s <= m; s++)
		if (use[s] != -1)
			res += G[use[s]][s];
	return res;
}
int main()
{
	int ed, te;
	scanf("%d", &te);
	while (te--) {
		scanf("%d%d", &n, &ed);
		m = n;
		for (int s = 1; s <= n; s++)
			for (int w = 1; w <= n; w++)
				G[s][w] = -inf;
		while (ed--) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			G[a][b] = max(G[a][b], -c);
		}
		int ans = km();
		ans = -ans;
		cout << ans << endl;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值