HDU 1853 Cyclic Tour[有向环最小权值覆盖]

Cyclic Tour

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


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   |   We have carefully selected several similar problems for you:   1533  3395  3315  1565  2448 
 

题意:
  给你一个 N 个顶点 M 条边的带权有向图, 要你把该图分成 1 个或多个不相交的有向环. 且所有点都只被一个有向环覆盖.

  问你该有向环所有权值的总和最小是多少?(保证有解)

解析:

  任意类似的【有向环最小权值覆盖】问题,都可以用最小费用流来写。
  由于题目中要求每个点最多走一次,为了防止走多次的发生,我们要把每个点 i 拆成左部点i和右部点i+n两个点。

具体建图如下:

  1、S向各点连<1,0>(前者表示容量,后者表示花费)
  2、各点向T连<1,0>
  3、如果i与j之间有连边,i向j+n连<1,w[i,j]>
最终如果最大流 == n 的话(即满流),那么最小费用就是我们所求,否则输出-1;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=30005;
const int M=2e5+5;
const int inf=0x3f3f3f3f;
struct edge{int v,cap,cost,next;}e[M<<1];int tot=1,head[N];
int n,m,cas,ans,res,S,T,dis[N],Prev[N],flow[N],q[N*2];
bool vis[N]; 
void add(int x,int y,int z,int cost){
    e[++tot].v=y;e[tot].cap=z;e[tot].cost=cost;e[tot].next=head[x];head[x]=tot;
    e[++tot].v=x;e[tot].cap=0;e[tot].cost=-cost;e[tot].next=head[y];head[y]=tot;
}
bool spfa(){
    for(int i=S;i<=T;i++) vis[i]=0,dis[i]=inf;
    int h=0,t=1;q[t]=S;dis[S]=0;flow[S]=inf;
    while(h!=t){
        int x=q[++h];vis[x]=0;
        for(int i=head[x];i;i=e[i].next){
            if(e[i].cap&&dis[e[i].v]>dis[x]+e[i].cost){
                dis[e[i].v]=dis[x]+e[i].cost;
                Prev[e[i].v]=i;
                flow[e[i].v]=min(flow[x],e[i].cap);
                if(!vis[e[i].v]){
                    vis[e[i].v]=1;
                    if(dis[e[i].v]<dis[x])
                        q[h--]=e[i].v;
                    else
                        q[++t]=e[i].v;
                }
            }
        }
    }
    return dis[T]!=inf;
}
void augment(){
    for(int i=T;i!=S;i=e[Prev[i]^1].v){
        e[Prev[i]].cap-=flow[T];
        e[Prev[i]^1].cap+=flow[T];
    }
    res+=flow[T];
    ans+=dis[T]*flow[T];
}
void init(){
    res=ans=0;tot=1;
    memset(head,0,sizeof head);
}
int main(){
    while(scanf("%d%d",&n,&m)==2){
        init();
        S=0,T=n<<1|1;
        for(int i=1;i<=n;i++) add(S,i,1,0),add(i+n,T,1,0);
        for(int i=1,x,y,w;i<=m;i++) x=read(),y=read(),w=read(),add(x,y+n,1,w);;
        while(spfa()) augment();
        if(res==n) printf("%d\n",ans);
        else printf("-1\n"); 
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/shenben/p/6378174.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值