POJ2135

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13644 Accepted: 5169

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
【题意】:农夫要带着客人去参观自己的农场的谷仓,首先从家到谷仓,然后从谷仓回到家中,要求两次走的路径不同,而且距离之和最小
【思路】:纯粹的最小费用最大流问题,而这道题也可以锻炼最短路的能力;不多说,首先设置一个超级源点0,一个超级汇点N+1,因为是1到N的双源最短路,所以直接将0与1相连,cap为2,N与N+1相连,cap为2,因为是无向图,所以每条边要连两次。

【给一组测试数据】:
10 15
7 1 13784
6 1 31692
4 9 16318
5 10 521
10 3 16420
5 2 11817
6 4 29070
8 5 13614
2 9 17168
8 1 19260
1 2 6076
2 3 1038
3 6 12917
2 6 17815
10 4 26493

输出:
56929

【代码】:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define INF 0xffff777
#define ll long long
using namespace std;
int n,m;
struct edge{
    int y,c,l,r;
};
vector<edge> g[10005];
int dis[10005];
int prev[10005],pree[10005];


void add(int x,int y,int l){
    g[x].push_back((edge){y,1,l,g[y].size()});
    g[y].push_back((edge){x,0,-l,g[x].size()-1});
}
ll miflow(int s,int t){
    ll res=0;
    while(1){
        fill(dis,dis+n+2,INF);
        dis[s]=0;
        bool update=1;
        while(update){
            update =0;
            for(int v=s;v<t;++v){
                //printf(" dis 4= %d\n",dis[4]);
                if(dis[v]==INF) continue;
                for(int i=0;i<g[v].size();++i){
                    edge& e=g[v][i];
                   //printf("v= %d %d\n",v,i);
                    if(e.c>0&&dis[e.y]>dis[v]+e.l){
                         //printf("v= %d %d e,y=%d\n",v,i,e.y);
                        dis[e.y]=dis[v]+e.l;
                        prev[e.y]=v;
                        pree[e.y]=i;
                        update=1;
                    }
                }
            }
        }
        //printf("%d %d\n",dis[5],g[4][g[4].size()-1].c);
        if(dis[t]==INF) break;
        int a=INF;

         for(int v=t; v!=s; v=prev[v])
            if( g[prev[v]][pree[v]].c< a )
                a = g[prev[v]][pree[v]                                                                  ].c;
        for(int v=t; v!=s; v=prev[v])
        {
            //printf("%d %d\n",2,g[2][4].l);
            edge& e=g[prev[v]][pree[v]];
            g[v][e.r].c += a;
            e.c -= a;
        }
        res += dis[t]*a;
    }
    return res;
}
int main()
{
    scanf("%d%d",&n,&m);
    int x,y,l;
    for(int i=1;i<=m;++i){
        scanf("%d%d%d",&x,&y,&l);
        add(x,y,l);
        add(y,x,l);
    }
     g[0].push_back((edge){1,2,0,g[1].size()});
    g[1].push_back((edge){0,0,0,g[0].size()-1});

     g[n].push_back((edge){n+1,2,0,g[n+1].size()});
    g[n+1].push_back((edge){n,0,0,g[n].size()-1});
    printf("%lld\n",miflow(0,n+1));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值