【HDU3488】【网络流】【最小费用最大流】Tour 题解
2017-06-07 19:13
399 查看
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
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 <cstring> #include <string> #include <set> #include <queue> #include <algorithm> #include <vector> #include <cstdlib> #include <cmath> #include <ctime> #include <stack> #define ll long long #define clr(x) memset(x,0,sizeof(x)) using namespace std; const int INF = 0x3f3f3f3f; int N,M; int w[205][205]; int lx[205],ly[205],sx[205],sy[205],match[205],slack[205]; int path(int u) { sx[u] = 1; for(int i = 1; i <= N; ++i) { if(sy[i]) continue; int t = lx[u] + ly[i] - w[u][i]; if(!t) { sy[i] = 1; if(!match[i] || path(match[i])) { match[i] = u; return true; } } else slack[i] = min(slack[i], t); } return false; } int main() { int T, x, y, ct; scanf("%d", &T); while(T--) { scanf("%d %d", &N, &M); memset(w, 0x80, sizeof (w)); for(int i = 1; i <= M; ++i) { scanf("%d %d %d", &x, &y, &ct); w[x][y] = max(w[x][y], -ct); } memset(match, 0, sizeof (match)); memset(lx, 0x80, sizeof (lx)); memset(ly, 0, sizeof (ly)); for(int i = 1; i <= N; ++i) for(int j = 1; j <= N; ++j) lx[i] = max(lx[i], w[i][j]); for(int i = 1; i <= N; ++i) { memset(slack, 0x3f, sizeof (slack)); while(1) { memset(sx, 0, sizeof (sx)); memset(sy, 0, sizeof (sy)); if (path(i)) break; int d = INF; for(int j = 1; j <= N; ++j) if (!sy[j]) d = min(d, slack[j]); for(int j = 1; j <= N; ++j) { if (sx[j]) lx[j] -= d; if (sy[j]) ly[j] += d; else slack[j] -= d; } } } int ret = 0; for(int i = 1; i <= N; ++i) ret += w[match[i]][i]; printf("%d\n", -ret); } return 0; }
相关文章推荐
- 【HDU3488】Tour-最小费用最大流
- 【网络流】hdu3488 Tour
- HDU - 1853 Cyclic Tour(网络流-最小费用最大流)
- 网络流算法集合 EK dinic 最小费用最大流 (Dijkstra实现)
- 网络流问题-最小费用最大流
- Farm Tour POJ - 2135 最小费用最大流
- hdu3488 Tour (KM最小权值和)
- 有向强连通和网络流大讲堂——史无前例最大流(最小割)、最小费用最大流
- 模板整理: 图论---网络流/最小费用最大流
- [题解] [网络流二十四题(十)] 餐巾计划问题(最小费用最大流)
- poj--1637--Sightseeing tour(网络流,最大流判断混合图是否存在欧拉图)
- HDU 1853Cyclic Tour(网络流之最小费用流)
- POJ1637 Sightseeing tour (混合图欧拉回路)(网络流)
- 网络流24题 运输问题 最小费用最大流 模版
- HDU3488 Tour [有向环覆盖 费用流]
- Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流)
- POJ 1637 Sightseeing tour(混合欧拉回路,网络流)
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
- 网络流之——最小费用最大流
- HDU 6118 度度熊的交易计划(网络流-最小费用最大流)