Problem Description
A group of thieves is approaching a museum in the country of zjsxzy,now they are in city A,and the museum is in city B,where keeps many broken legs of zjsxzy.Luckily,GW learned the conspiracy when he is watching stars and told it to zjsxzy. Zjsxzy decided to caught these thieves,and he let the police to do this,the police try to catch them on their way from A to B. Although the thieves might travel this way by more than one group, zjsxzy's excellent police has already gather the statistics that the cost needed on each road to guard it. Now ,zjsxzy's conutry can be described as a N*N matrix A,Aij indicates the city(i,j) have bidirectionals road to city(i+1,j) and city(i,j+1),gurad anyone of them costs Aij. Now give you the map,help zjsxzy to calculate the minimium cost.We assume thieves may travel in any way,and we will catch all passing thieves on a road if we guard it.
 
Input
The first line is an integer T,followed by T test cases. In each test case,the first line contains a number N(1<N<=400). The following N lines,each line is N numbers,the jth number of the ith line is Aij. The city A is always located on (1,1) and the city B is always located on (n,n). Of course,the city (i,j) at the last row or last line won't have road to (i,j+1) or (i+1,j).
 
Output
For each case,print a line with a number indicating the minimium cost to arrest all thieves.
 
Sample Input
1
3
10 5 5
6 6 20
4 7 9
 
Sample Output
18

解析:平面图最小割,把面当成点,然后用最短路求最小割,详见百度

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<string>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<queue>
  7. using namespace std;
  8. typedef __int64 LL;
  9. const LL INF=;
  10. const int maxn=**;
  11. int N,eid,head[maxn];
  12. struct edge{ int v,w,next; }E[*maxn];
  13. void AddEdge(int u,int v,int w)
  14. {
  15. E[++eid].v=v; E[eid].w=w; E[eid].next=head[u]; head[u]=eid;
  16. E[++eid].v=u; E[eid].w=w; E[eid].next=head[v]; head[v]=eid;
  17. }
  18. int f(int x,int y){ return x*(N-)+y; }
  19. struct node
  20. {
  21. int u;
  22. LL d;
  23. node(int u=,LL d=):u(u),d(d){}
  24. bool operator < (const node& t) const{ return d>t.d; }
  25. };
  26. priority_queue<node> que;
  27. LL D[maxn];
  28. bool vis[maxn];
  29. int Dij(int S,int T)
  30. {
  31. while(!que.empty()) que.pop();
  32. for(int i=;i<maxn;i++) D[i]=INF;
  33. D[S]=;
  34. memset(vis,false,sizeof(vis));
  35. que.push(node(S,));
  36. while(!que.empty())
  37. {
  38. node t=que.top(); que.pop();
  39. int u=t.u;
  40. if(vis[u]) continue;
  41. vis[u]=true;
  42. for(int i=head[u];i!=-;i=E[i].next)
  43. {
  44. edge& e=E[i];
  45. int v=e.v,w=e.w;
  46. if(D[v]>D[u]+w)
  47. {
  48. D[v]=D[u]+w;
  49. que.push(node(v,D[v]));
  50. }
  51. }
  52. }
  53. return D[T];
  54. }
  55. int main()
  56. {
  57. int T;
  58. scanf("%d",&T);
  59. while(T--)
  60. {
  61. scanf("%d",&N);
  62. int s=,t=(N-)*(N-)+;
  63. memset(head,-,sizeof(head));
  64. eid=;
  65. int u,v,w;
  66. for(int i=;i<=N;i++)
  67. for(int j=;j<=N;j++)
  68. {
  69. scanf("%d",&w);
  70. if(i==&&j<N)
  71. {
  72. u=s; v=j;
  73. AddEdge(u,v,w);
  74. }
  75. if(i==N&&j<N)
  76. {
  77. u=f(i-,j); v=t;
  78. AddEdge(u,v,w);
  79. }
  80. if(<i&&i<N&&j<N)
  81. {
  82. u=f(i-,j);
  83. v=f(i-,j);
  84. AddEdge(u,v,w);
  85. }
  86. if(j==&&i<N)
  87. {
  88. u=f(i-,j); v=t;
  89. AddEdge(u,v,w);
  90. }
  91. if(j==N&&i<N)
  92. {
  93. u=f(i-,j-); v=s;
  94. AddEdge(u,v,w);
  95. }
  96. if(<j&&j<N&&i<N)
  97. {
  98. u=f(i-,j-);
  99. v=f(i-,j);
  100. AddEdge(u,v,w);
  101. }
  102. }
  103. printf("%d\n",Dij(s,t));
  104. }
  105. return ;
  106. }

