Barricade

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns and M roads, and each road has the same length and connects two towns. The town numbered 1 is where general's castle is located, and the town numbered N is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i-th road requires wi units of wood. Because of lacking resources, you need to use as less wood as possible.
 
Input
The first line of input contains an integer t, then t test cases follow.
For each test case, in the first line there are two integers N(N≤1000) and M(M≤10000).
The i-the line of the next M lines describes the i-th edge with three integers u,v and w where 0≤w≤1000 denoting an edge between u and v of barricade cost w.
 
Output
For each test cases, output the minimum wood cost.
 
Sample Input
1
4 4
1 2 1
2 4 2
3 1 3
4 3 4
 
Sample Output
4
分析:对最短路求最小割最大流即可;
代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <vector>
  14. #include <list>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  17. #define mod 1000000007
  18. #define inf 0x3f3f3f3f
  19. #define vi vector<int>
  20. #define pb push_back
  21. #define mp make_pair
  22. #define fi first
  23. #define se second
  24. #define ll long long
  25. #define pi acos(-1.0)
  26. #define pii pair<int,int>
  27. #define Lson L, mid, rt<<1
  28. #define Rson mid+1, R, rt<<1|1
  29. const int maxn=1e3+;
  30. using namespace std;
  31. ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  32. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  33. int n,m,k,t,h[maxn],tot,vis[maxn],s,cur[maxn],f[maxn],d[maxn],g[maxn];
  34. vi edge[maxn];
  35. struct Node
  36. {
  37. int x,y,z;
  38. Node(){}
  39. Node(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
  40. }op[];
  41. struct node
  42. {
  43. int to,nxt,cap,flow;
  44. }e[<<];
  45. void add(int x,int y,int z)
  46. {
  47. e[tot].to=y;
  48. e[tot].nxt=h[x];
  49. e[tot].cap=z;
  50. e[tot].flow=;
  51. h[x]=tot++;
  52. e[tot].to=x;
  53. e[tot].nxt=h[y];
  54. e[tot].flow=;
  55. h[y]=tot++;
  56. }
  57. bool bfs()
  58. {
  59. memset(vis,,sizeof vis);
  60. queue<int>p;
  61. p.push(s);
  62. vis[s]=;
  63. while(!p.empty())
  64. {
  65. int x=p.front();p.pop();
  66. for(int i=h[x];i!=-;i=e[i].nxt)
  67. {
  68. int to=e[i].to,cap=e[i].cap,flow=e[i].flow;
  69. if(!vis[to]&&cap>flow)
  70. {
  71. vis[to]=vis[x]+;
  72. p.push(to);
  73. }
  74. }
  75. }
  76. return vis[t];
  77. }
  78. void pr_bfs(int s)
  79. {
  80. int i;
  81. memset(d,inf,sizeof d);
  82. memset(vis,,sizeof vis);
  83. queue<int>p;p.push(s);vis[s]=;d[s]=;
  84. while(!p.empty())
  85. {
  86. int q=p.front();p.pop();vis[q]=;
  87. for(int x:edge[q])
  88. {
  89. if(d[x]>d[q]+)
  90. {
  91. d[x]=d[q]+;
  92. if(!vis[x])p.push(x),vis[x]=;
  93. }
  94. }
  95. }
  96. if(s==n)rep(i,,n)f[i]=d[i];
  97. else rep(i,,n)g[i]=d[i];
  98. return;
  99. }
  100. int dfs(int x,int a)
  101. {
  102. if(x==t||a==)return a;
  103. int ans=,j;
  104. for(int&i=cur[x];i!=-;i=e[i].nxt)
  105. {
  106. int to=e[i].to,cap=e[i].cap,flow=e[i].flow;
  107. if(vis[to]==vis[x]+&&(j=dfs(to,min(a,cap-flow)))>)
  108. {
  109. e[i].flow+=j;
  110. e[i^].flow-=j;
  111. ans+=j;
  112. a-=j;
  113. if(a==)break;
  114. }
  115. }
  116. return ans;
  117. }
  118. int max_flow(int s,int t)
  119. {
  120. int flow=,i;
  121. while(bfs())
  122. {
  123. memcpy(cur,h,sizeof cur);
  124. flow+=dfs(s,inf);
  125. }
  126. return flow;
  127. }
  128. int main()
  129. {
  130. int i,j,test;
  131. scanf("%d",&test);
  132. while(test--)
  133. {
  134. tot=;
  135. memset(h,-,sizeof h);
  136. scanf("%d%d",&n,&m);
  137. rep(i,,n)edge[i].clear();
  138. rep(i,,m-)
  139. {
  140. int a,b,c;
  141. scanf("%d%d%d",&a,&b,&c);
  142. op[i]=Node(a,b,c);
  143. edge[a].pb(b),edge[b].pb(a);
  144. }
  145. pr_bfs(n);
  146. pr_bfs();
  147. rep(i,,m-)
  148. {
  149. int a=op[i].x,b=op[i].y,c=op[i].z;
  150. if(f[a]+g[b]+==f[])add(a,b,c);
  151. if(f[b]+g[a]+==f[])add(b,a,c);
  152. }
  153. s=n,t=;
  154. printf("%d\n",max_flow(s,t));
  155. }
  156. //system("Pause");
  157. return ;
  158. }
 

2016青岛网络赛 Barricade的更多相关文章

  1. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  2. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  3. HDU - 5878 2016青岛网络赛 I Count Two Three(打表+二分)

    I Count Two Three 31.1% 1000ms 32768K   I will show you the most popular board game in the Shanghai ...

  4. HDU - 5887 2016青岛网络赛 Herbs Gathering(形似01背包的搜索)

    Herbs Gathering 10.76% 1000ms 32768K   Collecting one's own plants for use as herbal medicines is pe ...

  5. HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)

    背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即 if(val + (LD)pk[d].val / (LD)pk[d]. ...

  6. HDU5880 Family View(2016青岛网络赛 AC自动机)

    题意:将匹配的串用'*'代替 tips: 1 注意内存的使用,据说g++中指针占8字节,c++4字节,所以用g++交会MLE 2 注意这种例子, 12abcdbcabc 故失败指针要一直往下走,否则会 ...

  7. 2016青岛网络赛 Sort

    Sort Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Des ...

  8. 2016青岛网络赛 The Best Path

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Pr ...

  9. 2016 年青岛网络赛---Family View(AC自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5880 Problem Description Steam is a digital distribut ...

随机推荐

  1. 高级CSS

    target="_black"新窗口打开,且打开第二个链接出来的页面覆盖第一个页面 .ui-pg-table td[dir="ltr"]{*width:94px ...

  2. kloxo面板教程-折腾了一天

    ------------------------------------------------------------------------------- 前一晚安装了掉线,不得不重新来,有点慢, ...

  3. find排除目录

    在linux find 进行查找的时候,有时候需要忽略某些目录不查找,可以使用 -prune 参数来进行过滤,但必须要注意要忽略的路径参数必须紧跟着搜索的路径之后,否则该参数无法起作用. 命令语法: ...

  4. Python使用os.listdir()函数来获得目录中的内容

    摘自:http://it.100xuexi.com/view/otdetail/20130423/057606dc-7ad1-47e4-8ea6-0cf75f514837.html   1.在Pyth ...

  5. Html:upload

    文件上传框 有时候,需要用户上传自己的文件,文件上传框看上去和其它 文本域差不多,只是它还包含了一个浏览按钮.访问者可以通 过输入需要上传的文件的路径或者点击浏览按钮选择需要上传 的文件. 代码格式: ...

  6. Xcode6无法用xib得问题解决方法

    1.创建一个新工程,选择singleView application 2.将storyboard和launchscreen删除,选择moveToTrash 3.删除info.plist文件中Main ...

  7. Nginx配置文件nginx.conf中文详解【转】

    PS:Nginx使用有两三年了,现在经常碰到有新用户问一些很基本的问题,我也没时间一一回答,今天下午花了点时间,结合自己的使用经验,把Nginx的主要配置参数说明分享一下,也参考了一些网络的内容,这篇 ...

  8. GsonFormat 报错

    GsonFormat原来也有bug 我是用GsonFormat来生成java bean的,但是运行起来居然报 Caused by: java.lang.NumberFormatException: E ...

  9. PatrolRobot(UVa1600)BFS

    PatrolRobot(UVa1600)BFS 珉黻郐距 河吏蝉醉 闵棵黏言 芤她之瞌 褰上稽莨 錾傻奉 郦玫睃芩 摇摇头还没回答魏海洪就抢先回答道:呵呵你们几个别试 蚰镉氡 钬 绦可 ...

  10. ASP.NET 会话状态的模式

    ASP.NET 会话状态为会话数据提供了几个不同的存储选项.每个选项都通过一个 SessionStateMode 枚举值进行识别.如下列表中描述了可用的会话状态模式: InProc 模式:把会话状态存 ...