poj2135 Farm Tour(最小费用流)

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11761 Accepted: 4380

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
/*
搞了好长时间,加油!!
Time:2014-9-20 15:56
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#include<queue>
using namespace std;
#define INF INT_MAX
#define MAX 1010
#define MAXM 10100
struct Edge{
	int u,v;
	int cap,cost;
	int next;
}edge[MAXM<<2];
int head[MAX],Num;
int sumFlow;
void Add(int u,int v,int cap,int cost){
	edge[Num].u=u;edge[Num].v=v;
	edge[Num].cap=cap;edge[Num].cost=cost;
	edge[Num].next=head[u]; head[u]=Num++;
	
	edge[Num].u=v;edge[Num].v=u;
	edge[Num].cap=0;edge[Num].cost=-cost;
	edge[Num].next=head[v];head[v]=Num++; 
}
int pre[MAX],d[MAX];
bool vis[MAX];
bool SPFA(int S,int T,int N){
	memset(vis,0,sizeof(vis));
	memset(pre,-1,sizeof(pre));//记得初始化为-1 
	for(int i=0;i<=N;i++)
		d[i]=INF;
	queue<int >q;
	q.push(S);d[S]=0;vis[S]=true;
	while(!q.empty()){
		int u=q.front();q.pop();
		vis[u]=false;
		for(int i=head[u];~i;i=edge[i].next){
			int v=edge[i].v;
			if(edge[i].cap&&d[v]>d[u]+edge[i].cost){
					pre[v]=i;
					d[v]=d[u]+edge[i].cost;
				if(!vis[v]){
					vis[v]=true;
					q.push(v);
				} 
			}
		}
	}
	if(d[T]==INF) return false;
	else return true; 
} 
int MCMF(int S,int T,int N){
	int mincost,flow,minflow;
	mincost=flow=0;
	while(SPFA(S,T,N)){
			minflow=INF;
		for(int i=pre[T];i!=-1;i=pre[edge[i].u]){
			if(minflow>edge[i].cap)
				minflow=edge[i].cap;
		}
		flow+=minflow;
		for(int i=pre[T];i!=-1;i=pre[edge[i].u]){
			edge[i].cap-=minflow;
			edge[i^1].cap+=minflow;//用^速度快,相当于+1 
		}
		mincost+=d[T]*minflow; 
	}
	sumFlow=flow;//最大流,但是此处只求最小费用 
	return mincost;
}
void Init(){
	Num=0;
	memset(head,-1,sizeof(head));
}
void solve(){
	int N,M;
	while(scanf("%d%d",&N,&M)!=EOF){
		int u,v,w;
		Init();
		while(M--){
			scanf("%d%d%d",&u,&v,&w);
			Add(u,v,1,w);Add(v,u,1,w);
		}
		int S=0;int T=N+1;
		Add(S,1,2,0);Add(N,T,2,0);
		printf("%d\n",MCMF(S,T,T+1));
	}
}
int main(){
	//freopen("poj2135.txt","r",stdin);
	solve();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值