poj 2135 Farm Tour 【最小费用流】

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16022   Accepted: 6208

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再回来不经过同一条路  所走的最短路程


即1到n的F=2的最小费用;



代码:

#include
#include
#include
using namespace std;
struct node{int to,cap,cost,rev;}OP;
vector P[1010];
int n,m,dist[1010],pre1[1010],pre2[1010],INF=36000000;
void ADD(int a,int b,int c)
{
    OP.cap=1;OP.to=b;OP.cost=c;OP.rev=P[b].size();
    P[a].push_back(OP);
    OP.cap=0;OP.to=a;OP.cost=-c;OP.rev=P[a].size()-1;
    P[b].push_back(OP);
}
int min_cost(int s,int t,int f)
{
    int res=0;
    while (f)
    {
        fill(dist,dist+n+1,INF);
        fill(pre1,pre1+n+1,-1);
        fill(pre2,pre2+n+1,-1);
        dist[s]=0;
        bool update=true;
        while (update)
        {
            update=false;
            for (int i=1;i<=n;i++)
            {
                if (dist[i]==INF) continue;
                for (int j=0;j0&&dist[i]+P[i][j].cost


你可能感兴趣的:(我的ACM成长历程---啦啦啦,网络流费用流,poj)