poj 2135 Farm Tour(费用流)

传送门:http://poj.org/problem?id=2135

                                                                           Farm Tour

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20591 Accepted: 7929

Description

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.

Input

* 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.

Output

A single line containing the length of the shortest tour.

Sample Input

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

Sample Output

6

这道题的题意是,在一个无向图中,从1点找出一条路径使到达n点的距离最短,并在从n点走不同的路回到1点。

乍一看这是个最短路的问题,只不过是要求两次,并且不能重复走相同的路径。

所以我们总不能求两次最短路吧,再看一下题,从n点回到1点和从1点到n点其实是一样的,所以可以等价为求出最短的两条从1点到n点距离最短的路径,那么是不是可以看成这个图中,最大流量为2呢?并且不能走重复的路,那么每条边的流量设为1不就好了?而花费就是每条边的长度。

这样问题就转化成了求出一个无向图中最大流为2,每条边的流量都为1的最小费用。这样的话我们再加上两个点,源点s和汇点t,设置成s->1,流量为2,花费0,n->t,流量为2,花费为0。这样图就构建完成了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e6+10;
int n,m,tot,s,t,mincost,maxflow;
int head[N],dis[N],pre[N],path[N],vis[N];          //dis数组为求出的到达一点的最小费用
struct node
{
    int v;
    int w;
    int next;
    int cost;
    node()
    {
        v=0;
        w=0;
        next=-1;
        cost=0;
    }
}e[N];
void add(int u,int y,int z,int co)
{
    e[++tot].v=y;
    e[tot].w=z;
    e[tot].next=head[u];
    e[tot].cost=co;
    head[u]=tot;

    e[++tot].v=u;
    e[tot].w=0;
    e[tot].cost=-co;
    e[tot].next=head[y];
    head[y]=tot;
}
bool spfa()
{
    queue<int>q;
    met(dis,inf);
    met(pre,0);
    met(path,0);
    met(vis,false);
    vis[s]=true;
    dis[s]=0;
    while(!q.empty())
        q.pop();
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(dis[v]>e[i].cost+dis[u]&&e[i].w>0){
                dis[v]=e[i].cost+dis[u];
                pre[v]=u;         //前一个点
                path[v]=i;     //前一个边
            if(!vis[v]){
                vis[v]=true;
                q.push(v);
            }
          }
        }
    }
    if(dis[t]==inf)
        return false;
    return true;
}
void minf()
{
     maxflow=0;
     mincost=0;
    while(spfa()){
        int f=inf;
        for(int i=t;i!=s;i=pre[i]){
            if(e[path[i]].w<f)
                f=e[path[i]].w;
        }
        mincost+=f*dis[t];
        maxflow+=f;
        for(int i=t;i!=s;i=pre[i]){
            e[path[i]].w-=f;          //正向边减少容量
            e[path[i]^1].w+=f;         //反向边增加流量
        }
    }
}
void init()
{
    tot=-1;
    met(head,-1);
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
            int x,y,f;
            init();
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&f);
            add(x,y,1,f);
            add(y,x,1,f);
        }
        s=n+1;
        t=s+1;
        add(s,1,2,0);
        add(n,t,2,0);
        minf();
        printf("%d\n",mincost);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值