累了就要写题解,近期总是被虐到没脾气。

来回最短路问题貌似也能够用DP来搞。只是拿费用流还是非常方便的。

能够转化成求满流为2 的最小花费。一般做法为拆点,对于 i 拆为2*i 和 2*i+1。然后连一条流量为1(花费依据题意来定) 的边来控制每一个点仅仅能通过一次。

额外加入source和sink来控制满流为2。

代码都雷同,以HDU3376为例。

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <queue>
  7. #include <cmath>
  8. #include <stack>
  9. #include <map>
  10.  
  11. #pragma comment(linker, "/STACK:1024000000");
  12. #define EPS (1e-8)
  13. #define LL long long
  14. #define ULL unsigned long long
  15. #define _LL __int64
  16. #define INF 0x3f3f3f3f
  17. #define Mod 6000007
  18.  
  19. using namespace std;
  20.  
  21. const int EDGE = 6000000,POINT = 730000;
  22.  
  23. struct E
  24. {
  25. int Max,cost,v,next;
  26. }edge[EDGE];
  27.  
  28. int head[POINT];
  29.  
  30. int Top;
  31.  
  32. void Link(int u,int v,int w,int cost)
  33. {
  34. edge[Top].v = v;
  35. edge[Top].Max = w;
  36. edge[Top].cost = cost;
  37. edge[Top].next = head[u];
  38. head[u] = Top++;
  39. }
  40.  
  41. int Map[610][610];
  42.  
  43. int dis[POINT],cur[POINT],flow[POINT];
  44. bool mark[POINT];
  45.  
  46. void Updata(int site,int flow,int &cost)
  47. {
  48. for(;cur[site] != -1; site = edge[cur[site]^1].v)
  49. {
  50. edge[cur[site]].Max -= flow;
  51. edge[cur[site]^1].Max += flow;
  52. cost += edge[cur[site]].cost * flow;
  53. }
  54. }
  55.  
  56. queue<int> q;
  57.  
  58. int spfa(int S,int T,int &cost)
  59. {
  60. memset(mark,false,sizeof(mark));
  61. memset(dis,INF,sizeof(dis));
  62.  
  63. cur[S] = -1,dis[S] = 0,flow[S] = INF;
  64.  
  65. q.push(S);
  66.  
  67. int f,t;
  68.  
  69. while(q.empty() == false)
  70. {
  71. f = q.front();
  72. q.pop();
  73. mark[f] = false;
  74.  
  75. for(int p = head[f];p != -1; p = edge[p].next)
  76. {
  77. t = edge[p].v;
  78. if(edge[p].Max && dis[t] > dis[f] + edge[p].cost)
  79. {
  80. dis[t] = dis[f] + edge[p].cost;
  81. cur[t] = p;
  82. flow[t] = min(flow[f],edge[p].Max);
  83.  
  84. if(mark[t] == false)
  85. {
  86. mark[t] = true;
  87. q.push(t);
  88. }
  89. }
  90. }
  91. }
  92.  
  93. if(dis[T] == INF)
  94. return 0;
  95. Updata(T,flow[T],cost);
  96. return flow[T];
  97. }
  98.  
  99. int Cal_Max_Flow_Min_Cost(int S,int T,int n)
  100. {
  101. int temp,flow = 0,cost = 0;
  102.  
  103. do
  104. {
  105. temp = spfa(S,T,cost);
  106. flow += temp;
  107. }while(temp);
  108. return cost;
  109. }
  110.  
  111. inline int Cal(int x,int y,int n)
  112. {
  113. return ((x-1)*n+y)*2-1;
  114. }
  115.  
  116. int main()
  117. {
  118. int n;
  119. int i,j;
  120.  
  121. while(scanf("%d",&n) != EOF)
  122. {
  123. memset(head,-1,sizeof(head));
  124. Top = 0;
  125.  
  126. for(i = 1;i <= n; ++i)
  127. {
  128. for(j = 1;j <= n; ++j)
  129. {
  130. scanf("%d",&Map[i][j]);
  131. if(i == j && (i == 1 || i == n))
  132. Link(Cal(i,j,n),Cal(i,j,n)+1,2,-Map[i][j]);
  133. else
  134. Link(Cal(i,j,n),Cal(i,j,n)+1,1,-Map[i][j]);
  135. Link(Cal(i,j,n)+1,Cal(i,j,n),0,Map[i][j]);
  136. }
  137. }
  138.  
  139. for(i = 1;i <= n; ++i)
  140. {
  141. for(j = 1;j <= n; ++j)
  142. {
  143. if(j < n)
  144. {
  145. Link(Cal(i,j,n)+1,Cal(i,j+1,n),1,0);
  146. Link(Cal(i,j+1,n),Cal(i,j,n)+1,0,0);
  147. }
  148. if(i < n)
  149. {
  150. Link(Cal(i,j,n)+1,Cal(i+1,j,n),1,0);
  151. Link(Cal(i+1,j,n),Cal(i,j,n)+1,0,0);
  152. }
  153. }
  154. }
  155.  
  156. printf("%d\n",-Cal_Max_Flow_Min_Cost(1,n*n*2,n*n*2) - Map[1][1] - Map[n][n]);
  157. }
  158. return 0;
  159. }

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路的更多相关文章

  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

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

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

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

  5. POJ 2135 Farm Tour (费用流)

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

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

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

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

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

  8. POJ 2135 Farm Tour 最小费用流

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

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

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

