HDU3488:Tour(KM算法)

Tour

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4279    Accepted Submission(s): 2041

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3488

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
 
题意:
我又读错题了...这题我感觉有点歧义,题目中说的是一条路线包含至少一个环,每个城市在一条路线中,反正我是这么理解的...
后来才发现,这个题路线可以多条,但是每一条都至少一个环,然后就是要求到达所有的城市,问最短的路程是多少。
是我英语水平不行么...
 
题解:
题目有几个要求,1.到达所有点;2.路程最短;3.环。
通过思考发现,求二分图的最小权匹配便可以满足以上几个条件,构造二分图时我们是需要把一个点拆分为出度点和入度点,因为每个点只有一次,所以只有一个入度一个出度。对于环中的所有点也是这样。
所以当所有点都为匹配点时,即可满足环的要求。
至于最小权,将边置为其负数然后用KM求最大匹配即可。
 
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define INF 1e9+7
#define mem(x) memset(x,0,sizeof(x))
using namespace std;

const int N = 415 , M = 30005 ;
int t,n,m,ans;
int match[N],w[N][N],l[N],r[N],slack[N],visx[N],visy[N];

void init(){
    mem(match);mem(r);ans=0;
    for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) w[i][j]=INF;
    for(int i=1;i<=N;i++) l[i]=-INF;
}

int dfs(int x){
    visx[x]=1;
    for(int i=n+1;i<=2*n;i++){
        if(w[x][i]==INF || visy[i]) continue ;
        int tmp = l[x]+r[i]-w[x][i];
        if(!tmp){
            visy[i]=1;
            if(!match[i] || dfs(match[i])){
                match[i]=x;
                return 1;
            }
        }else{
            slack[i]=min(slack[i],tmp);
        }
    }
    return 0;
} 

void update(){
    int d=INF;
    for(int i=n+1;i<=2*n;i++) if(!visy[i]) d=min(d,slack[i]);
    for(int i=1;i<=n;i++) if(visx[i]) l[i]-=d;
    for(int i=n+1;i<=2*n;i++) if(visy[i]) r[i]+=d;else slack[i]-=d; 
}

int KM(){
    for(int i=1;i<=n;i++){
        fill(slack,slack+N,INF);
        while(1){
            mem(visx);mem(visy);
            if(dfs(i)) break;
            update();
        }
    }
    for(int i=n+1;i<=n*2;i++) ans+=w[match[i]][i];
    return ans;
}

int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=1,x,y,c;i<=m;i++){
            scanf("%d%d%d",&x,&y,&c);
            w[x][y+n]=min(w[x][y+n],c);
        }
        for(int i=1;i<=n;i++)
            for(int j=n+1;j<=2*n;j++)
                if(w[i][j]!=INF) w[i][j]=-w[i][j],l[i]=max(l[i],w[i][j]);
        printf("%d\n",-KM());
    }
    return 0;
} 

 

posted @ 2018-11-15 21:36  heyuhhh  阅读(160)  评论(1编辑  收藏  举报