poj2135 Farm Tour 最小费用流

本文介绍了一种求解从起点到终点的两条无公共边最短路径的问题,通过转化为求最小费用流的方法来解决。利用SPFA算法进行最短路径搜索,并通过多次迭代找到总长度最短的两条路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21810 Accepted: 8364

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

USACO 2003 February Green

/*将问题转化为求从1号顶点到N号顶点的两条没有公共边的路径,转化后即求流量为2的最小费用流了。*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
typedef long long ll;
const int N=1005;
const int INF=0x3f3f3f3f;
int n,m,s,t;
struct edge{
	ll to,cap,cost,rev;
};
vector<edge>vec[N];
ll dis[N],vis[N];
int preV[N],preE[N];
void add_edge(int from,int to,ll cap,ll cost){
	vec[from].push_back(edge{to,cap,cost,vec[to].size()});
	vec[to].push_back(edge{from,0,-cost,vec[from].size()-1});
}
bool spfa(int s,int t){
	memset(preV,-1,sizeof(preV));
	memset(dis,INF,sizeof(dis));
	dis[s]=0;
	vis[s]=true;
	queue<int>que;
	que.push(s);
	while(!que.empty()){
		int now=que.front();
		que.pop(); vis[now]=false;
		for(int i=0;i<vec[now].size();i++){
			edge &e=vec[now][i];
			if(dis[now]+e.cost<dis[e.to]&&e.cap>0){
				dis[e.to]=dis[now]+e.cost;
				preV[e.to]=now;
				preE[e.to]=i;
				if(!vis[e.to]){//必须在这里判断标记,不然超时。
					que.push(e.to);
					vis[e.to]=true;	
				}
				
			}
		}
	}
	if(preV[t]==-1) return false;
	return true;
}
ll min_cost_flow(int s,int t,ll f){
	ll cost=0;
	while(spfa(s,t)&&f>0){
		ll d=INF;
		for(int v=t;v!=s;v=preV[v]){
			d=min(d,vec[preV[v]][preE[v]].cap);
		}
		f-=d;
		cost+=dis[t]*d;
		for(int v=t;v!=s;v=preV[v]){
			edge &e=vec[preV[v]][preE[v]];
			e.cap-=d;
			vec[v][e.rev].cap+=d;
		}
	}
	return cost;
}
int main(){
	int a,b,c;
	scanf("%d %d",&n,&m);
	for(int i=0;i<m;i++){
		scanf("%d %d %d",&a,&b,&c);
		add_edge(a,b,1,c);
		add_edge(b,a,1,c);
	}
	printf("%d\n",min_cost_flow(1,n,2));
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值