博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2135 Farm Tour
阅读量:5108 次
发布时间:2019-06-13

本文共 2449 字,大约阅读时间需要 8 分钟。

POJ2135 Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6299 Accepted: 2248

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 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6
**********************************************************************************
题目大意:一个人要从1走到n又要从n走到1,每条路上都有一个花费,要求,同一条路只走一遍以及花费最小。
解题思路:最小费用最大流。因为要从1到n再从n到1,其实可以想成是从1到n走了两次,而这两次里没有一条路是重复的。将路拆成两条,然后每条的容量都是1,费用都是v,求最小费用最大流即可。
PS:万恶的期中考试,去备战了3天,结果现在写代码竟然如此生疏,不太爽。
#include 
#include
#include
#include
#define N 1005#define M 40005#define INF 0x3f3f3f3fusing namespace std;int head[N],ed[M],val[M],nxt[M],up[M],eid;int n,m;int vis[N],dis[N],pre[N],road[N];void add_edge(int s,int e,int u,int v){ up[eid]=u; ed[eid]=e; val[eid]=v; nxt[eid]=head[s]; head[s]=eid++; up[eid]=0; ed[eid]=s; val[eid]=-v; nxt[eid]=head[e]; head[e]=eid++;}void spfa(void){ queue
que; memset(vis,0,sizeof(vis)); memset(pre,0,sizeof(pre)); for(int i=2;i<=n;i++) dis[i]=INF; dis[1]=0; que.push(1); while(!que.empty()) { int t=que.front(); que.pop(); vis[t]=0; for(int i=head[t];~i;i=nxt[i]) { if(up[i]==0)continue; int e=ed[i],v=val[i]; if(dis[t]+v

  

 

转载于:https://www.cnblogs.com/Fatedayt/archive/2011/10/26/2225725.html

你可能感兴趣的文章
浅谈 unix, linux, ios, android 区别和联系
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
中国烧鹅系列:利用烧鹅自动执行SD卡上的自定义程序(含视频)
查看>>
Solaris11修改主机名
查看>>
latex for wordpress(一)
查看>>
如何在maven工程中加载oracle驱动
查看>>
Flask 系列之 SQLAlchemy
查看>>
iframe跨域与session失效问题
查看>>
aboutMe
查看>>
【Debug】IAR在线调试时报错,Warning: Stack pointer is setup to incorrect alignmentStack,芯片使用STM32F103ZET6...
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>
Hash和Bloom Filter
查看>>
python常用函数
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
[HDU3683 Gomoku]
查看>>
【工具相关】iOS-Reveal的使用
查看>>
数据库3
查看>>
存储分类
查看>>
下一代操作系统与软件
查看>>