Farm Tour

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2135
64-bit integer IO format: %lld      Java class name: Main

 
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

  1. 4 5
  2. 1 2 1
  3. 2 3 1
  4. 3 4 1
  5. 1 3 2
  6. 2 4 2

Sample Output

  1. 6
  2.  
  3.   这题就是在一个无向图中找出两条从点1到点n的路径,同时要求路程最短。
      于是贴最小费用最大流模板就AC啦。
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5. using namespace std;
  6. const int INF=;
  7. const int maxn=,maxm=;
  8. int cnt,fir[maxn],nxt[maxm],to[maxm],cap[maxm],val[maxm],dis[maxn],path[maxn];
  9.  
  10. void addedge(int a,int b,int c,int v)
  11. {
  12. nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
  13. }
  14. int S,T;
  15. int Spfa()
  16. {
  17. queue<int>q;
  18. memset(dis,,sizeof(dis));
  19. q.push(S);dis[S]=;
  20. while(!q.empty())
  21. {
  22. int node=q.front();q.pop();
  23. for(int i=fir[node];i;i=nxt[i])
  24. if(cap[i]&&dis[node]+val[i]<dis[to[i]]){
  25. dis[to[i]]=val[i]+dis[node];
  26. path[to[i]]=i;
  27. q.push(to[i]);
  28. }
  29. }
  30. return dis[T]==dis[T+]?:dis[T];
  31. }
  32.  
  33. int Aug()
  34. {
  35. int p=T,f=INF;
  36. while(p!=S)
  37. {
  38. f=min(f,cap[path[p]]);
  39. p=to[path[p]^];
  40. }
  41. p=T;
  42. while(p!=S)
  43. {
  44. cap[path[p]]-=f;
  45. cap[path[p]^]+=f;
  46. p=to[path[p]^];
  47. }
  48. return f;
  49. }
  50.  
  51. int MCMF()
  52. {
  53. int ret=,d;
  54. while(d=Spfa())
  55. ret+=Aug()*d;
  56. return ret;
  57. }
  58.  
  59. void Init(int n)
  60. {
  61. cnt=;S=;T=n+;
  62. for(int i=;i<=n;i++)fir[i]=;
  63. }
  64.  
  65. int main()
  66. {
  67. int n,m;
  68. while(~scanf("%d%d",&n,&m))
  69. {
  70. Init(n);
  71. int a,b,v;
  72. for(int i=;i<=m;i++)
  73. {
  74. scanf("%d%d%d",&a,&b,&v);
  75. addedge(a,b,,v);
  76. addedge(b,a,,-v);
  77. addedge(b,a,,v);
  78. addedge(a,b,,-v);
  79. }
  80. addedge(S,,,);
  81. addedge(,S,,);
  82. addedge(n,T,,);
  83. addedge(T,n,,);
  84. printf("%d\n",MCMF());
  85. }
  86. return ;
  87. }
  1.  

网络流(最小费用最大流):POJ 2135 Farm Tour的更多相关文章

  1. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  2. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

  3. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  4. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

  5. POJ 2135 Farm Tour(最小费用最大流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  6. HDU 6118 度度熊的交易计划(网络流-最小费用最大流)

    度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但 ...

  7. POJ 2135 Farm Tour(最小费用最大流,变形)

    题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...

  8. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  9. POJ 2135 Farm Tour 最小费用流

    两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...

随机推荐

  1. C++ 简单的入门语法

    入门的hello world using namespace std; 是使用命名空间,有点像java里面的引入包main 方法和java一样是主入口,有且只有一个,因为是int ,所以还必须返回一个 ...

  2. MongoDB_1

    突然想去看下MongoDB的东西,于是有了这篇文章.其实很早以前就看过一些关于NoSql的文章,还记得当时里面有介绍MongoDB的,多瞅了2眼,并且在Window下安装了MongoDB的驱动,小玩了 ...

  3. Java 获取字符串中第N次出现的字符位置

    public static int getCharacterPosition(String string){    //这里是获取"/"符号的位置    Matcher slash ...

  4. maven mirror

    国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...

  5. meta标签常用属性整理

    在segmentfault看到这篇文章,觉得整理的很详细,所以转载过来和大家分享一下. 原文地址:http://segmentfault.com/blog/ciaocc/119000000240791 ...

  6. YII框架的部署 通过YII脚手架程序创建应用程序系统

    1,把YII框架里面的framework复制粘贴到nginx目录下 2,创建一个商城系统: 1)修改环境变量 制定php.exe的目录 2)C:\Users\Administrator>cd C ...

  7. Keil的使用-1创建项目和工程

    下载keil,注意不要使用MDK版本(主要是arm开发使用),大小约54M 安装过程不再详述 安装Keil成功并运行后,新建项目,   创建新项目,然后弹出下图,选择对应的单片机芯片(双击)     ...

  8. BTREE与HASH的区别

    对于 B-tree 和 hash 数据结构的理解能够有助于预测不同存储引擎下使用不同索引的查询性能的差异,尤其是那些允许你选择 B-tree 或者 hash 索引的内存存储引擎. B-Tree 索引的 ...

  9. 清北第一套题(zhx)

    死亡 [问题描述] 现在有个位置可以打sif,有个人在排队等着打sif.现在告诉你前个人每个人需要多长的时间打sif,问你第个人什么时候才能打sif.(前个人必须按照顺序来) [输入格式] 第一行两个 ...

  10. Solr4.8.0源码分析(25)之SolrCloud的Split流程

    Solr4.8.0源码分析(25)之SolrCloud的Split流程(一) 题记:昨天有位网友问我SolrCloud的split的机制是如何的,这个还真不知道,所以今天抽空去看了Split的原理,大 ...