Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3922 Accepted Submission(s): 1872
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
思路:完美匹配求最小值,用KM算法,将权值转化为负值
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<set>
#include<stack>
#include<algorithm>
#define bug printf("!!!!!\n");
#define maxn 400
#define INF 1<<30
using namespace std;
int n;
int weight[maxn][maxn];
int lx[maxn],ly[maxn];
bool sx[maxn],sy[maxn];
int match[maxn];
bool path(int x){
sx[x]=true;
for(int y=1;y<=n;y++){
if(!sy[y]&&lx[x]+ly[y]==weight[x][y]){
sy[y]=true;
if(match[y]==-1||path(match[y])){
match[y]=x;
return true;
}
}
}
return false ;
}
int KM(bool max_weight){
if(!max_weight){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
weight[i][j]=-weight[i][j];
}
}
}
for(int i=1;i<=n;i++){
ly[i]=lx[i]=0; //注意当前所有值为负
for(int j=1;j<=n;j++){
lx[i]=max(lx[i],weight[i][j]);
}
}
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++){
while(1){
memset(sx,0,sizeof(sx));
memset(sy,0,sizeof(sy));
if(path(i)){
break;
}
int inc=1<<30;
for(int i=1;i<=n;i++){
if(sx[i]){
for(int j=1;j<=n;j++){
if(!sy[j]&&(lx[i]+ly[j]-weight[i][j])<inc)
inc=lx[i]+ly[j]-weight[i][j];
}
}
}
if(inc==0)return -1;
for(int i=1;i<=n;i++){
if(sx[i])
lx[i]-=inc;
if(sy[i])
ly[i]+=inc;
}
}
}
int sum=0;
for(int i=1;i<=n;i++){
if(match[i]>=0)
sum+=weight[match[i]][i];
}
if(!max_weight){
sum=-sum;
}
return sum;
}
void solve()
{
int x,y,z,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
weight[i][j]=INF;
}
}
for(int i=0;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
weight[x][y]=min(weight[x][y],z);
}
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(sx,0,sizeof(sx));
memset(sy,0,sizeof(sy));
printf("%d\n",KM(0));
}
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int cas=1,t=1;
scanf("%d",&t);
while(t--){
// printf("Case %d: ",cas++);
solve();
}
}