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. 如何实现iframe页面与父级页面js交互

    处理办法:1.同一域下,相同端口2.父级.子集页面上同时标记 document.domain = "xxx.com" 操作内部元素:1.jQuery使用 iframe.conten ...

  2. [Clr via C#读书笔记]Cp13接口

    Cp13接口 类和接口继承 接口只提供签名,不提供实现:等效于契约:凡事能使用具名接口的地方都能够使用实现了的接口. 定义接口 定义很简单,FCL也提供了大量的现成接口供使用: 继承接口 类不能多继承 ...

  3. [Clr via C#读书笔记]Cp10属性

    Cp10属性 属性的本质就是方法,只是看起来像字段罢了: 无参属性 就是一般属性: 字段一般要private,然后通过设置访问方法-访问器来访问:属性是方法语法变种:getset不一定要访问支持字段: ...

  4. php 连接redis查询数据

    class Layoutdemo{ function index(){ $db = new Db(); $id=390; $layout_json = array(); if($info = $db- ...

  5. lintcode-143-排颜色 II

    143-排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 注意事项 You are not ...

  6. redis切换数据库的方法【jedis】

    package com.test; import redis.clients.jedis.Jedis; public class readredis { public static void main ...

  7. 结对作业二——WordCount进阶版

    软工作业三 要求地址 作业要求地址 结对码云项目地址 结对伙伴:秦玉 博客地址 PSP表格 PSP2.1 个人开发流程 预估耗费时间(分钟) 实际耗费时间(分钟) Planning 计划 10 7 · ...

  8. 某一线互联网公司前端面试题js部分总结

    js部分 1,使用严格模式的优点 - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全之处,保证代码运行的安全: - 提高编译器效率,增加运行速度 ...

  9. 生成以指定字符为开头的md5值(6位数字)

    以下脚本的功能是生成以指定字符为开头的md5值 #-*- coding:utf-8 -*- #脚本功能:生成以指定字符为开头的md5值(6位数字) import hashlib import rand ...

  10. 【.NET】- Task.Run 和 Task.Factory.StartNew 区别

    Task.Run 是在 dotnet framework 4.5 之后才可以使用, Task.Factory.StartNew 可以使用比 Task.Run 更多的参数,可以做到更多的定制. 可以认为 ...