poj 2351 Farm Tour (最小费用最大流)

Farm Tour
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17230   Accepted: 6647

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

题意:n个点 m条路 每条路都有一个长度 现在定义从1节点出发 到n节点  再从n节点回1节点
这两条路不能有重边 问你最短路多少
其实 这可以简化成一个求做小费用的网络流,每条边的流量设为1 长度为他们的费用 
自己定义一个汇点和出发点 答案就是求流量为2的最小费用最大流 (这题目要主要 这是无向边 我也不知道为什么  反正没建就错了)
套个板子就可以了
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cstdlib>
  6 #include<string.h>
  7 #include<set>
  8 #include<vector>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<cmath>
 13 typedef long long ll;
 14 typedef unsigned long long LL;
 15 using namespace std;
 16 const double PI=acos(-1.0);
 17 const double eps=0.0000000001;
 18 const int INF=0x3f3f3f3f;
 19 const int N=1000000+100;
 20 int head[N];
 21 int dis[N];
 22 int pre[N];
 23 int vis[N];
 24 int tot;
 25 int m,n;
 26 struct node{
 27     int from,to,next,flow,cost;
 28 }edge[N<<1];
 29 void init(){
 30     memset(head,-1,sizeof(head));
 31     tot=0;
 32 }
 33 void add(int u,int v,int c,int cost){
 34     edge[tot].from=u;
 35     edge[tot].to=v;
 36     edge[tot].flow=c;
 37     edge[tot].cost=cost;
 38     edge[tot].next=head[u];
 39     head[u]=tot++;
 40     edge[tot].from=v;
 41     edge[tot].to=u;
 42     edge[tot].flow=0;
 43     edge[tot].cost=-cost;
 44     edge[tot].next=head[v];
 45     head[v]=tot++;
 46 }
 47 int spfa(int s,int t){
 48     memset(pre,-1,sizeof(pre));
 49     memset(dis,INF,sizeof(dis));
 50     memset(vis,0,sizeof(vis));
 51     queue<int>q;
 52     dis[s]=0;
 53     vis[s]=1;
 54     q.push(s);
 55     while(!q.empty()){
 56         int x=q.front();
 57         q.pop();
 58         vis[x]=0;
 59         for(int i=head[x];i!=-1;i=edge[i].next){
 60             int v=edge[i].to;
 61             if(edge[i].flow&&dis[v]>dis[x]+edge[i].cost){
 62                 dis[v]=edge[i].cost+dis[x];
 63                 pre[v]=i;
 64                 if(vis[v]==0){
 65                     vis[v]=1;
 66                     q.push(v);
 67                 }
 68 
 69             }
 70         }
 71     }
 72     if(pre[t]==-1)return 0;
 73     return 1;
 74 }
 75 int MCMF(int s,int t){
 76     int flow=0;
 77     int cost=0;
 78     while(spfa(s,t)){
 79         int minn=INF;
 80         //cout<<3<<endl;
 81         for(int i=pre[t];i!=-1;i=pre[edge[i].from]){
 82             minn=min(minn,edge[i].flow);
 83         }
 84         for(int i=pre[t];i!=-1;i=pre[edge[i].from]){
 85             edge[i].flow=edge[i].flow-minn;
 86             edge[i^1].flow=edge[i^1].flow+minn;
 87             cost=edge[i].cost+cost;
 88         }
 89         flow=flow+minn;
 90         if(flow==2)return cost;
 91     }
 92     return cost;
 93 }
 94 int main(){
 95     while(scanf("%d%d",&n,&m)!=EOF){
 96         init();
 97         add(0,1,2,0);
 98         add(n,n+1,2,0);
 99         for(int i=1;i<=m;i++){
100             int u,v,cost;
101             scanf("%d%d%d",&u,&v,&cost);
102             add(u,v,1,cost);
103             add(v,u,1,cost);
104         }
105         cout<<MCMF(0,n+1)<<endl;
106     }
107 }
原文地址:https://www.cnblogs.com/Aa1039510121/p/7218415.html