poj 2135 Farm Tour

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16367 Accepted: 6320
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
Source

USACO 2003 February Green


【分析】
最小费用最大流(费用流)板子…不过建图需要一定技巧
题意:给一个图,从1走到n,再从n走到1,且每条边只能走一边所走过的最小边权。

由于每条边只能走一次,所以给每条边设定流量为1,将点1与源点s连接,费用为0,流量为2,将n与汇点t连接,费用为0,流量为2。

跑一边费用流就好了。


【代码】

//poj 2135 Farm Tour 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 1e9+7
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=10005<<2;
queue <int> q;
int n,m,s,t,cnt,ans;
bool vis[mxn];
int head[1005],dis[1005],pre[1005];
struct node {int from,to,next,d,flow;} f[mxn];
inline void add(int u,int v,int d,int flow)
{
    f[++cnt].to=v,f[cnt].from=u,f[cnt].next=head[u],f[cnt].d=d,f[cnt].flow=flow,head[u]=cnt;
    f[++cnt].to=u,f[cnt].from=v,f[cnt].next=head[v],f[cnt].d=-d,f[cnt].flow=0,head[v]=cnt;
}
inline void spfa()
{
    int i,u,v,d,flow;
    memset(dis,0x3f,sizeof dis);
    memset(pre,-1,sizeof pre);
    M(vis);
    q.push(s);
    dis[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        for(i=head[u];i;i=f[i].next)
        {
            v=f[i].to,d=f[i].d,flow=f[i].flow;
            if(dis[v]>dis[u]+d && flow>0)
            {
                dis[v]=dis[u]+d;
                pre[v]=i;   //记录前驱 
                if(!vis[v]) vis[v]=1,q.push(v);
            }
        }
    }
}
inline void maxflow()
{
    int i,u,v,d;
    spfa();
    while(pre[t]!=-1)
    {
        int tmp=inf;
        for(i=pre[t];i!=-1;i=pre[f[i].from])
          tmp=min(tmp,f[i].flow);
        ans+=dis[t]*tmp;
        for(i=pre[t];i!=-1;i=pre[f[i].from])
        {
            f[i].flow-=tmp;
            if(i&1) f[i+1].flow+=tmp;
            else f[i-1].flow+=tmp;
        }
        spfa();
    }
}
int main()
{
    int i,j,u,v,d;
    scanf("%d%d",&n,&m);
    s=0,t=n+1;
    while(m--)
    {
        scanf("%d%d%d",&u,&v,&d);
        add(u,v,d,1);add(v,u,d,1);
    }
    add(s,1,0,2),
    add(1,s,0,2),
    add(n,t,0,2),
    add(t,n,0,2);
    maxflow();
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值