hdu 1853 Cyclic Tour //km

Cyclic Tour

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


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
 

Recommend
lcy

 

 

 

#include<cstdio>

#include<cstring>

#include<algorithm>

const int inf=1<<28;

using namespace std;

int g[505][505],lx[505],ly[505];//顶点标号

bool sx[505],sy[505];//是否已经搜索过

int link[505],n,stack[505];

bool path(int k)//从x[k]寻找增广路

{

    sx[k]=true;

    for(int i=1; i<=n; i++)

    {

        if(sy[i])  continue;

        int t=ly[i]+lx[k]-g[k][i];

        if(t==0)

        {

            sy[i]=1;

            if(link[i]==-1||path(link[i]))

            {

                link[i]=k;

                return true;

            }

        }

        else if(stack[i]>t) stack[i]=t;

    }

    return false;

}

int BestMatch()

{

    int d,sum;

    memset(ly,0,sizeof(ly));

    memset(link,-1,sizeof(link));

    for(int i=1; i<=n; i++)

    {

        lx[i]=-inf;

        for(int j=1; j<=n; j++)

            if(lx[i]<g[i][j]&&g[i][j]!=0)  lx[i]=g[i][j];

    }

    for(int k=1; k<=n; k++)

    {

        for (int i=1; i<=n; i++) stack[i]=inf;

        while(1)

        {

            memset(sx,0,sizeof(sx));

            memset(sy,0,sizeof(sy));

            if(path(k))  break;

            d=inf;

            for(int i=1; i<=n; i++)

                if (!sy[i]&&stack[i]<d) d=stack[i];

            for(int i=1; i<=n; i++)

                if(sx[i]) lx[i]-=d;

            for(int i=1; i<=n; i++)

                if(sy[i]) ly[i]+=d;

                else  stack[i]-=d;

        }

    }

    sum=0;

    for(int i=1; i<=n; i++)

    {

        if(link[i]==-1||g[link[i]][i]==-inf)  return -1;

        sum+=g[link[i]][i];

    }

    return -sum;

}

int main()

{

    int m;

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        for(int i=1;i<=n;i++)

          for(int j=1;j<=n;j++)

                  g[i][j]=inf;

        for(int i=1; i<=m; i++)

        {

            int x,y,c;

            scanf("%d%d%d",&x,&y,&c);

            g[x][y]=min(g[x][y],c);

        }

        for(int i=1; i<=n; i++)

            for(int j=1; j<=n; j++) g[i][j]=-g[i][j];

        printf("%d/n",BestMatch());

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值