# POJ 2135 Farm Tour （网络流，最小费用最大流）

### 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.

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

6

## 代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int maxN=5001;
const int maxM=50001;
const int inf=2147483647;

class Edge
{
public:
int u,v,flow,cost;//记录每一条边的信息，出点，目的点，残量，花费
};

int n,m;
int cnt=-1;//记录邻接表的边数
int Next[maxM];
Edge E[maxM];
int Flow[maxN];//spfa中保存每个点可以通过的残量
int Pre[maxN];//spfa中保存每个点是由哪一条边转移过来的边的标号
int Dist[maxN];//spfa中保存到每个点的距离，即最小花费
bool inqueue[maxN];

void Add_Edge(int u,int v,int flow,int cost);//添加边
void _Add(int u,int v,int flow,int cost);
bool spfa();

int main()
{
cin>>n>>m;
memset(Next,-1,sizeof(Next));
for (int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
}
int Ans=0;//记录花费
while (spfa())//spfa寻增广路
{
int now=n+1;
int last=Pre[now];//从汇点向回走，将增广路上的每一条边均减去消耗的流量
while (now!=0)
{
E[last].flow-=Flow[n+1];
E[last^1].flow+=Flow[n+1];
now=E[last].u;
last=Pre[now];
}
Ans+=Dist[n+1]*Flow[n+1];//累计花费
}
cout<<Ans<<endl;
}

void Add_Edge(int u,int v,int flow,int cost)
{
return;
}

void _Add(int u,int v,int flow,int cost)
{
cnt++;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow;
E[cnt].cost=cost;
return;
}

bool spfa()
{
memset(Pre,-1,sizeof(Pre));//前驱边的编号
memset(inqueue,0,sizeof(inqueue));
memset(Flow,0,sizeof(Flow));
memset(Dist,127,sizeof(Dist));
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);//将源点放入队列
Dist[0]=0;
Flow[0]=inf;
inqueue[0]=1;
do
{
int u=Q.front();
//cout<<u<<endl;
inqueue[u]=0;
Q.pop();
{
int v=E[i].v;
if ((E[i].flow>0)&&(Dist[u]+E[i].cost<Dist[v]))//当还有残量存在且花费更小时，修改v的信息
{
Dist[v]=E[i].cost+Dist[u];
Pre[v]=i;
Flow[v]=min(Flow[u],E[i].flow);
if (inqueue[v]==0)
{
Q.push(v);
inqueue[v]=1;
}
}
}
}
while (!Q.empty());
if (Pre[n+1]==-1)//当汇点没有前驱，及说明没有增广到汇点，也说明不存在增广路，直接退出
return 0;
return 1;
}

posted @ 2017-07-31 08:07  SYCstudio  阅读(666)  评论(0编辑  收藏