poj 2135 Farm Tour(最小费用最大流模板)

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14530 Accepted: 5540

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

题意:先从第一个点到第n个点,再从第n个点到第一个点,同一条路只能走一遍,问最短路是多少?

思路:因为同一条路只能走一遍,我们可以用网络流来做,每条路的流量都是1,这样就只能走一次了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1010
#define M 10010
#define INF 0x3f3f3f3f
struct Node
{
    int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用
}edge[M<<2];
int cnt,head[N];
int vis[N],d[N],pp[N];
int sumflow;///最大流量总和
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int from,int to,int cap,int cost)
{
    edge[cnt].from=from;edge[cnt].to=to;edge[cnt].cost=cost;edge[cnt].cap=cap;
    edge[cnt].next=head[from];head[from]=cnt++;
    edge[cnt].from=to;edge[cnt].to=from;edge[cnt].cost=-cost;edge[cnt].cap=0;
    edge[cnt].next=head[to];head[to]=cnt++;///存反向边
}
int spfa(int s,int t,int n)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
    for(int i=0;i<=n;i++)
        d[i]=INF;
    d[s]=0;
    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)
        {
            int v=edge[i].to;
            if(edge[i].cap>0&&d[v]>d[u]+edge[i].cost)
            {
                d[v]=d[u]+edge[i].cost;
                pp[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(d[t]==INF) return 0;///找不到一条到终点的路
    return 1;
}
int MCMF(int s,int t,int n)
{
    int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量
    while(spfa(s,t,n))///找当前的最短路
    {
        minflow=INF+1;
        for(int i=pp[t];i!=-1;i=pp[edge[i].from])
            minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
        flow+=minflow;///总流量加上最小流量
        for(int i=pp[t];i!=-1;i=pp[edge[i].from])
        {
            edge[i].cap-=minflow;///当前边减去最小流量
            edge[i^1].cap+=minflow;///反向边加上最小流量
        }
        mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)
    }
    sumflow=flow;
    return mincost;
}
int main()
{
    int n,m;
    int from,to,cost;
    while(~scanf("%d %d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
        scanf("%d %d %d",&from,&to,&cost);
        addedge(from,to,1,cost);
        addedge(to,from,1,cost);
        }
        int S=0,T=n+1;
        addedge(S,1,2,0);
        addedge(n,T,2,0);
        int ans=MCMF(S,T,T);///流量为2保证只会走两次,如果是1到n有可能走多次
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值