hdu 2833 WuKong【Floyd+dp】【好题】

WuKong

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1761    Accepted Submission(s): 638

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

2009 Multi-University Training Contest 2 - Host by TJU

 

 题目大意:给你n个点,m条边,问在最短路的情况下,给你两对点对,问这两条路径中重叠路径中最多有多少点。


思路:

1、首先,恭喜又一道好题让手贱看题解毁了。其次,思路来源:http://blog.csdn.net/azheng51714/article/details/8465357


2、对于dp【i】【j】表示i,j路径上有多少个点,初始化(i!=j)dp【i】【j】=2;if(i==j)dp【i】【j】=1;

其状态转移方程跟随Floyd过程:


if(map【j】【 k】>map【j】【i】+map【i】【k】)dp【j】【k】=dp【j】【i】+dp【i】【k】-1

if(map【j】【 k】==map【j】【i】+map【i】【k】)dp【j】【k】=max(dp【j】【i】+dp【i】【k】-1,dp【j】【k】)


3、在Floyd三层for之后,我们得到的dp【i】【j】=x就表示从i到j的路径上,最多经过x个点。


4、那么在寻找这部分最多重叠点的时候,我们就可以通过枚举路径来达到寻找最大值的功效。

辣么:


if(map【s1】【i】+map【i】【j】+map【j】【e1】==map【s1】【e1】&&map【s2】【i】+map【i】【j】+map【j】【e2】==map【s2】【e2】)那么从i到j就是两条路径的重叠路径之一。即dp【i】【j】为这条路径上的点个数。然后我们枚举i,j点对,维护最大值即可。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int map[305][305];
int dp[305][305];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map[i][j]=0x3f3f3f3f;
                dp[i][j]=2;
            }
            map[i][i]=0;
            dp[i][i]=1;
        }
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            map[x][y]=min(map[x][y],w);
            map[y][x]=map[x][y];
        }
        int s1,e1,s2,e2;
        scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=n;k++)
                {
                    if(map[j][k]>map[j][i]+map[i][k])
                    {
                        map[j][k]=map[j][i]+map[i][k];
                        dp[j][k]=dp[j][i]+dp[i][k]-1;
                    }
                    if(map[j][k]==map[j][i]+map[i][k])
                    {
                        if(dp[j][k]<dp[j][i]+dp[i][k]-1)
                        {
                            dp[j][k]=dp[j][i]+dp[i][k]-1;
                        }
                    }
                }
            }
        }
        int output=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(map[s1][i]+map[i][j]+map[j][e1]==map[s1][e1]&&map[s2][i]+map[i][j]+map[j][e2]==map[s2][e2])
                {
                    output=max(output,dp[i][j]);
                }
            }
        }
        printf("%d\n",output);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值