Marriage Match IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3147    Accepted Submission(s): 946

Problem Description
Do not sincere non-interference。
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.

So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?

 
Input
The first line is an integer T indicating the case number.(1<=T<=65)
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.

Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.

At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.

 
Output
Output a line with a integer, means the chances starvae can get at most.
 
Sample Input
  1.  
3 7 8 1 2 1 1 3 1 2 4 1 3 4 1 4 5 1 4 6 1 5 7 1 6 7 1 1 7 6 7 1 2 1 2 3 1 1 3 3 3 4 1 3 5 1 4 6 1 5 6 1 1 6 2 2 1 2 1 1 2 2 1 2
 
Sample Output
2 1 1
 
题意:
有n个城市,m条边,a到b耗费为c,为单向边。要求从s到t的最短路径有多少条,每一条边只能走一次。
 
思路:
如果每天边不一定走一次的话,那么可以通过树形dp来求解。但是这里每条边只能走一次,也就是说每条路径上面的边的流量为1,从起点到终点求一次最大流即可。这样题目就能够用最大流来解决。对于建立新的图,可以先从终点到起点求一次最短路,然后从起点开始dfs,并且维护now[]数组,表示从起点到这个点的路径长度,
如果now[rt] + dis[t] + edge[i].val == dis[S](dis[]的起点是原本图中的终点),那么说明这两个点是最短路上的点,那么可以连边,流量为1,。跑一次最大流解决问题了。
 
  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<string>
  7. #include<time.h>
  8. #include<vector>
  9. #include<cstdio>
  10. #include<cstring>
  11. #include<iostream>
  12. #include<algorithm>
  13. #define INF 1000000001
  14. #define ll long long
  15. #define lson l,m,rt<<1
  16. #define rson m+1,r,rt<<1|1
  17. using namespace std;
  18. const int MAXN = ;
  19. struct node
  20. {
  21. int to;
  22. int val;
  23. int next;
  24. }edge[MAXN**],e[MAXN**];
  25. int ind,pre[MAXN],vis[MAXN],dis[MAXN],pre1[MAXN],ind1;
  26. int now[MAXN],S,T;
  27. int n,m;
  28. void add1(int x,int y,int z)
  29. {
  30. e[ind1].to = y;
  31. e[ind1].val = z;
  32. e[ind1].next = pre1[x];
  33. pre1[x] = ind1 ++;
  34. }
  35. void spfa()
  36. {
  37. for(int i = ; i <= n; i++){
  38. dis[i] = INF;
  39. vis[i] = ;
  40. }
  41. vis[T] = ;
  42. dis[T] = ;
  43. queue<int>q;
  44. q.push(T);
  45. while(!q.empty()){
  46. int tp = q.front();
  47. q.pop();
  48. vis[tp] = ;
  49. for(int i = pre1[tp]; i != -; i = e[i].next){
  50. int t = e[i].to;
  51. if(dis[t] > dis[tp] + e[i].val){
  52. dis[t] = dis[tp] + e[i].val;
  53. if(!vis[t]){
  54. vis[t] = ;
  55. q.push(t);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. void add(int x,int y,int z)
  62. {
  63. edge[ind].to = y;
  64. edge[ind].val = z;
  65. edge[ind].next = pre[x];
  66. pre[x] = ind ++;
  67. }
  68. void dfs1(int rt)
  69. {
  70. vis[rt] = ;
  71. if(rt == T)return ;
  72. for(int i = pre1[rt]; i != -; i = e[i].next){
  73. int t = e[i].to;
  74. if(now[rt] + dis[t] + e[i].val == dis[S]){
  75. now[t] = now[rt] + e[i].val;
  76. add(rt,t,);
  77. add(t,rt,);
  78. if(!vis[t]){
  79. dfs1(t);
  80. }
  81. }
  82. }
  83. }
  84. int bfs()
  85. {
  86. memset(vis,-,sizeof(vis));
  87. queue<int>q;
  88. vis[S] = ;
  89. q.push(S);
  90. while(!q.empty()){
  91. int tp = q.front();
  92. q.pop();
  93. for(int i = pre[tp]; i != -; i = edge[i].next){
  94. int t = edge[i].to;
  95. if(vis[t] == - && edge[i].val){
  96. vis[t] = vis[tp] + ;
  97. q.push(t);
  98. }
  99. }
  100. }
  101. if(vis[T] == -)return ;
  102. return ;
  103. }
  104. int dfs(int rt,int low)
  105. {
  106. int used = ;
  107. if(rt == T)return low;
  108. for(int i = pre[rt]; i != - && used < low; i = edge[i].next){
  109. int t = edge[i].to;
  110. if(vis[t] == vis[rt] + && edge[i].val){
  111. int a = dfs(t,min(low-used,edge[i].val));
  112. used += a;
  113. edge[i].val -= a;
  114. edge[i^].val += a;
  115. }
  116. }
  117. if(used == )vis[rt] = -;
  118. return used;
  119. }
  120. int x[MAXN*],y[MAXN*],z[MAXN*];
  121. void Init(int flag)
  122. {
  123. ind1 = ;
  124. memset(pre1,-,sizeof(pre1));
  125. for(int i = ; i <= m; i++){
  126. if(!flag){
  127. add1(y[i],x[i],z[i]);
  128. }
  129. else {
  130. add1(x[i],y[i],z[i]);
  131. }
  132. }
  133. }
  134. int main()
  135. {
  136. int t;
  137. scanf("%d",&t);
  138. while(t--){
  139. scanf("%d%d",&n,&m);
  140. for(int i = ; i <= m; i++){
  141. scanf("%d%d%d",&x[i],&y[i],&z[i]);
  142. }
  143. Init();
  144. scanf("%d%d",&S,&T);
  145. spfa();
  146. Init();
  147. ind = ;
  148. memset(now,,sizeof(now));
  149. memset(pre,-,sizeof(pre));
  150. dfs1(S);
  151. int ans = ;
  152. while(bfs()){
  153. while(){
  154. int a = dfs(S,INF);
  155. if(!a)break;
  156. ans += a;
  157. }
  158. }
  159. printf("%d\n",ans);
  160. }
  161. return ;
  162. }

hdu3416 判断最短路是否唯一(每条边只能走一次)的更多相关文章

  1. poj 1679 Prim判断次短路

    题意:判断最短路是否唯一. 思路:先prrim一次求出最短路同时记录最短路加入的边: 然后枚举所求边,将其删除再求n-1次prim,判断再次所求得的最短路与第一次求得的次短路的关系. 代码: #inc ...

  2. ZOJ - 2587 Unique Attack (判断最小割是否唯一)

    题意:判断最小割是否唯一. 分析:跑出最大流后,在残余网上从源点和汇点分别dfs一次,对访问的点都打上标记. 若还有点没有被访问到,说明最小割不唯一. https://www.cnblogs.com/ ...

  3. CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数

    题意: 给出n个点m条公路k条铁路. 接下来m行 u v w      //u->v 距离w 然后k行 v w         //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...

  4. hdu-3416(最短路+网络流)

    题意:给你一个有向权图,问你从S到E有几条最短路,每条边直走一次的情况下: 解题思路:每条边直走一次,最大流边权为1,因为要算几条最短路,那么能得到最短路的路径标记下,然后跑最大流 代码: #incl ...

  5. 判断强联通图中每条边是否只在一个环上(hdu3594)

    hdu3594 Cactus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. hdu3416+hdu6582(最短路+最大流)

    题意 hdu3416: 给一个图,边不能重复选,问有多少个最短路 hdu6582: 给一个图,问最少删除边权多少的边后,最短路长度增加 分析 边不能重复选这个条件可以想到边权为1,跑最大流,所以我们可 ...

  7. POJ 1679 判断最小树是否唯一

    题意:       给你一个图,问你最小树是否唯一,唯一则输出最小数的权值,不唯一输出Not Unique! 思路:      题目问的是最小树是否唯一,其实也就是在问次小树是否等于最小树,如果等于则 ...

  8. mysql中判断表中是否存在某条记录

    SELECT CASE WHEN EXISTS (SELECT * FROM usergroupmap WHERE groupId = groupIdIn AND userId = v_friendI ...

  9. objectarx之判断三点是否在一条直线上

    bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...

随机推荐

  1. 在虚拟机中安装CentOS7

    在虚拟机中安装CentOS7 听语音 | 浏览:17352 | 更新:2014-10-31 12:14 1 2 3 4 5 6 7 分步阅读 一键约师傅 百度师傅最快的到家服务,最优质的电脑清灰! 百 ...

  2. 转: windows 10使用原生linux bash命令行

    转: https://www.zybuluo.com/pandait/note/337430 windows 10使用原生linux bash命令行 linux bash windows-10 第一时 ...

  3. Tree

    //Header.h #ifndef _HEAD_ #define _HEAD_ #include <queue> #include <iostream> using name ...

  4. php常用自定义函数

    1,写一个函数,算出两个文件的相对路径 有两种方法,一种是利用array的相关方法,如例1,另外一种是使用?:;运算符 先看第一种方法 function getrelativepath2($path1 ...

  5. HP “云图”GPU虚拟化工作站解决方案

    HP PCS ”云图”GPU虚拟化工作站解决方案 ——将图形计算从桌面移到数据中心 惠普云图形GPU虚拟化桌面系统是以用户为中心的私有云服务.除了保留了传统桌面虚拟化方案以集中设备为中心统一管理等优点 ...

  6. NET Core项目定义Item Template

    NET Core项目定义Item Template 作为这个星球上最强大的IDE,Visual Studio不仅仅提供了很多原生的特性,更重要的是它是一个可定制的IDE,比如自定义Project Te ...

  7. nfs客户端报错解决Stale file handle

    NFS故障: 场景:客户端挂载是好的.服务端磁盘满了,重新给挂了一快.客户端df -h nfs挂载消失. 客户端报错:Stale file handle 现象如下: [root@test63-spri ...

  8. Linux下源码安装ffmpeg及ffmpeg的简单使用说明

    一.编译安装 ffmpeg在安装时依赖的包和版本都很让人头疼,不同编译环境也各不相同.公司之前封装了一个又各种出错. 其实办法很简单,就是到官网一步一步按着做就行了:http://trac.ffmpe ...

  9. Android 的图片异步请求加三级缓存 ACE

    使用xUtils等框架是很方便,但今天要用代码实现bitmapUtils 的功能,很简单, 1 AsyncTask请求一张图片 ####AsyncTask #####AsyncTask是线程池+han ...

  10. visual studio 2012 的制作ActiveX、打包和发布

    开发环境是Vs 2012  Framework 4.0 源码和制作工具在文章最下边 一. ActiveX控件Demo 新建一个Window窗体控件库项目 在自动生成的UserControl1页面上添加 ...