码迷,mamicode.com
首页 > 其他好文 > 详细

(KM) hdu 1853

时间:2015-03-19 18:18:02      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

Cyclic Tour

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


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
 
 
裸KM。。。。/唯一要注意的是判断重边
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define INF 1<<30
int n,m,link[105],w[105][105],visx[105],visy[105],lx[105],ly[105],slack[105];
int dfs(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]==-1||dfs(link[y]))
            {
                link[y]=x;
                return 1;
            }
        }
        else if(slack[y]>t)
            slack[y]=t;
    }
    return 0;
}
int km()
{
    memset(link,-1,sizeof(link));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
    {
        lx[i]=-INF;
        for(int j=1;j<=n;j++)
            lx[i]=max(lx[i],w[i][j]);
    }
    for(int x=1;x<=n;x++)
    {
        for(int i=1;i<=n;i++)
            slack[i]=INF;
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(dfs(x))
                break;
            int d=INF;
            for(int i=1;i<=n;i++)
            {
                if(!visy[i]&&d>slack[i])
                    d=slack[i];
            }
            for(int i=1;i<=n;i++)
            {
                if(visx[i])
                    lx[i]-=d;
            }
            for(int i=1;i<=n;i++)
            {
                if(visy[i])
                    ly[i]+=d;
                else
                    slack[i]-=d;
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)
        res+=w[link[i]][i];
    return res;
}
int main()
{
    int u,v,p;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        bool flag=true;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                w[i][j]=-INF;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&p);
            if(-p>w[u][v])
                w[u][v]=-p;
        }
        int ans=km();
        for(int i=1;i<=n;i++)
        {
            if(link[i]==-1||w[link[i]][i]==-INF)
            {
                flag=false;
                break;
            }
        }
        if(!flag)
            printf("-1\n");
        else
            printf("%d\n",-ans);
    }
    return 0;
}

  

(KM) hdu 1853

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4350976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!