zoukankan      html  css  js  c++  java
  • HDU2833-WuKong(求不同起点,终点最短路的交点最多数量)

    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;   

  • 相关阅读:
    老司机心得之时间管理"入坑"
    shell的字符串和数字的转化(数字自动做字符串处理,变量名做字符串输出用单引号)
    shell的date命令:使用方法,以及小时、分钟的计算
    一篇详细的linux中shell语言的字符串处理
    linux的string操作(字符串截取,长度计算)
    linux下数学运算器:expr命令(shell中完成数学运算)
    shell脚本格式的几点注意:格式严格,空格不能随便出现(一写就记不住)
    vim的颜色修改,高亮设置。
    pyhton exit
    python判断类型:想知道一个对象(实例或者变量)是什么类型,什么结构的
  • 原文地址:https://www.cnblogs.com/csushl/p/9386773.html
Copyright © 2011-2022 走看看