D - WuKong(Floyd+dp)

题目描述:

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.

题意:

悟空和唐憎各自要走一段路,给出各自的起点和终点A和B,C和D,问他们各自的起点到终点的最短路径上最多能有多少个公共点。

分析:

一开始我做这道题的时候没有一点思绪。看了一些大佬的博客之后发现是一个简单的dp。
我们用一个num[i][j]数组来保存i到j需要经过的结点数。
那么在使用Floyd的时候,我们如果碰到了距离相同的情况就需要取结点数较多的那个。
在Floyd执行完之后,我们需要用到一个嵌套循环来找到共同的结点数。
枚举每两点i和j,若G[A][B] == G[A][i] + G[i][j] + G[j][B]且G[C][D] == G[C][i] + G[i][j] + G[j][D],则说明边(i,j)均在悟空和唐憎各自的最短路径上,那么根据num[i][j],就能更新答案的值。
AC代码:

#include"stdio.h"
#include"string.h"
#include"algorithm"
    using namespace std;
#define INF 1000000
void Floyd(int Graph[][301],int N,int num[][301])
{
    int i,j,k;
    for(i=1;i<=N;i++)
        for(j=1;j<=N;j++)
           for(k=1;k<=N;k++)
           {
               if(Graph[j][k]>Graph[j][i]+Graph[i][k])
               {
                   Graph[j][k]=Graph[j][i]+Graph[i][k];
                   num[j][k]=num[j][i]+num[i][k];//不断更新
               }
               else
               if(Graph[j][k]==Graph[j][i]+Graph[i][k])//如果距离值相等,那么取大的结点值
                num[j][k]=max(num[j][k],num[j][i]+num[i][k]);
           }
}
int main()
{
    int i,j,k;
    int Graph[301][301];
    int num[301][301];
    int N,M;
    while(~scanf("%d%d",&N,&M))
    {
        if(N==0&&M==0)
            break;
        for(int i=0;i<=N;i++)
            for(int j=0;j<=N;j++)
                if(i==j)
                   {Graph[i][j]=0;num[i][j]=0;}
                else
                   {Graph[i][j]=INF;num[i][j]=0;}
        for(int i=0;i<M;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(Graph[a][b]>c)
            {
                Graph[a][b]=c;
                Graph[b][a]=c;
                num[a][b]=1;//a和b之间有边,则num值为1
                num[b][a]=1;
            }
        }
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        Floyd(Graph,N,num);
        int mark=-1;
        for(i=1;i<=N;i++)
            {for(j=1;j<=N;j++)
                if(Graph[a][b]==Graph[a][i]+Graph[i][j]+Graph[j][b])
                   if(Graph[c][d]==Graph[c][i]+Graph[i][j]+Graph[j][d])
                      {//需要全部遍历一遍才能得到正确答案
                         mark=max(mark,num[i][j]);
                      }
            }
        printf("%d\n",mark+1);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43506138/article/details/87616577