POJ2135:Farm Tour

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块地,其中约翰的家在1号地,而N号地有个很大的仓库。农场内有M条道路(双向通行),道路i连接着ai号地和bi号地,长度为ci。约翰希望按照从家里出发,经过若干块地后达到仓库,然后再返回家中的顺序带朋友参观。如果要求往返不能经过同一条道路两次,求参观路线总长度的最小值。

解题思路
该题可转化为一个最小费用流的问题。将每条边的容量设置为1,保证每边只经过一次。将流量设置为2,即求两条从s到t的路,并保证在s到t有流量为F(2)的流的前提下,费用(道路长度)*流量(1)的和最小。
代码如下:

#include
#include
#include
#define INF 0x3f3f3f3f  //很奇怪,虽然道路长度不大于35000,但是将INF设置为1000000答案错误。
using namespace std;
struct edge{int to,cap,cost,rev;};
int V;
vector  G[1005];
int dist[1005];
int prevv[1005],preve[1005];
int N,M;
int a[10005],b[10005],c[10005];
 void add_edge(int from,int to,int cap,int cost)
 {
    edge temp;
    temp.to=to;
    temp.cap=cap;
    temp.cost=cost;
    temp.rev=G[to].size();
    G[from].push_back(temp);

    temp.to=from;
    temp.cap=0;
    temp.cost=-cost;
    temp.rev=G[from].size()-1;
    G[to].push_back(temp);
 }
 int min_cost_flow(int s,int t,int f)
 {
    int res=0;
    while(f>0)
    {
        for(int i=0;i<1005;i++)
        dist[i]=INF;

        dist[s]=0;
        bool updata=true;
        while(updata)
        {
            updata=false;
            for(int v=0;vif(dist[v]==INF)
                continue;
                for(int i=0;iif(e.cap>0&&dist[e.to]>dist[v]+e.cost)
                    {
                        dist[e.to]=dist[v]+e.cost;
                        prevv[e.to]=v;
                        preve[e.to]=i;
                        updata=true;
                    }
                }
            }
        }
        if(dist[t]==INF)
        return -1;

        int d=f;
        for(int v=t;v!=s;v=prevv[v])
        {
            d=min(d,G[prevv[v]][preve[v]].cap);
        }
        f-=d;
        res+=d*dist[t];
        for(int v=t;v!=s;v=prevv[v])
        {
            edge &e=G[prevv[v]][preve[v]];
            e.cap-=d;
            G[v][e.rev].cap+=d;
        }
    }
    return res;
 }
 void solve()
 {
    int s=0,t=N-1;
    V=N;
    for(int i=0;i1,b[i]-1,1,c[i]);
        add_edge(b[i]-1,a[i]-1,1,c[i]);
    }
    cout<2)<int main()
 {
    int i;
    while(cin>>N>>M)
    {
        for(i=0;icin>>a[i]>>b[i]>>c[i];
        } 


        solve();
    }
    return 0;
 }

你可能感兴趣的:(图论)