hdu 3488

Tour

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


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
最小权匹配。      KM模板
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 9999999
#define maxn 205
int lx[maxn],ly[maxn];
int w[maxn][maxn];
int Match[maxn],visx[maxn],visy[maxn];
int slack[maxn];
int ans,n,m;
bool findPath(int x)
{
    int tmp;
    visx[x]=1;
    for(int y=1; y<=n; y++)
    {
        if(visy[y])continue;
        if(w[x][y]==lx[x]+ly[y])
        {
            visy[y]=1;
            if(!Match[y]||findPath(Match[y]))
            {
                Match[y]=x;
                return true;
            }
        }
        else
            slack[y]=min(slack[y],w[x][y]-lx[x]-ly[y]);
    }
    return false;
}
void km()
{
    memset(Match,0,sizeof(Match));
    memset(ly,0,sizeof(ly));
    for(int i=1; i<=n; i++)
    {
        lx[i]=INF;
        for(int j=1; j<=n; j++)
            lx[i]=min(lx[i],w[i][j]);
    }
    for(int x=1; x<=n; x++)
    {
        for(int i=1; i<=n; i++)
            slack[i]=INF;
        while(true)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(findPath(x))break;
            int tmp=INF;
            for(int i=1; i<=n; i++)
            {
                if(!visy[i])
                {
                    if(tmp>slack[i])
                        tmp=slack[i];
                }
            }
            if(tmp==INF)return ;
            for(int i=1; i<=n; i++)
            {
                if(visx[i])lx[i]+=tmp;
                if(visy[i])ly[i]-=tmp;
            }
        }
    }
}
int main()
{
    int cas;
    cin>>cas;
    while(cas--)
    {
        int a,b,c;
        cin>>n>>m;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                w[i][j]=INF;
            }
        for(int i=0; i<m; i++)
        {
            cin>>a>>b>>c;
            if(w[a][b]>c)
                w[a][b]=c;
        }
        km();
        int ans=0;
        for(int i=1; i<=n; i++)
            if(Match[i])
                ans+=w[Match[i]][i];
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(hdu 3488)