HDU 3488 Tour(二分图最优匹配:有向环覆盖)

来源:互联网 发布:vb正则表达式语法 编辑:程序博客网 时间:2024/05/21 08:01

题意:给你一个N个顶点M条边的带权有向图,要你把该图分成1个或多个不相交的有向环.且所有定点都被有向环覆盖.问你该有向环所有权值的总和最小是多少?(保证有解)

思路:参照HDU1853


#include<cstdio>#include<cstring>#include<algorithm>#define INF 1e9using namespace std;const int maxn=200+5;struct Max_Match{    int n, W[maxn][maxn];    int Lx[maxn],Ly[maxn];    bool S[maxn],T[maxn];    int left[maxn];    bool match(int i)    {        S[i]=true;        for(int j=1;j<=n;j++)if(Lx[i]+Ly[j]==W[i][j] && !T[j])        {            T[j]=true;            if(left[j]==-1 || match(left[j]))            {                left[j]=i;                return true;            }        }        return false;    }    void update()    {        int a=1<<30;        for(int i=1;i<=n;i++)if(S[i])        for(int j=1;j<=n;j++)if(!T[j])            a=min(a, Lx[i]+Ly[j]-W[i][j]);        for(int i=1;i<=n;i++)        {            if(S[i]) Lx[i]-=a;            if(T[i]) Ly[i]+=a;        }    }    int solve(int n)    {        this->n=n;        memset(left,-1,sizeof(left));        for(int i=1;i<=n;i++)        {            Lx[i]=Ly[i]=0;            for(int j=1;j<=n;j++)                Lx[i]=max(Lx[i], W[i][j]);        }        for(int i=1;i<=n;i++)        {            while(true)            {                for(int j=1;j<=n;j++) S[j]=T[j]=false;                if(match(i)) break;                else update();            }        }        int ans=0;        for(int i=1;i<=n;i++)            ans += W[left[i]][i];        return -ans;    }}KM;int main(){    int T; scanf("%d",&T);    while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)            KM.W[i][j]=-INF;        while(m--)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            KM.W[u][v]= max(KM.W[u][v], -w);        }        printf("%d\n",KM.solve(n));    }    return 0;}

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

16 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 4
 

Sample Output

42
 


0 0
原创粉丝点击