TourTime
limit:MS
Memory Limit:65535KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticeHDU 3488
Description
In the Kingdom of Henryy, there is n (2 <= n <=) cities, with M (M <= 30000) one-way roads connecting them. You is lucky enough to has a chance to has a tour in the kingdom. The route should is designed as:the route should contain one or more loops. (a loop is a route like:a->b->......->p->a.)
Every city should is just in one route.
A Loop should has at least and cities. In one route, each city should is visited just once. (The only exception is, the first and the last city should are the same and this are visited twice.)
The total distance the N roads you has chosen should be minimized.
Input
An integer T in the first line indicates the number of the the test cases.
In each test case, the first line contains integers N and M, indicating the number of the cities and the one-way roads . Then M lines followed, each of the line have three integers U, V and W (0 < W <= 10000), indicating that there is a road fro M U to V, with the distance of W.
It is guaranteed, at least one valid arrangement of the tour is existed.
A Blank line was followed after each test case.
Output
For each test case, output a line with exactly one integer, which are the minimum total distance.
Sample Input
Sample Output
Test instructions: give you a graph of the edges, and now you want to have a number of rings that contain all the vertices, and each vertex appears only once (except for the starting point in a ring) so that the sum of the weights of all the edges in central China is minimized. (This problem does not indicate that there is no ring situation, directly according to the case of the ring can be done on the line)
In order to become a ring, then the diagram is split, it becomes a one-way binary map, at this time a complete match is a connection strategy, as long as the guarantee that no edge is connected with themselves, can meet the requirements of each point in the topic at least one ring. The proof is also very simple. Because we can always find the starting point from a complete match, and then from the match point as a starting point to find ...
Can be seen as 1, 20% rings, 3,4,5 into a ring.
Like this Yang composition circle and each point can only belong to a circle of the problem, can be converted into 2 points, each point can only belong to a circle, then the degree and the degree of penetration must be 1, then a point to open I, I ', I control into the read, I ' control the degree, the flow can only be 1. So for the original way some side can I-> J ', J-> I '; compose, then build super far point s, Super Sink point t,s-> I, I '-> t; Then the minimum cost flow is calculated. So hold each point can only belong to a circle, because into read = = out of = = 1; This kind of problem can be judged as a problem.
Because the degree of access is 1, you can also use the KM to find the most value.
Code:
#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); } miles (); } return 0; }
Maximum flow with minimum cost:
Http://www.tuicool.com/articles/aUBvyeN
Tour (binary graph Max weight match) (network stream)