zoukankan      html  css  js  c++  java
  • HDU2833 最短路 floyd

    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=10000007;
    int mp[305][305],num[305][305],n,m;
    int a,b,c,s1,s2,e1,e2;
    int main()
    {
        while(scanf("%d%d",&n,&m)&&(n+m)){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++){
                    if(i==j){
                        mp[i][j]==0;num[i][j]=1;
                    }
                    else {mp[i][j]=MAX;num[i][j]=2;}
                }
            for(int i=0;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=1;k<=n;k++){
                for(int i=1;i<=n;i++){
                    for(int j=1;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]-1;
                        }
                        else if(mp[i][j]==mp[i][k]+mp[k][j])
                            num[i][j]=num[i][k]+num[k][j]-1;
                    }
                }
            }
            int tmp=0;
            for(int i=1;i<=n;i++){
                for(int j=1;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
    ",tmp);
        }
        return 0;
    }
  • 相关阅读:
    如何处理CrashLoopBackOff状态的pod
    如何全方位的保障系统的稳定性
    Linux系统-网络带宽占用分析
    Docker之路-通过swarm管理配置及服务升级回滚
    Effective Java
    springboot websocket 简单入门
    JDK8 API文档
    Java反编译工具 Java Decompiler
    如何在高并发分布式系统中生成全局唯一ID
    Linux命令发送Http GET/POST请求
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6285585.html
Copyright © 2011-2022 走看看