POJ 2135 Farm Tour

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11534 Accepted: 4270

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,求两次最小路径之和,走过的路不能再走,建双向图,需在源点和汇点的流量设为2,其余为1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#define MAX 100001
#define inf 99999999
using namespace std;
struct node
{
    int u,v,w,f;
    int next;
}e[MAX];
int S,T,n,m,cnt;
int pre[MAX],head[MAX],dis[MAX],vis[MAX];
void add(int u,int v,int f,int w)
{
    e[cnt].u=u;
    e[cnt].v=v;
    e[cnt].f=f;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]= cnt++;

    e[cnt].u=v;
    e[cnt].v=u;
    e[cnt].f=0;
    e[cnt].w=-w;
    e[cnt].next=head[v];
    head[v]= cnt++;

}
int spfa()
{
    for(int i=0;i<=T;i++)
    {
        dis[i]=inf;
        vis[i]=0;
        pre[i]=-1;
    }
    vis[S]=1;
    dis[S]=0;
    queue<int >q;
    q.push(S);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        vis[t]=0;
        for(int r=head[t];r!=-1;r=e[r].next)
        {
            if(e[r].f && dis[e[r].v] > dis[t] + e[r].w)
            {
                dis[e[r].v] = dis[t] + e[r].w;
                pre[e[r].v]=r;
                if(!vis[e[r].v])
                {
                    vis[e[r].v]=1;
                    q.push(e[r].v);
                }
            }
        }
    }
    if(pre[T]==-1)
        return 0;
    return 1;
}
int minliu()
{
    int ans=0;
    while(spfa())
    {
        int max1=inf;
        for(int t=pre[T];t!=-1;t=pre[e[t].u])
            max1=min(max1,e[t].f);
         for(int t=pre[T];t!=-1;t=pre[e[t].u])
         {
             e[t].f-=max1;
             e[t+1].f+=max1;
             ans+=max1*e[t].w;
         }
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        S=0;
        T=n+1;
        add(S,1,2,0);
        add(n,T,2,0);
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,1,c);
            add(b,a,1,c);
        }
        printf("%d\n",minliu());
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值