Tour
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3198 Accepted Submission(s): 1541
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
zhouzeyong
题意:N个点,M条边的有向图,边都是正权,求使每个点至少属于一个环的路径的最小权和
解题思路:二分图完美匹配,二分图最大权匹配,把每个边的权值变负值
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int N=210;
int nx,ny,m,n;//两边的点数
int g[N][N];//二分图描述
int linker[N],lx[N],ly[N];//y中各点匹配状态,x,y中的点标号
int slack[N];
bool visx[N],visy[N];
bool DFS(int x)
{
visx[x]=true;
for(int y=0; y<ny; y++)
{
if(visy[y])continue;
int tmp = lx[x]+ly[y]-g[x][y];
if(tmp==0)
{
visy[y]=true;
if(linker[y]==-1||DFS(linker[y]))
{
linker[y]=x;
return true;
}
}
else slack[y]=min(tmp,slack[y]);
}
return false;
}
void KM()
{
memset(linker,-1,sizeof(linker));
memset(ly,0,sizeof(ly));
for(int i=0;i<nx;i++)
{
lx[i]=-INF;
for(int j=0;j<ny;j++)
lx[i]=max(lx[i],g[i][j]);
}
for(int x=0;x<nx;x++)
{
for(int i=0;i<ny;i++) slack[i]=INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x)) break;
int d=INF;
for(int i=0;i<ny;i++)
if(!visy[i]) d=min(d,slack[i]);
for(int i=0;i<nx;i++)
if(visx[i]) lx[i]-=d;
for(int i=0;i<ny;i++)
{
if(visy[i]) ly[i]+=d;
else slack[i]-=d;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(g,-INF,sizeof g);
scanf("%d %d",&n,&m);
int u,v,w;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
u--,v--;
g[u][v]=max(g[u][v],-w);
}
nx=ny=n;
KM();
int ans=0;
for(int i=0;i<ny;i++)
if(linker[i]!=-1) ans+=g[linker[i]][i];
printf("%d\n",-ans);
}
return 0;
}