首页 > ACM题库 > HDU-杭电 > HDU 2833-WuKong-动态规划-[解题报告]HOJ
2014
02-17

HDU 2833-WuKong-动态规划-[解题报告]HOJ

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

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.

/*
题意:
 给定一个无向图,和两对起点终点,求两条最短路上的最多公共交点数。
 反证法容易验证相交公共点比连续!!
 那么我们假设存在2组数据 s1,e1,s2,e2;
 我们用dp[i][j] 代表 从点i到点j最短路上最多有多少个点!
 那么 map[s1][i]+map[i][j]+map[j][e1]=map[s1][e1] 不就表示i到j的最短路为 s1到e1最短路的子路嘛;
 我们只需更新dp[i][j]中的最大值即可
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=502;
const int INF=1<<30;
int dp[maxn][maxn],map[maxn][maxn],ans,n,m;
void floyd()
{
    int i,j,k;
    for(k=1;k<=n;k++)
    {
        for(i=1;i<=n;i++)
        {
            if(map[i][k]!=INF&&i!=k)
            for(j=1;j<=n;j++)
            {
                if(i==j||j==k)continue;
                if(map[i][j]>map[i][k]+map[k][j])
                {
                    map[i][j]=map[i][k]+map[k][j];
                    dp[i][j]=dp[i][k]+dp[k][j]-1;
                }
                else if(map[i][j]==map[i][k]+map[k][j]&&dp[i][j]<dp[i][k]+dp[k][j])
                {
                    dp[i][j]=dp[i][k]+dp[k][j]-1;
                }
            }
        }
    }
}
int solve(int s1,int e1,int s2,int e2)
{
    int i,j,res=0;
    if(map[s1][e1]>=INF||map[s2][e2]>=INF)return 0;
    for(i=1;i<=n;i++)
    {
        for(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])
            {
                res=max(res,dp[i][j]);
            }
        }
    }
    return res;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        int i,j,u,v,w,s1,e1,s2,e2;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                map[i][j]=INF;
                dp[i][j]=2;
            }
            dp[i][i]=1;
            map[i][i]=0;
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
             map[v][u]=map[u][v]=min(map[u][v],w);
        }
        floyd();
        scanf("%d%d%d%d",&s1,&e1,&s2,&e2);
        printf("%d\n",solve(s1,e1,s2,e2));
    }
    return 0;
}

解题参考:http://blog.csdn.net/azheng51714/article/details/8465357


  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  2. 有一点问题。。后面动态规划的程序中
    int dp[n+1][W+1];
    会报错 提示表达式必须含有常量值。该怎么修改呢。。

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。