POJ2135 Farm Tour

Posted SilverNebula

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2135 Farm Tour相关的知识,希望对你有一定的参考价值。

 
Time Limit: 2MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

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

 
 
POJ的对应页面突然进不去了,没法评测。
目前只确保过了样例
 
最小费用问题
 
 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<queue>
 8 using namespace std;
 9 const int INF=1000000;
10 const int mxn=2100;
11 int head[mxn],dis[mxn],pr[mxn];
12 bool inqu[mxn];
13 int n,m;
14 int s,t;
15 int ans;
16 int cnt=1;
17 struct edge{
18     int from,to,next,v,c;
19 }e[mxn*30];
20 void add_edge(int f,int t,int v,int c){
21     e[++cnt]=(edge){f,t,head[f],v,c};head[f]=cnt;
22     e[++cnt]=(edge){t,f,head[t],0,-c};head[t]=cnt;
23 }
24 bool SPFA(){
25     queue<int>q;
26     memset(inqu,false,sizeof(inqu));
27     for(int i=0;i<=t;i++)dis[i]=INF;
28     dis[s]=0;
29     inqu[s]=1;
30     q.push(s);
31     int i,j;
32     while(!q.empty()){
33         int u=q.front();q.pop();
34         inqu[u]=false;
35         for(i=head[u];i;i=e[i].next){
36             int v=e[i].to;
37             if(e[i].v && dis[u]+e[i].c<dis[v]){
38                 dis[v]=dis[u]+e[i].c;
39                 pr[v]=i;
40                 if(!inqu[v]){
41                     q.push(v);
42                     inqu[v]=true;
43                 }
44             }
45         }
46     }
47     return dis[t]!=INF;
48 }
49 void mcf(){
50     int i;
51     while(SPFA()){
52         int temp=INF;
53         for(i=pr[t];i;i=e[pr[i]].from)temp=min(temp,e[i].v);
54         ans+=dis[t]*temp;
55         for(i=pr[t];i;i=e[pr[i]].from){
56             e[i].v-=temp;
57             e[i^1].v+=temp;
58         }
59     }
60 }
61 int main(){
62     scanf("%d%d",&n,&m);
63     s=0;t=n+1;
64     int x,y,c;
65     for(int i=1;i<=m;i++){
66         scanf("%d%d%d",&x,&y,&c);
67         add_edge(x,y,1,c);
68         add_edge(y,x,1,c);
69     }
70     add_edge(s,1,2,0);
71     add_edge(n,t,2,0);
72     mcf();
73     printf("%d\n",ans);
74 }

 

以上是关于POJ2135 Farm Tour的主要内容,如果未能解决你的问题,请参考以下文章

POJ2135 Farm Tour

POJ 2135.Farm Tour 最小费用流

Farm Tour POJ - 2135 (最小费用流)

网络流(最小费用最大流):POJ 2135 Farm Tour

POJ2135 Farm Tour

POJ 2135.Farm Tour 消负圈法最小费用流