WuKong

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

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
3

Hint: 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.

 
Source
题意:
给出n个点m条边和两对起点和终点s1,e1;s2,e2,分别求其最短路,问他们的最短路中最多有多少个点是相同的。
代码:
/*
如果两条最短路有公共点,公共点一定是连续的。因此只要找两条最短路最长的公共子序列就行。floyd算出每两点之间的最短路
如果s1,e1与s2,e2之间都存在一个最长的路径mp[i][j]满足mp[s1/s2][i]+mp[i][j]+mp[j][e1/e2]==mp[s1/s2][e1/e2],则i到j的
长度就是答案,只要枚举找到这个中间量即可。
*/
#include<iostream>
#include<cstdio>
using namespace std;
const int MAX=;
int mp[][],num[][],n,m;
int a,b,c,s1,s2,e1,e2;
int main()
{
while(scanf("%d%d",&n,&m)&&(n+m)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(i==j){
mp[i][j]==;num[i][j]=;
}
else {mp[i][j]=MAX;num[i][j]=;}
}
for(int i=;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
if(mp[a][b]>c)
mp[a][b]=mp[b][a]=c;
}
scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(mp[i][j]>mp[i][k]+mp[k][j]){
mp[i][j]=mp[i][k]+mp[k][j];
num[i][j]=num[i][k]+num[k][j]-;
}
else if(mp[i][j]==mp[i][k]+mp[k][j])
num[i][j]=num[i][k]+num[k][j]-;
}
}
}
int tmp=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if((mp[s1][e1]==mp[s1][i]+mp[i][j]+mp[j][e1])&&
(mp[s2][e2]==mp[s2][i]+mp[i][j]+mp[j][e2])&&(num[i][j]>tmp))
tmp=num[i][j];
}
}
printf("%d\n",tmp);
}
return ;
}

HDU2833 最短路 floyd的更多相关文章

  1. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  2. 模板C++ 03图论算法 2最短路之全源最短路(Floyd)

    3.2最短路之全源最短路(Floyd) 这个算法用于求所有点对的最短距离.比调用n次SPFA的优点在于代码简单,时间复杂度为O(n^3).[无法计算含有负环的图] 依次扫描每一点(k),并以该点作为中 ...

  3. 最短路 - floyd算法

    floyd算法是多源最短路算法 也就是说,floyd可以一次跑出所以点两两之间的最短路 floyd类似动态规划 如下图: 用橙色表示边权,蓝色表示最短路 求最短路的流程是这样的: 先把点1到其他点的最 ...

  4. HDU1869---(最短路+floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=1869 思路:最短路+floyd 分析:1 题目是要求所有的数据能否满足“六度分离”,那么我们就想到所有点之间的最 ...

  5. 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...

  6. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  7. poj 3613 经过k条边最短路 floyd+矩阵快速幂

    http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...

  8. 最短路--floyd算法模板

    floyd算法是求所有点之间的最短路的,复杂度O(n3)代码简单是最大特色 #include<stdio.h> #include<string.h> ; const int I ...

  9. poj 3216 Repairing Company(最短路Floyd + 最小路径覆盖 + 构图)

    http://poj.org/problem?id=3216 Repairing Company Time Limit: 1000MS   Memory Limit: 131072K Total Su ...

随机推荐

  1. (转) GEM透视阴影贴图

    转载:小道 透视阴影贴图(Perspective Shadow Maps, PSMs)是由Stamminger和Drettakis在 SIGGRAPH 2002上提出的一种阴影贴图(Shadow Ma ...

  2. [HNOI2018]寻宝游戏(题解转载自别处)

    题解(自别处转载): Luogu CSDN 这题关键是将运算符也替换成0,1 然后在运算符与原串混杂里找规律. 而且替换的方式也有所要求,考场上两种替换方式都要尝试. #include <bit ...

  3. LeetCode 386——字典序排数

    1. 题目 2. 解答 2.1 方法一 假设返回 118 以内数的字典顺序,则为 1,10,100,101,102,...,109,11,110,111,112,...,118,12,13,....根 ...

  4. day-18 滑动平均模型测试样例

    为了使训练模型在测试数据上有更好的效果,可以引入一种新的方法:滑动平均模型.通过维护一个影子变量,来代替最终训练参数,进行训练模型的验证. 在tensorflow中提供了ExponentialMovi ...

  5. 如何让thinkpad X1C 用U盘 安装上专业版win10

    1 BIOS内置了文件 会导致win10 iso默认装家庭版 2 给iso 的resouse 目录中增加文件ei.cfg 3 内容如下 [EditionID]Professional[Channel] ...

  6. Java经典问题

    1.JAVA初学者都应该搞懂的问题 对于这个系列里的问题,每个学Java的人都应该搞懂.当然,如果只是学Java玩玩就无所谓了.如果你认为自己已经超越初学者了,却不很懂这些问题,请将你自己重归初学者行 ...

  7. [C++] OOP - Virtual Functions and Abstract Base Classes

    Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...

  8. CP文件覆盖问题

    # \cp -r -a aaa/* /bbb[这次是完美的,没有提示按Y.传递了目录属性.没有略过目录]

  9. JS中Document节点总结

    document对象是documentHTML的一个实例,也是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. Document节点的子节点可以是DocumentTy ...

  10. 学霸系统ui部分软件发布说明

    一.版本新功能 1.搜索主页按钮 在学长的版本中,要想进入搜索主页,方法只有一个,就是在问答搜索结果页中点击链接进入搜索主页,这就使得用户很不方便.我们在学霸系统的主页增加了一个链接按钮,方便用户能够 ...