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. 

题目大意:给一个n个点m条边的无向图,求从1走到n再从n走到1的最短路径,要求边不重复。

思路:建立一个附加源点S,连一条边到点1,容量为2,费用为0;建立一个附加汇点T,从n连一条边到T,容量为2,费用为0;对于每条边u、v,连一条边u到v,容量为1,费用为距离,再连一条边v到u,容量为1,费用为距离。最小费用最大流为答案。

建图正确性略微说明:可以考虑两次从1走到n,边不重复,那么建图流两个流量从源点到汇点,最小的费用就保证了距离最短。由于边长度为非负数,那么最小花费一定不会是一条路径从u到v,另一条路径从v到u,如果这样走了两次,还不如两条路径都不经过u-v,都走另外一条路的后继路径(不要跟我说长度是0怎么破)

PS:忘了改数组大小又TLE了一次T_T,怎么老是忘

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int MAXV = ;
  9. const int MAXE = ;
  10. const int INF = 0x7f7f7f7f;
  11.  
  12. struct SPFA_COST_FLOW {
  13. bool vis[MAXV];
  14. int head[MAXV], dis[MAXV], pre[MAXV];
  15. int to[MAXE], next[MAXE], cost[MAXE], flow[MAXE];
  16. int n, st, ed, ecnt;
  17.  
  18. void init() {
  19. memset(head, , sizeof(head));
  20. ecnt = ;
  21. }
  22.  
  23. void add_edge(int u, int v, int c, int w) {
  24. to[ecnt] = v; flow[ecnt] = c; cost[ecnt] = w; next[ecnt] = head[u]; head[u] = ecnt++;
  25. to[ecnt] = u; flow[ecnt] = ; cost[ecnt] = -w; next[ecnt] = head[v]; head[v] = ecnt++;
  26. }
  27.  
  28. bool spfa() {
  29. memset(vis, , sizeof(vis));
  30. memset(dis, 0x7f, sizeof(dis));
  31. queue<int> que; que.push(st);
  32. vis[st] = true; dis[st] = ;
  33. while(!que.empty()) {
  34. int u = que.front(); que.pop();
  35. vis[u] = false;
  36. for(int p = head[u]; p; p = next[p]) {
  37. int &v = to[p];
  38. if(flow[p] && dis[v] > dis[u] + cost[p]) {
  39. dis[v] = dis[u] + cost[p];
  40. pre[v] = p;
  41. if(!vis[v]) {
  42. vis[v] = true;
  43. que.push(v);
  44. }
  45. }
  46. }
  47. }
  48. return dis[ed] < INF;
  49. }
  50.  
  51. int maxFlow, minCost;
  52.  
  53. int min_cost_flow(int ss, int tt, int nn) {
  54. st = ss, ed = tt, n = nn;
  55. maxFlow = minCost = ;
  56. while(spfa()) {
  57. minCost += dis[ed];
  58. int u = ed, tmp = INF;
  59. while(u != st) {
  60. tmp = min(tmp, flow[pre[u]]);
  61. u = to[pre[u] ^ ];
  62. }
  63. u = ed;
  64. while(u != st) {
  65. flow[pre[u]] -= tmp;
  66. flow[pre[u] ^ ] += tmp;
  67. u = to[pre[u] ^ ];
  68. }
  69. maxFlow += tmp;
  70. }
  71. return minCost;
  72. }
  73. } G;
  74.  
  75. int n, m;
  76.  
  77. int main() {
  78. scanf("%d%d", &n, &m);
  79. G.init();
  80. int ss = n + , tt = n + ;
  81. while(m--) {
  82. int u, v, c;
  83. scanf("%d%d%d", &u, &v, &c);
  84. G.add_edge(u, v, , c);
  85. G.add_edge(v, u, , c);
  86. }
  87. G.add_edge(ss, , , );
  88. G.add_edge(n, tt, , );
  89. printf("%d\n", G.min_cost_flow(ss, tt, tt));
  90. }

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

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

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

  2. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  3. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  4. [poj] 1235 Farm Tour || 最小费用最大流

    原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...

  5. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  6. TZOJ 1513 Farm Tour(最小费用最大流)

    描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...

  7. Farm Tour(最小费用最大流模板)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18150   Accepted: 7023 Descri ...

  8. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  9. poj 2135 Farm Tour 最小费最大流

    inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...

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

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

随机推荐

  1. jenkins 安装配置: centos-master windows/linux-slave + nginx代理 + node + job

    centos install jenkins: 1.sudo vi /etc/yum.repos.d/jenkins.repo [jenkins] name=Jenkins baseurl=http: ...

  2. 菜鸟笔记 -- Chapter 1 计算机从0到1

    进入20世纪第二个十年,计算机已经成为生活中一个必不可小的工具了,但我们真的了解计算机吗?计算机有哪些部分构成?不同的计算机又可以做什么样的事情呢?我们的PC和用来做加减乘除的计算器都属于计算机范畴吗 ...

  3. 【转】RMAN删除过期备份或非过期备份

    (一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件.当使用RMAN执行备份操作时,会在RMAN资料库(RMAN Repository)中生成RMAN备份记录,默认情况下RMAN ...

  4. Java常用的正则校验

    1.非负整数: (^[1-9]+[0-9]*$)|(^[0]{1}$) 或 (^[1-9]+[0-9]*$)|0 2.非正整数: (^-[1-9]+[0-9]*$)|(^[0]{1}$) 或 (^-[ ...

  5. Sonar安装-Linux[20171227]

    前言     一款不错的代码质量管理工具Sonar 前期准备     官方参考文档 https://docs.sonarqube.org/display/SONAR/Documentation     ...

  6. mybatis笔记之一次插入多条数据sql语句写法

    <insert id="insertList" parameterType="java.util.List"> insert into balanc ...

  7. php-5.6.26源代码 - include_once、require_once、include、require、eval 的opcode处理器

    # ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER 实现在文件 php-\Zend\zend_vm_execute.h static int ZEND_FASTCALL ...

  8. Scala语法(二)

    (1)类,对象 //定义类(属性.方法),实例化对象 class counter{ *//主构造器 class counter(name:String,mode:Int){ ... } 实例化:val ...

  9. Python学习之模块基础

    模块就是程序 编写以下简单代码 print('hello python') 并将py文件保存在c盘的python(假设新建)文件下,通过pycharm的Terminal 或者windom命令窗口调出p ...

  10. Druid时序数据库常见问题及处理方式

    最近将Druid-0.10.0升级到Druid-0.12.1的过程中遇到一些问题,为了后期方便分析问题和及时解决问题,特此写这篇文章将工作中遇到的Druid问题及解决办法记录下来,以供其他人借鉴,其中 ...