Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2549 Accepted Submission(s): 1257
Problem 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.
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.
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
Source
Recommend
#include<cstdio> #include<cstring> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; int n,m; int map[210][210]; int slock[210]; int lx[210],ly[210]; bool visx[210],visy[210]; int nx,ny,match[210]; void getmap() { scanf("%d%d",&n,&m); nx=ny=n; for(int i=1;i<=nx;i++) for(int j=1;j<=ny;j++) map[i][j]=-INF; int x,y,w; for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&w); if(-w>map[x][y]) map[x][y]=-w; } } int DFS(int x) { visx[x]=true; for(int y=1;y<=ny;y++) { if(visy[y]) continue; int t=lx[x]+ly[y]-map[x][y]; if(t==0) { visy[y]=true; if(match[y]==-1||DFS(match[y])) { match[y]=x; return 1; } } else if(t<slock[y]) slock[y]=t; } return 0; } void KM() { memset(match,-1,sizeof(match)); memset(ly,0,sizeof(ly)); for(int x=1;x<=nx;x++) { lx[x]=-INF; for(int y=1;y<=ny;y++) lx[x]=max(lx[x],map[x][y]); } for(int x=1;x<=nx;x++) { for(int y=1;y<=ny;y++) slock[y]=INF; while(1) { memset(visx,false,sizeof(visx)); memset(visy,false,sizeof(visy)); if(DFS(x)) break; int d=INF; for(int i=1;i<=ny;i++) { if(!visy[i]&&slock[i]<d) d=slock[i]; } for(int i=1;i<=nx;i++) { if(visx[i]) lx[i]-=d; } for(int i=1;i<=ny;i++) { if(visy[i]) ly[i]+=d; else slock[i]-=d; } } } int ans = 0; for(int i = 1;i <= ny; i++) ans += map[match[i]][i]; printf("%d ",-ans); } int main() { int t; scanf("%d",&t); while(t--) { getmap(); KM(); } return 0; }