随机推荐

  1. JS post提交表单

    js post方式提交表单有两种办法,1:AJAX提交 2:在JS里拼出一个form,然后submit 第二种办法的代码 //这个主要是解决给password MD5 var email = 'ema ...

  2. ubuntu下root和安装mysql

    sudo password创建新的root密码: 1.用当前登录用户打开终端,在终端输入命令 sudo passwd,输入当前用户的密码然后回车 2.会提示输入新密码,输入完成后回车(http://w ...

  3. Eclipse添加SVN插件:导入项目+上传项目+更新项目

    首先在Eclipse中安装SVN插件,方法同安装Pydev相同 首先点击help,然后点击Install New Software 然后在弹出的窗口中点击Add,再在新弹出的窗口中的url栏输入如下内 ...

  4. 011.KVM-V2V迁移

    一 虚拟化存储池 1.1 创建虚拟化存储池 [root@kvm-host ~]# mkdir -p /data/vmfs 1.2 定义存储池与目录 [root@kvm-host ~]# virsh p ...

  5. JSONObject 自定义过滤配置

    一.自定义过滤器说明 PropertyPreFilter 根据PropertyName判断是否序列化  PropertyFilter 根据PropertyName和PropertyValue来判断是否 ...

  6. jstat命令总结

    jvm统计信息监控工具 一. jstat是什么 jstat是JDK自带的一个轻量级小工具.全称"Java Virtual Machine statistics monitoring tool ...

  7. 初识thinkphp(2)

    thinkphp的url路径的表示格式为 http://ip/tp/public/index.php/模块/控制器/操作 这里url最后的操作就是类里面的函数. 0x01:url访问格式 官方文档中有 ...

  8. 漏洞利用查询工具sandi

    漏洞利用查询工具sandi   在渗透测试中,一旦发现漏洞,就需要查找该漏洞的利用方式.由于漏洞众多,就需要渗透测试人员从海量的漏洞信息找出可用攻击载荷.Kali Linux内置了一个查询工具sand ...

  9. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem A. Alien Visit 计算几何

    Problem A. Alien Visit 题目连接: http://codeforces.com/gym/100714 Description Witness: "First, I sa ...

  10. VMware 使用本机代理上网

    灰机使用方法 VMware 安装方法 首先解决主机的配置 1.查询本机 IP 地址,使用 ipconfig /all 2.更改小灰机的设置 3.虚拟机设置 4.Ubuntu 设置