hdu 1853 Cyclic Tour(费用流OR二分图最佳匹配,5级)

O - Cyclic Tour
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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.  
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=234;
const int oo=1e9;
bool vis[mm];
int head[mm],edge,work[mm],dis[mm],src,dest,node;
class Edge
{
  public:int v,next,flow,cost;
}e[mm*mm*2];
void data()
{
  clr(head,-1);edge=0;
}
void add(int u,int v,int _flow,int _cost)
{
  e[edge].v=v;e[edge].flow=_flow;e[edge].cost=_cost;e[edge].next=head[u];head[u]=edge++;
  e[edge].v=u;e[edge].flow=0;e[edge].cost=-_cost;e[edge].next=head[v];head[v]=edge++;
}
bool spfa()
{
  FOR(i,0,node)dis[i]=oo,vis[i]=0;
  queue<int>Q;work[src]=work[dest]=-1;
  Q.push(src);int u,v;dis[src]=0;
  while(!Q.empty())
  {
    u=Q.front();Q.pop();vis[u]=0;
    for(int i=head[u];~i;i=e[i].next)
    {
      v=e[i].v;
      if(e[i].flow&&dis[v]>dis[u]+e[i].cost)
      {
        dis[v]=dis[u]+e[i].cost;work[v]=i^1;//path
        if(vis[v])continue;vis[v]=1;Q.push(v);
      }
    }
  }
  //FOR(i,0,node)printf("d=%d %d\n",i,dis[i]);
  return work[dest]!=-1;
}
int max_spfa(int& many)
{
  int ret=0,num;
  while(spfa())
  {
    num=oo;//puts("++");
    for(int i=work[dest];~i;i=work[e[i].v])
    {
      if(e[i^1].flow<num)num=e[i^1].flow;
    }
    ret+=num*dis[dest];many+=num;
    for(int i=work[dest];~i;i=work[e[i].v])
      e[i^1].flow-=num,e[i].flow+=num;
  }
  return ret;
}
int n,m;
int main()
{ int a,b,c;
  while(~scanf("%d%d",&n,&m))
  {
    data();src=0;dest=n+n+1;node=n+n+2;
    FOR(i,1,n)add(src,i,1,0),add(i+n,dest,1,0);
    FOR(i,1,m)
    {
      scanf("%d%d%d",&a,&b,&c);
      add(a,b+n,1,c);
    }
    int ret=0,ans;
    ans=max_spfa(ret);
    //cout<<ret<<endl;
    if(ret==n)
      printf("%d\n",ans);
    else printf("-1\n");
  }
  return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值