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.

题解:题意题目已给,就是悟空和唐僧分别从s1,s2出发,然后到达t1,t2.求在保证两个人走的都是最短路的前提下,两个人共同经过的地点的数量的最大值;我们可以推出 : 如有几个公共地点,则这些点必定为连续的点;然后,处理一下各个点相互之间的最短距离,若满足dis[s1][i]+dis[i][j]+dis[j][t1]=dis[s1][t1],则i~j即为一段,然后利用DP不断更新,处理处其最大值即可;

AC代码为:

//找分别为s1 t1,s2 t2 最短路径最多相同的地点 

#include<bits/stdc++.h>

using namespace std;

const int INF=0x3f3f3f3f;

int N,M,U,V,W,A,B,C,D;

int dis[310][310],dp[310][310];

void Folyd()

{

    for(int k=1;k<=N;k++)

    {

        for(int i=1;i<=N;i++)

        {

            for(int j=1;j<=N;j++)

            {

                if(i==j||j==k||i==k) continue;

                if(dis[i][j]>dis[i][k]+dis[k][j])

                {

                    dis[i][j]=dis[i][k]+dis[k][j];

                    dp[i][j]=dp[i][k]+dp[k][j]-1;   

                }

                else if(dis[i][j]==dis[i][k]+dis[k][j])

                    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]-1);     

            }

        }

    }

}

int work(int s1,int t1,int s2,int t2)

{

    int ans=0;

    if(dis[s1][t1]>=INF||dis[s2][t2]>=INF) return 0;

    for(int i=1;i<=N;i++)

    {

        for(int j=1;j<=N;j++)

        {

            if(dis[s1][i]+dis[i][j]+dis[j][t1]==dis[s1][t1]&&dis[s2][i]+dis[i][j]+dis[j][t2]==dis[s2][t2])

            {

                ans=max(ans,dp[i][j]);

            }

        }

    }

    return ans;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    while(cin>>N>>M)

    {

        if(!N && !M) break;

        for(int i=1;i<=N;i++) 

        {

            for(int j=1;j<=N;j++) 

            {

                dis[i][j]=INF;

                dp[i][j]=2;

            }

            dis[i][i]=0;

            dp[i][i]=1;

        }

        for(int i=1;i<=M;i++)

        {

            cin>>U>>V>>W;

            dis[U][V]=dis[V][U]=min(W,dis[U][V]);

        }

        cin>>A>>B>>C>>D;

        Folyd();

        cout<<work(A,B,C,D)<<endl;

    } 

    return 0;   

}

HDU2833-WuKong(求不同起点,终点最短路的交点最多数量)的更多相关文章

  1. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  2. java实现根据起点终点和日期查询去哪儿网的火车车次和火车站点信息

    本文章为原创文章,转载请注明,欢迎评论和改正. 一,分析 之前所用的直接通过HTML中的元素值来爬取一些网页上的数据,但是一些比较敏感的数据,很多正规网站都是通过json数据存储,这些数据通过HTML ...

  3. 洛谷p2149----两个终点和两个起点,最短路最大交汇长度!!!

    说实话,这题真第一次见,学到了不少有趣的东西,因吹丝汀!! 思路:因为不可能同时并行和相遇(我也不知道为啥,等我会证明了就来说说) 所以正向建边再反向建边,拓扑排序+dp求最下长路,记录下最大的就是解 ...

  4. Bellman-Ford 求含负权最短路

    该算法详解请看   https://www.cnblogs.com/tanky_woo/archive/2011/01/17/1937728.html 单源最短路   当图中存在负权边时 迪杰斯特拉就 ...

  5. python脚本,计算起点终点高程

    import arcpy >>> import arcpy ... gd="D:/项目/shp/Pipe.gdb/ZK/GDPOINT" ... gx=" ...

  6. NX二次开发-创建圆弧(起点-终点-半径)UF_CURVE_create_arc_point_point_radius

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> UF_initialize(); //起点 ]; ArcStartPoint[ ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. 算法基础⑧搜索与图论--dijkstra(迪杰斯特拉)算法求单源汇最短路的最短路径

    单源最短路 所有边权都是正数 朴素Dijkstra算法(稠密图) #include<cstdio> #include<cstring> #include<iostream ...

  9. POJ1523 Tarjan求割点以及删除割点之后强连通分量的数量

    题目链接:http://poj.org/problem?id=1523 SPF:A Single Point of Failure也就是割点(一个点导致网络之间的不连通),由于给出的图是无向图,所以只 ...

随机推荐

  1. 利用爬虫爬取LOL官网上皮肤图片

    今天在浏览网页时,看到一篇很有意思的文章,关于网络爬虫的.该文章是讲述如何利用request爬取英雄联盟官网皮肤图片.看过文章后觉得挺有用的,把代码拿过来运行了一下,果真爬取成功.下面给大家分享一下代 ...

  2. 201871010114-李岩松《面向对象程序设计(java)》第一周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  3. 从EFCore上下文的使用到深入剖析DI的生命周期最后实现自动属性注入

    故事背景 最近在把自己的一个老项目从Framework迁移到.Net Core 3.0,数据访问这块选择的是EFCore+Mysql.使用EF的话不可避免要和DbContext打交道,在Core中的常 ...

  4. Java编程思想——第14章 类型信息(二)反射

    六.反射:运行时的类信息 我们已经知道了,在编译时,编译器必须知道所有要通过RTTI来处理的类.而反射提供了一种机制——用来检查可用的方法,并返回方法名.区别就在于RTTI是处理已知类的,而反射用于处 ...

  5. SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)

    上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...

  6. secureCRT安装与激活

    SecureCRT安装及激活方式 百度网盘地址: SecureCRT及激活软件的地址: 1. 安装secureCRT 百度网盘下载,点击scrt814-x64.exe,按照提示安装secureCRT, ...

  7. UCACO刷题

    UCACO刷题 SUBMIT: /* ID: your_id_here LANG: C++ TASK: test */ 文件:freopen(“file.in", "r" ...

  8. vscode加入到鼠标右键

    新建.reg的文件,复制下面代码,然后运行 D:\\软件\\VsCode\\Microsoft VS Code\\Code.exe路径改为自己的,必须是两个 \\ 才能生效 Windows Regis ...

  9. 软件测试从业者必备的高频Linux命令

    命令 cd 1.如何进入上级目录 cd .. 2.如何进入当前用户主目录 cd ~ 3.如何进入上两级目录 cd ../.. 4.进入当前目录命令 cd . 5.如何进入目录 /usr/isTeste ...

  10. python--debug神器pysnooper

    使用它你可以获得什么优势: (1)无需用print就可以获得变量的值: (2)以日志地形式进行保存,方便随时查看: (3)可以根据需要,设置调用函数的深度: (4)多个函数的日志,可以设置前缀进行标识 ...