Tour(二分圖最大權匹配)(網路流)
阿新 • • 發佈:2018-12-24
Tour
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u
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.
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.
For each test case, output a line with exactly one integer, which is the minimum total distance.
用最小費用最大流:
Description
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
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
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 4Sample Output
42題意:給你一個有向圖,邊有權值,現在要你求若干個環包含所有的頂點,並且每個頂點只出現一次(除了一個環中的起始點)使得華中所有邊得權值之和最小。(這道題沒有說明沒有環的情況,直接按照都有環的情況做就行了)
由於要成環,那麼將這個圖進行拆點,就變成了單向的二分圖了,此時一個完備匹配就是一種連線策略,只要保證沒有邊是和自己相連,就能夠滿足題目中要求的每個點至少屬於一個環。證明也是很簡單的。因為我們總可以從一個完備匹配中找出起點,然後再從匹配點作為起點找......
上圖可以看做是1,2成環,3,4,5成環。
像這楊構成圈並且每個點只能屬於一個圈的題, 可以轉化成2 分圖, 每個點只能屬於一個圈, 那麼出度和入度必定為1 , 那麼把一個點拆開i, i`, i控制入讀, i` 控制出度, 流量只能為1 。 那麼對於原來途中有的邊 可以 i - > j`, j - > i`;連起來構圖, 然後建立超級遠點s,超級匯點t,s - > i , i` - > t ; 然後求最小費用流。。這樣就抱著了每個點只能屬於一個圈, 因為入讀 == 出度 == 1 ;這類也問題可以 做為判斷性問題出。
因為出入度 都是1 所以也可以用 km 求最值。。
程式碼:
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int N, M;
const int INF = 0x3f3f3f3f;
int w[205][205];
int lx[205], ly[205];
int sx[205], sy[205];
int 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;
}
void KM() {
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);
}
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);
}
KM();
}
return 0;
}
用最小費用最大流: