HDUOJ 3488 Tour

HDUOJ 3488 Tour

题目链接

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.

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.

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

比较巧妙的二分图最大权匹配问题~
有人可能会问,这不是求最小距离吗?我们只要将所有边权值取负即可,初始化将所有边权值赋 - i n f inf ,此时问题就转化为二分图最大权匹配问题了,套一个 K M KM 模板即可,注意答案最后取负输出,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=210;
const int inf=1e8;
int n,m,w[N][N],lx[N],ly[N],visx[N],visy[N],slack[N],link[N];
bool found(int x){
    visx[x]=1;
    for(int y=1;y<=n;y++){
        if(visy[y]) continue;
        int t=lx[x]+ly[y]-w[x][y];
        if(t==0){
            visy[y]=1;
            if(link[y]==0 || found(link[y])){
                link[y]=x;
                return 1;
            }
        }
        else if(slack[y]>t) slack[y]=t;
    }
    return 0;
}

void KM(){
    fill(lx,lx+N,0);
    fill(ly,ly+N,0);
    fill(link,link+N,0);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            if(lx[i]<w[i][j]) lx[i]=w[i][j];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++) slack[j]=inf;
        while(1){
            fill(visx,visx+N,0);
            fill(visy,visy+N,0);
            if(found(i)) break;
            int d=inf;
            for(int k=1;k<=n;k++)
                if(!visy[k] && d>slack[k]) d=slack[k];
            for(int k=1;k<=n;k++){
                if(visx[k]) lx[k]-=d;
                if(visy[k]) ly[k]+=d;
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++)
        if(w[link[i]][i]!=inf) ans+=w[link[i]][i];
    printf("%d\n",-ans);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        scanf("%d%d",&n,&m);
        fill(w[0],w[0]+N*N,-inf);
        int x,y,z;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&z);
            w[x][y]=max(w[x][y],-z);
        }
        KM();
    }
    return 0;
}
发布了479 篇原创文章 · 获赞 38 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/105681626