poj2135 Farm Tour -最小费用流

来源:互联网 发布:网络写作软件哪个好 编辑:程序博客网 时间:2024/06/05 05:49
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 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6

 

题目大意:

FJ带朋友参观自己的农场,从自己的房子出发到农场,再从农场返回自己的房子,要求去回不走同一条路。房子的点数为1,农场为n,在1到n之间有很多点,给出n个顶点,m条边,然后m行每行有三个数,a,b,c代表a到c的路径长度为c,并且a到b是无向边,现在要求从1点到n点在从n点返回1点的最短路

解题思路:

最容易想到的是先找一条最短路,然后把这条最短路去掉,然后再找一条最短路,但这样是不对的。

这样,我们假设一个超级源点,它到起点的 有向边 的容量为2,费用为0,一个超级汇点,终点到它的有向边的容量为2,费用为0,然后起点到各个点双向边的容量均为1,各个点到终点的双向边的容量也均为1。这样,由于各个点到终点的容量最多是1,保证了是由两条路进入的终点,又由于每个点之间容量都是1,保证了一条边不会走两次,这样的最小费用流就是所求的费用了。

关于超级源点和汇点其实可以不用设置,逻辑上认为它存在就可以了,详见代码main函数。

代码

#include <iostream>#include <cstdio>#include <vector>#include <cstring>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1000+10;struct edge{    int to,cap,cost,rev;};vector <edge> G[maxn];int dist[maxn];int prevv[maxn],preve[maxn];int V;void add_edge(int from,int to,int cap,int cost){    G[from].push_back((edge){to,cap,cost,G[to].size()});    G[to].push_back((edge){from,0,-cost,G[from].size()-1});}int min_cost_flow(int s,int t,int f){    int res = 0;    while(f>0)    {        memset(dist,INF,sizeof(dist));        dist[s] = 0;        bool update = true;        while(update)        {            update = false;            for(int v=0;v<V;++v)            {                if(dist[v]==INF) continue;                for(int i=0;i<G[v].size();++i)                {                    edge &e=G[v][i];                    if(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;                        update = 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;}int m;int main(){    int a,b,c;    scanf("%d%d",&V,&m);    int s=0,t=V-1;    for(int i=0;i<m;++i)    {        scanf("%d%d%d",&a,&b,&c);        add_edge(a-1,b-1,1,c);        add_edge(b-1,a-1,1,c);    }    printf("%d\n",min_cost_flow(s,t,2));    return 0;}


 

 

1 0
原创粉丝点击