poj 2135: Farm Tour

本文介绍了一种利用最小费用最大流算法解决农场中最短巡游路径问题的方法。通过建立双向边模型,确保了从起点到终点的路径既是最短也是唯一的,并且不重复经过任意一条边。

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

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14059 Accepted: 5354

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

题解:

  最小费用最大流模板直接上,注意是双向边。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<cstring>
 7 #include<queue>
 8 #include<vector>
 9 using namespace std;
10 const int inf=1e9;
11 const int maxn=2000,maxm=3000000;
12 struct node{
13     int to,rest,next,cost;
14 }e[maxm];
15 int head[maxn],cnt=1;
16 inline void addedge(int x,int y,int z,int c){
17     e[++cnt].to=y; e[cnt].rest=z; e[cnt].next=head[x]; head[x]=cnt; e[cnt].cost= c;
18     e[++cnt].to=x; e[cnt].rest=0; e[cnt].next=head[y]; head[y]=cnt; e[cnt].cost=-c;    
19 }
20 int N,M,S,T;
21 int dis[maxn],pre[maxn];
22 bool vis[maxn];
23 int maxflow,mincost;
24 bool SPFA(){
25     static queue<int> Q;
26     for(int i=S;i<=T;i++) dis[i]=inf,vis[i]=false;
27     Q.push(S); dis[S]=0; vis[S]=true;
28     while(!Q.empty()){
29         int x=Q.front(); Q.pop();
30         vis[x]=false;
31         for(int i=head[x];i;i=e[i].next){
32             int y=e[i].to;
33             if(e[i].rest&&dis[y]>dis[x]+e[i].cost){
34                 dis[y]=dis[x]+e[i].cost;
35                 pre[y]=i;
36                 if(vis[y]==false){
37                     vis[y]=true;
38                     Q.push(y);
39                 }
40             }
41         }
42     }
43     return dis[T]<inf;
44 }
45 void update(){
46     int flow=inf;
47     for(int i=pre[T];i;i=pre[e[i^1].to])
48         flow=min(flow,e[i].rest);
49     for(int i=pre[T];i;i=pre[e[i^1].to]){
50         e[i].rest-=flow;
51         e[i^1].rest+=flow;
52     }
53     maxflow+=flow; mincost+=flow*dis[T];
54 }
55 void MCF(){
56     maxflow=mincost=0;
57     while(SPFA()) update();
58 }
59 int main(){
60     scanf("%d%d",&N,&M);
61     S=0; T=N+1;
62     addedge(S,1,2,0);
63     for(int i=1,u,v,c;i<=M;i++){
64         scanf("%d%d%d",&u,&v,&c);
65         addedge(u,v,1,c);
66         addedge(v,u,1,c);
67     }
68     addedge(N,T,2,0);
69     MCF();
70     printf("%d",mincost);
71     return 0;
72 }

 

 

 

转载于:https://www.cnblogs.com/CXCXCXC/p/5207954.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值