hdu3870-Catch the Theves(平面图最小割)的更多相关文章

  1. tyvj P1209 - 拦截导弹 平面图最小割&&模型转化

    P1209 - 拦截导弹 From admin    Normal (OI)总时限:6s    内存限制:128MB    代码长度限制:64KB 背景 Background 实中编程者联盟为了培养技 ...

  2. [BZOJ 2007] [Noi2010] 海拔 【平面图最小割(对偶图最短路)】

    题目链接:BZOJ - 2007 题目分析 首先,左上角的高度是 0 ,右下角的高度是 1.那么所有点的高度一定要在 0 与 1 之间.然而选取 [0, 1] 的任何一个实数,都可以用整数 0 或 1 ...

  3. B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij

    B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij 题意:城市被东西向和南北向的主干道划分为n×n个区域.城市中包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向 ...

  4. 【平面图最小割】BZOJ1001- [BeiJing2006]狼抓兔子

    [题目大意]左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)<==>(x+1,y) 2:(x,y)<==>(x,y+1) ...

  5. 【平面图最小割】BZOJ2007-[NOI2010]海拔

    [题目大意] 城市被东西向和南北向的主干道划分为n×n个区域,包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向道路.现得到了每天每条道路两个方向的人流量.每一个交叉路口都有海拔,每向上爬h ...

  6. BZOJ 2007 海拔(平面图最小割转对偶图最短路)

    首先注意到,把一个点的海拔定为>1的数是毫无意义的.实际上,可以转化为把这些点的海拔要么定为0,要么定为1. 其次,如果一个点周围的点的海拔没有和它相同的,那么这个点的海拔也是可以优化的,即把这 ...

  7. bzoj 1001 狼抓兔子 —— 平面图最小割(最短路)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 平面图最小割可以转化成最短路问题: 建图时看清楚题目的 input ... 代码如下: ...

  8. 【BZOJ1001】狼抓兔子(平面图最小割转最短路)

    题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路  1:(x,y)<==>(x+1,y ...

  9. BZOJ2007/LG2046 「NOI2010」海拔 平面图最小割转对偶图最短路

    问题描述 BZOJ2007 LG2046 题解 发现左上角海拔为 \(0\) ,右上角海拔为 \(1\) . 上坡要付出代价,下坡没有收益,所以有坡度的路越少越好. 所以海拔为 \(1\) 的点,和海 ...

随机推荐

  1. <php>删除(有内容的)文件夹函数程序

    function deldir($dirname) { if(!file_exists($dirname)) {//判断文件夹是否存在 die("文件夹不存在!");//作用等于( ...

  2. github atom 试用

    github的编辑器atom 1.0已经出来了,在https://atom.io/ 我之前在win上一直用notepad++写了两年脚本.最近改写lua了,项目组统一用的sublime text.su ...

  3. JavaScript 判断一个字符串是否在另一个字符串中

    传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. start ...

  4. WebSphere之wasprofile.sh使用

    概要文件(profile) 6.0版本以后才有profile,目的是将用户数据和was本身的文件分开,这样可以定义多个profile,每个profile相当于一个用户,相当于提供了多用户的支持. pr ...

  5. javasscript学习笔记 之 数组学习二 数组的所有方法

    1.push() 和 pop()  栈的方法 后进先出 push() 该方法是向数组末尾添加一个或者多个元素,并返回新的长度. push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返 ...

  6. openssl 非对称加密算法DSA命令详解

    1.DSA算法概述 DSA算法是美国的国家标准数字签名算法,它只能用户数字签名,而不能用户数据加密和密钥交换. DSA与RSA的生成方式不同,RSA是使用openssl提供的指令一次性的生成密钥(包括 ...

  7. 【转】深入理解篇UIScrollerView

    转自:http://www.mamicode.com/info-detail-1144770.html 接下来,我整理一下自己的思路,深入理解 UIScrollView 基本点 : 1 . UIScr ...

  8. Javascript进阶篇——总结--DOM案例+选项卡效果

    断断续续的把慕课的JavaScript基础和进阶看完了,期间不怎么应用有的都忘记了,接下来多开始写些效果,进行实际应用. 制作一个表格,显示班级的学生信息. 1. 鼠标移到不同行上时背景色改为色值为 ...

  9. AutoMapper2

    1.嵌套映射 namespace Second { class Program { static void Main(string[] args) { Mapper.CreateMap<Oute ...

  10. 点击<a>标签,禁止页面自动跳到顶部的解决办法

       最近在开发一个小web的时候想给一个按钮增加一个弹出dialog功能,但是发现点击按钮后页面总是自动滚动至顶部,这点从用户体验上来讲是极其不爽的,于是开始跳进google大池寻求解决办法.网上的 ...