## Cyberdream(CD)的博客

「Stay hungry. Stay foolish.」

# poj 2135 Farm Tour 最小费用最大流模板题

###### 描述：

Farm Tour
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15031 Accepted: 5756

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

###### 题意：

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

###### 代码：
#include <iostream>
#include<queue>
#include<cstring>
using namespace std;

typedef long long ll;
typedef int Type;
const int N=1005;
const Type inf=0x3f3f3f3f;

struct edge{
int u,v;
Type cap, flow, cost;
edge() {}
edge(int from,int to,int c,int f,int co):u(from),v(to),cap(c),flow(f),cost(co){}
};

struct MCMF{
int n,m;
vector<edge>es;
vector<int>G[N];
int inq[N];//是否在队列中
Type d[N];//SPFA时总费用
int p[N];//上一条弧
Type a[N];//可改进量

void init(int n){
this->n=n;
for(int i=0; i<n; i++)G[i].clear();
es.clear();
}

void add_edge(int u,int v,Type cap,Type cost){
es.push_back(edge(u, v, cap, 0, cost));
es.push_back(edge(v, u, 0, 0, -cost));
m=es.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}

bool spfa(int s,int t,Type& flow,Type& cost){
memset(inq, 0, sizeof(inq));
for(int i=0; i<n; i++)d[i]=inf;
d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;

queue<int>q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();inq[u]=0;
for(int i=0; i<G[u].size(); i++){
edge& e=es[G[u][i]];
int v=e.v;
if(e.cap>e.flow && d[v]>d[u]+e.cost){ //在新流下考虑最小费用
d[v]=d[u]+e.cost;
a[v]=min(a[u], e.cap-e.flow);
p[v]=G[u][i];
if(!inq[v]){ inq[v]=1; q.push(v);}
}
}
}
if(d[t]==inf)return false;
flow+=a[t],cost+=a[t]*d[t];
int u=t;
while(u!=s){
es[p[u]].flow+=a[t];
es[p[u]^1].flow-=a[t];
u=es[p[u]].u;
}
return true;
}

Type Mincost(int s,int t){
Type flow=0, cost=0;
while(spfa(s,t,flow,cost));
return cost;
}
}H;

int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);

int n,m;
cin>>n>>m;
H.init(n+2);
int u,v,c;
while(m--){
cin>>u>>v>>c;
}

cout<<H.Mincost(0, n+1)<<endl;
return 0;
}

10-14 1.2万

03-22 3278

08-07 6116

11-28 874

08-24 1774

04-05 477

10-23 148

05-09 29

08-18 3989

03-23 3890