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. facebook演讲

    任何为了更大愿景工作的人,可能会被称为疯子,即使你最终获得成功. 任何为了复杂问题工作的人,都会因为不能全面了解挑战而被指责,即使你不可能事先了解一切. 任何抓住主动权先行一步的人,都会因为步子太快而 ...

  2. c# 批量处理数据录入

    c# 分批处理数据录入 //using System.Text; //using System.Data; //using System.Data.SqlClient; //using System; ...

  3. 自测之Lesson7:设备文件操作

    题目:请编写一个输入密码(不回显)的程序,要求通过设置终端来完成. 完成代码: #include <stdio.h> #include <unistd.h> #include ...

  4. 静态类型&动态类型

    何时使用:使用存在继承关系的类型时,必须将一个变量或其他表达式的静态类型与该表达式表示对象的动态类型区分开来 静态类型:表达式的静态类型在编译时总是已知的,它是变量声明时的类型或表达式生成的类型 动态 ...

  5. 深搜(DFS)与广搜(BFS)区别

    最近做了不少的搜索题,时而用到DFS时而用到BFS,这里对两种搜索方法做一个总结. 广度优先搜索算法(Breadth-First-Search,缩写为 BFS),是一种利用队列实现的搜索算法.简单来说 ...

  6. Windows环境下的TensorFlow安装过程

    安装环境 Windows8.1 python3.5.x(TensorFlow only supports version 3.5.x of Python on Windows) pip 9.0.1 t ...

  7. Uncaught Error: Syntax error, unrecognized expression: |117的js错误

    117指的是js代码在浏览器运行时的出错的行号 var  a="117|117" 前面的错误是由于有特殊符号“|”,用$("txtId"+a).val();去取 ...

  8. MongoDb Driver For Net

    由于mongodb开源github提供的net驱动都比较新,从2.3及以上版本都是netcore系列了,netframework至少都是4.6以上,且提供的dll并没有签名, 这就产生了一些问题 1 ...

  9. BAT批处理(四)

    网络命令 net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" / ...

  10. pycharm/webstorm创建react项目

    1.安装nodejs 2.安装reactapp依赖:npm install -g create-react-app 在pycharm/webstorm中选择react