hdu 1853 Cyclic Tour 费用流

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. 
         
 
题意:
给你n个城市和m条路,现在汤姆想要旅游所有的城市,而且每个城市只能经过一次,当然,旅游路上有一点的花费,现在问汤姆怎么走才能使总花费最小。
思路:
不要管中途有几个环只要他全部遍历肯定是没有入度为0和出度为0的点,所以找到流的和为n那么就是全部遍历完了,否则就输出-1

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
    int u,v,w,f,next;
} edge[2000000];
int head[200000],cnt,vis[200000],dis[200000],pre[200000];
int ans,ss,T,s;
void add(int u,int v,int w,int f)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].f=f;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].f=-f;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int SPFA()
{
    int i;
    memset(pre,-1,sizeof(pre));
    memset(vis,0,sizeof(vis));
    for(i=0; i<cnt; i++)
        dis[i]=INF;
        dis[s]=0;
    queue<int>q;
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            if(edge[i].w>0&&dis[edge[i].v]>dis[u]+edge[i].f)
            {
                dis[edge[i].v]=dis[u]+edge[i].f; 
                pre[edge[i].v]=i;
                if(!vis[edge[i].v])
                {
                    vis[edge[i].v]=1;
                    q.push(edge[i].v);
                   
                }
            }
        }
    }
    if(pre[T]==-1)
        return 0;
    return 1;
}
void MincostMaxFlow()
{
     ans=0,ss=0;
    while(SPFA())
    {
        int maxl=INF;
        for(int p=pre[T];p!=-1;p=pre[edge[p].u])
        {
            maxl=min(maxl,edge[p].w);
        }
         for(int p=pre[T];p!=-1;p=pre[edge[p].u])//对于为什么是pre[edge[p].u]其实就是相当于一段线找到他的后面然后看看谁与他的前面连接再找就行了
        {
            edge[p].w-=maxl;
            edge[p^1].w+=maxl;
        }
       ans+=maxl*dis[T];  //这个贷方要注意
       ss+=maxl;
    }
}
int main()
{
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        int u,v,w;
        s=0;
        T=2*n+1;
        memset(head,-1,sizeof(head));
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            add(u,n+v,1,w);
        }
        for(int i=1; i<=n; i++)
        {
            add(s,i,1,0);
            add(n+i,T,1,0);
        }
        MincostMaxFlow();
        if(ss==n)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值