WuKong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Liyuan wanted to rewrite the famous book “Journey to the West” (“Xi You Ji” in Chinese pinyin). In the original book, the Monkey King Sun Wukong was trapped by the Buddha for 500 years, then he was rescued by Tang Monk, and began his journey to the west. Liyuan thought it is too brutal for the monkey, so he changed the story:

One day, Wukong left his home - Mountain of Flower and Fruit, to the Dragon   King’s party, at the same time, Tang Monk left Baima Temple to the Lingyin Temple to deliver a lecture. They are both busy, so they will choose the shortest path. However, there may be several different shortest paths between two places. Now the Buddha wants them to encounter on the road. To increase the possibility of their meeting, the Buddha wants to arrange the two routes to make their common places as many as possible. Of course, the two routines should still be the shortest paths.

Unfortunately, the Buddha is not good at algorithm, so he ask you for help.

Input
There are several test cases in the input. The first line of each case contains the number of places N (1 <= N <= 300) and the number of roads M (1 <= M <= N*N), separated by a space. Then M lines follow, each of which contains three integers a b c, indicating there is a road between place a and b, whose length is c. Please note the roads are undirected. The last line contains four integers A B C D, separated by spaces, indicating the start and end points of Wukong, and the start and end points of Tang Monk respectively.

The input are ended with N=M=0, which should not be processed.

Output
Output one line for each case, indicating the maximum common points of the two shortest paths.
Sample Input
6 6 1 2 1 2 3 1 3 4 1 4 5 1 1 5 2 4 6 3 1 6 2 4 0 0
Sample Output
3Hint: One possible arrangement is (1-2-3-4-6) for Wukong and (2-3-4) for Tang Monk. The number of common points are 3.
题意:求两条最短路之间公共点的个数。
解题思路:这道题如果考虑的是普通的单源最短路,那会变得很难处理,我之前就是这么想的,无奈解不出来。这道题让我见识到了Floyd的强大,Floyd算法不仅要记住它的三层循环,更要理解算法的思路。这里定义的dp[i][j]为i->j的最短路上最多有多少个点。如何更新dp[i][j]是一个问题,但如果熟悉Floyd算法思想的话,那么它可以直接用Floyd更新即可。接下来是如何两条路的最多公共点,这里采用的是这样的策略:如果说map[s][i] + map[i][j] + map[j][e] == map[s][e],这意味着s->e的最短路必定经过i->j的最短路,那我们只需要枚举两条路的公共最短路即可。这个题确实是好题,熟悉Floyd算法的核心思想是关键。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int maxn = 305;
const int inf = 0x3f3f3f3f;
int n,m,dp[maxn][maxn],map[maxn][maxn];void floyd()
{for(int k = 1; k <= n; k++)for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++){if(map[i][j] > map[i][k] + map[k][j]){map[i][j] = map[i][k] + map[k][j];dp[i][j] = dp[i][k] + dp[k][j] - 1;}else if(map[i][j] == map[i][k] + map[k][j] && dp[i][j] < dp[i][k] + dp[k][j])dp[i][j] = dp[i][k] + dp[k][j] - 1;}
}int solve(int s1,int e1,int s2,int e2)
{int res = 0;for(int i = 1; i <= n; i++)for(int j = 1; j <= n; j++)if(map[s1][i] + map[i][j] + map[j][e1] == map[s1][e1] && map[s2][i] + map[i][j] + map[j][e2] == map[s2][e2])res = max(res,dp[i][j]);return res;
}int main()
{int u,v,w,s1,e1,s2,e2;while(scanf("%d%d",&n,&m),m+n){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){map[i][j] = inf;dp[i][j] = 2;}dp[i][i] = 1;map[i][i] = 0;}for(int i = 1; i <= m; i++){scanf("%d%d%d",&u,&v,&w);map[u][v] = map[v][u] = min(map[u][v],w);}floyd();scanf("%d%d%d%d",&s1,&e1,&s2,&e2);printf("%d\n",solve(s1,e1,s2,e2));}return 0;
}

hdu 2833(Floyd + dp)相关推荐

  1. hdu 六度分离 floyd

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 题意分析:比较简单的最短路算法,最后只需判断最远两点距离是否大于7即可. /*六度分离Time ...

  2. hdu 1520 树形dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 #include<cstdio> #include<cstring> # ...

  3. hdu2833 Floyd + dp

    题意:      给你一个无向图,给你两组起点和终点,问你这两组起点和终点的最短路上最多有多少个交点... 思路:      开一个数组dp[i][j]记录最短路上i,j之间的点有多少个,这个数组是根 ...

  4. hdu 4035 可能性DP 成都网络游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 获得: 1.首先推断是不是树.事实上,所有的感觉身影,既看边数==算-1是不成立 2.有时候,我告诉孩子来 ...

  5. HDU 2836 (离散化DP+区间优化)

    Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...

  6. hdu 5568(dp+大数模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5568 官方题解: #include <cstdio> #include <cstri ...

  7. hdu 5464(简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5464 解题思路: 由于p很小,而ai很大,所以先把ai%p,由于ai可能有负数,所以ai=(ai%p+ ...

  8. hdu 5433(bfs+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5433 解题思路: dp[i][j][k]表示在(x,y)点,毅力为k时的最小体力.由于每个点可能会走多 ...

  9. HDU 2833 WuKong

    传送门 求两条最短路最多重叠的点数. 给一个无向图,再给两对起点终点,每一对起点终点之间都可能有多条最短的路,设s1和t1的最短路是r1,s2和t2的最短路是r2,求r1和r2最多能相互重叠多少个点. ...

最新文章

  1. vim 忽略大小写查找
  2. 小米redmi_99.9元!Redmi首款智能手环评测:能“打赢”小米手环5吗?
  3. 核心动画与UIView的区别
  4. php 去掉nbsp,php 正则去掉pnbsp;/p 空格 nbsp;
  5. 第四十五期:万亿级日访问量下,Redis在微博的9年优化历程
  6. 论文浅尝 | 基于迭代的概率规则约束的知识图谱分布式表示
  7. linux mysql 主从数据库_【Linux】【MySQL】MySQL主从数据库
  8. java org.jsoup does not exist_java使用Jsoup连接网站超时的解决方法
  9. 解决CentOS 6 字体变成方框的方法
  10. 堆积如山的Java面试简历,如何脱颖而出?写简历前你先要做这些
  11. mac matlab安装libsvm
  12. matlab ocx控件,matlabr.ocx控件下载
  13. MATLAB Cholesky分解
  14. Vbs脚本编程简明教程
  15. 【一起学习输入法】华宇拼音输入法开源版本解析(5)
  16. php制作600行表格,表格排版的基本操作
  17. 如何为NFT熊市做准备
  18. c语言编写fac函数 计算阶乘,编写一个计算阶乘的函数fac
  19. 企业文件加密软件如何做到根源防止泄密?全面专业的数据防泄密方案怎么选
  20. 第二篇 自制系统内核

热门文章

  1. 致客户的一封信:关于产品生命周期管理与高可用版本的提供
  2. 反馈速度小于 200ms!“弹窗”功能让你极速触达用户内心
  3. Django开发—如何重置migration
  4. 简便的chrome插件安装
  5. H盘由于IO设备错误,无法运行此项请求要怎样找到资料
  6. 探讨.NET Core数据进行3DES加密和解密问题
  7. 通过随机数生成兑换码和概率生成随机数
  8. HTML+CSS实例——漂亮的查询部件(一)
  9. 判断远程图片是否存在的JavaScript代码
  10. how to add the language things at the idiscover