Farm Tour

 

题目描述

 

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

 

输入

 

* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

 

输出

 

A single line containing the length of the shortest tour.

 

样例输入

 

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

 

样例输出

 

6
http://218.5.5.242:9018/JudgeOnline/problem.php?cid=1070&pid=0
网络流裸题
将源点向点1连接一条费用0,流量2的路
将点n向汇点连接一条费用0,流量2的路
后跑费用流!
但原谅我英语不好
看别人翻译
结果做成了有向图。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define S 0
#define T n+1
#define MAXN 100005
#define INF 200000000
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;
}
int q[100001],top,tail;
int ans=0;
bool mark[MAXN];
int n,m,cnt=1,head[MAXN],d[MAXN],from[MAXN];
struct tu{
    int from,w,to,next,c;
}bian[100001];
void ins(int f,int t,int w,int c)
{
    bian[++cnt].next=head[f];
    head[f]=cnt;
    bian[cnt].to=t;
    bian[cnt].w=w;
    bian[cnt].c=c;
    bian[cnt].from=f;
}
void insw(int f,int t,int w,int c)
{ins(f,t,w,c);ins(t,f,0,-c);}
bool spfa()
{
    for(int i=1;i<=T;i++)d[i]=INF;
    top=tail=10005;q[top]=S;mark[S]=1;d[S]=0;
    while(top>=tail)
    {
        int u=q[tail++];
        for(int i=head[u];i;i=bian[i].next)
        if(bian[i].w&&d[u]+bian[i].c<d[bian[i].to])
        {
            d[bian[i].to]=d[u]+bian[i].c;from[bian[i].to]=i;
            if(!mark[bian[i].to])
            {
                if(d[bian[i].to]<d[q[tail]]) q[--tail]=bian[i].to;
                else q[++top]=bian[i].to; mark[bian[i].to]=1;
            }        
        }    
        mark[u]=0;
    }
    return d[T]!=INF;
}
void mcf()
{
    int minn=INF;
    for(int i=from[T];i;i=from[bian[i].from])
        minn=min(minn,bian[i].w);
    for(int i=from[T];i;i=from[bian[i].from])
    {
        ans+=bian[i].c;
        bian[i].w-=minn;
        bian[i^1].w+=minn;    
    }//SPFA费用流模板
}
int main()
{
    n=read();m=read();
    insw(S,1,2,0);
    for(int i=1;i<=m;i++)
    {
        int f=read(),t=read(),c=read();
        insw(f,t,1,c);    
         insw(t,f,1,c);  //要连两次
    }
    insw(n,T,2,0);
       while(spfa())mcf();
    printf("%d",ans);
    return 0;
}




 

 

转载于:https://www.cnblogs.com/0degreeofsail/p/6622422.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值