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

Farm Tour
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5540

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1010
#define M 10010
#define INF 0x3f3f3f3f
struct Node
{
int from,to,next,cap,cost;///起点，终点，同起点下一条边，残余流量，费用
}edge[M<<2];
int vis[N],d[N],pp[N];
int sumflow;///最大流量总和
void init()
{
cnt=0;
}
void addedge(int from,int to,int cap,int cost)
{
edge[cnt].from=from;edge[cnt].to=to;edge[cnt].cost=cost;edge[cnt].cap=cap;
edge[cnt].from=to;edge[cnt].to=from;edge[cnt].cost=-cost;edge[cnt].cap=0;
}
int spfa(int s,int t,int n)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
for(int i=0;i<=n;i++)
d[i]=INF;
d[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
{
int v=edge[i].to;
if(edge[i].cap>0&&d[v]>d[u]+edge[i].cost)
{
d[v]=d[u]+edge[i].cost;
pp[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(d[t]==INF) return 0;///找不到一条到终点的路
return 1;
}
int MCMF(int s,int t,int n)
{
int mincost=0,minflow,flow=0;///最小费用，路径中最小流量，总流量
while(spfa(s,t,n))///找当前的最短路
{
minflow=INF+1;
for(int i=pp[t];i!=-1;i=pp[edge[i].from])
minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
flow+=minflow;///总流量加上最小流量
for(int i=pp[t];i!=-1;i=pp[edge[i].from])
{
edge[i].cap-=minflow;///当前边减去最小流量
edge[i^1].cap+=minflow;///反向边加上最小流量
}
mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量（经过多少次）
}
sumflow=flow;
return mincost;
}
int main()
{
int n,m;
int from,to,cost;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&from,&to,&cost);
}
int S=0,T=n+1;
int ans=MCMF(S,T,T);///流量为2保证只会走两次，如果是1到n有可能走多次
printf("%d\n",ans);
}
return 0;
}


#### POJ_2135_Farm Tour(最小费用流)

2015-09-11 11:48:05

#### poj2135（*最小费用最大流）

2017-02-11 10:46:46

#### POJ 2135 Farm Tour(网络流之费用流）

2014-07-26 17:20:41

#### POJ2135 Farm Tour（最小费用最大流裸题）

2016-10-03 23:52:59

#### POJ 2135 Farm Tour(最小费用最大流)

2014-09-03 15:50:24

#### poj 2135 Farm Tour（最小费用最大流）

2016-04-02 16:40:59

#### Farm Tour POJ - 2135 最小费用最大流

2017-04-30 21:08:00

#### poj 2135 Farm Tour 【最小费用最大流】

2015-08-27 17:02:17

#### POJ 2135 Farm Tour (最小费用最大流)

2016-04-30 12:51:30

#### POJ 2135 Farm Tour (最小费用最大流）

2011-10-25 21:41:56