HDU(1853),最小权匹配,KM

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

Cyclic Tour

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 2289    Accepted Submission(s): 1162

Problem Description
There are N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy that, each cycle contain at least two cities, and each city belongs to one cycle exactly. Tom wants the total length of all the tours minimum, but he is too lazy to calculate. Can you help him?
 
Input
There are several test cases in the input. You should process to the end of file (EOF).
The first line of each test case contains two integers N (N ≤ 100) and M, indicating the number of cities and the number of roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there is a road from city A to city B, whose length is C. (1 ≤ A,B ≤ N, A ≠ B, 1 ≤ C ≤ 1000).
 
Output
Output one number for each test case, indicating the minimum length of all the tours. If there are no such tours, output -1.
Sample Input
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 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
Sample Output
42 -1
Hint
In the first sample, there are two cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
 
Author
RoBa@TJU
 
Source
 
 
题意:给你n个城市和m条路,现在汤姆想要旅游所有的城市,而且每个城市只能经过一次,当然,旅游路上有一点的花费,现在问汤姆怎么走才能使总花费最小。
分析:刚开始,被题目误导了,说什么跑环啊什么的,好多好多的环啊,我越看越懵逼了。感觉像最小生成树,又没有回来,感觉像一个跑两遍的最短路,一个点又只能经过一次。
其实可以这样思考:
由于图是单向的,每条边的两个点,分别放到入点的集合,和出点的集合,我然后做最小权匹配,然后扫一遍出点,因为要成环,必然每个点既是入点,也是出点。要是每个出点都匹配到了,不管你匹配的点是什么,反正这个环,或者几个环都匹配好了,就返回最小权值,否则返回-1.
然后这里要注意重边,还有,匹配好了,也可能是之前的无穷大,所以加一个判断。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 330;
const int INF = 0x3f3f3f3f;

int N,NX,NY;
int match[MAXN],lx[MAXN],ly[MAXN],slack[MAXN];
int visx[MAXN],visy[MAXN];
int Map[MAXN][MAXN];

bool FindPath(int u)
{
    visx[u] = true;
    for(int i = 1; i <= NY; ++i)
    {
        if(visy[i])
            continue;
        int temp = lx[u] + ly[i] - Map[u][i];
        if(temp == 0)
        {
            visy[i] = true;
            if(match[i] == -1 || FindPath(match[i]))
            {
                match[i] = u;
                return true;
            }
        }
        else if(slack[i] > temp)
            slack[i] = temp;
    }
    return false;
}


int KM()
{
    memset(ly,0,sizeof(ly));
    memset(lx,-1,sizeof(lx));
    memset(match,-1,sizeof(match));
    for(int i = 1; i <= NX; ++i)
    {
        for(int j = 1; j <= NY; ++j)
            if(Map[i][j] > lx[i])
                lx[i] = Map[i][j];
    }

    for(int i = 1; i <= NX; ++i)
    {
        for(int j = 1; j <= NY; ++j)
            slack[j] = INF;
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(FindPath(i))
                break;
            int d = INF;
            for(int j = 1; j <= NY; ++j)
                if(!visy[j] && d > slack[j])
                    d = slack[j];
            for(int j = 1; j <= NX; ++j)
                if(visx[j])
                    lx[j] -= d;
            for(int j = 1; j <= NY; ++j)
                if(visy[j])
                    ly[j] += d;
                else
                    slack[j] -= d;
        }
    }

    int res = 0;
    int cnt = 0;
    for(int i = 1; i <= NY; ++i)
        if(match[i]!=-1&&Map[match[i]][i]!=-INF)
        {
            res += Map[match[i]][i];
            cnt++;
        }
    if(cnt<NY) return -1;
    return -res;
}


int main()
{

    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                Map[i][j] = -INF;
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            Map[u][v] = max(Map[u][v],-w);
        }
        NX = NY = n;
        printf("%d
",KM());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/5773172.html