码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2833 WuKong(floyd + dp)经典

时间:2015-04-18 20:40:11      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:图论   floyd   dp   

WuKong

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


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
 


看了他人的解题报告,下面是来自:http://blog.csdn.net/azheng51714/article/details/8465357

  1. 题意: 
  2.  给定一个无向图,和两对起点终点,求两条最短路上的最多公共交点数。 
  3.  反证法容易验证相交公共点比连续!! 
  4.  那么我们假设存在2组数据 s1,e1,s2,e2; 
  5.  我们用dp[i][j] 代表 从点i到点j最短路上最多有多少个点! 
  6.  那么 map[s1][i]+map[i][j]+map[j][e1]=map[s1][e1] 就表示i到j的最短路为 s1到e1最短路的一条最优子路嘛; 
  7.  我们只需更新dp[i][j]中的最大值即可 
#include<stdio.h>
const int N = 405;
const int inf = 1e9;

int mapt[N][N],dp[N][N],n;

void init()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            mapt[i][j]=inf,dp[i][j]=2;
        mapt[i][i]=0; dp[i][i]=1;
    }
}
void floyd()
{
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    if(k!=i)
    {
        for(int j=1;j<=n;j++)
        if(i!=j&&j!=k)
        {
            if(mapt[i][j]>mapt[i][k]+mapt[k][j])
            {
                mapt[i][j]=mapt[i][k]+mapt[k][j];
                dp[i][j]=dp[i][k]+dp[k][j]-1;
            }
            else if(mapt[i][j]==mapt[i][k]+mapt[k][j]&&dp[i][j]<dp[i][k]+dp[k][j]-1)
                dp[i][j]=dp[i][k]+dp[k][j]-1;
        }
    }
}
int findAns(int s1,int e1,int s2,int e2)//找一共同的路径,公共点最多
{
    int ans=0;
    if(mapt[s1][e1]==inf||mapt[s2][e2]==inf)
        return ans;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    if(mapt[s1][i]+mapt[i][j]+mapt[j][e1]==mapt[s1][e1])
    if(mapt[s2][i]+mapt[i][j]+mapt[j][e2]==mapt[s2][e2])
    if(ans<dp[i][j])
     ans=dp[i][j];
    return ans;
}
int main()
{
    int m,a,b,c;
    while(scanf("%d%d",&n,&m)>0&&n+m!=0)
    {
        init();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(mapt[a][b]>c)
                mapt[a][b]=mapt[b][a]=c;
        }
        int s1,e1,s2,e2;
        scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
        floyd();
        printf("%d\n",findAns(s1,e1,s2,e2));
    }
}


 

HDU2833 WuKong(floyd + dp)经典

标签:图论   floyd   dp   

原文地址:http://blog.csdn.net/u010372095/article/details/45115765

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!