HDU2833 WuKong(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

2009 Multi-University Training Contest 2 - Host by TJU

看了他人的解题报告,下面是来自: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));
    }
}
时间: 2024-10-10 10:03:11

HDU2833 WuKong(floyd + dp)经典的相关文章

HDU 2833 WuKong(floyd最短路)

题目地址:HDU 2833 这题想到了最后是通过dis[s][t]==dis[s][i]+dis[i][j]+dis[j][t]的思路来判定是否属于最短路的一条..但是没想到可以用floyd来找最短路中的点数...最短路还是太渣了..好多性质都不会利用.. 这题的思路就是通过floyd对每两个点之间的最短路条数进行计数,然后通过上面的公式(对两条路线均要判定,都符合才说明都可以走),再找最短路中的最大点数. 代码如下: #include <iostream> #include <stdi

hdu2833 WuKong

给定两个起点终点,求两条最短路径上的最多交集点数. 求了最短路之后,枚举两条路上每条必然属于最短路径上的路径,(d[u]+w==d[v],则该条路径必然在最短路径上) dp[a][b]表示以a b为终点的最多交集点数. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm&

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

DP经典题型:石子合并问题

本周集训专题为DP系列,一个经典的系列便是石子归并问题. (1)有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动相邻的2堆石子合并,合并花费为新合成的一堆石子的数量.求将这N堆石子合并成一堆的总花费最小(或最大). 这是石子归并的简化版本,石子处于一排.由于发现只能是相邻的2堆石子进行归并.我们会发现,贪心算法在此处便失去作用,局部最优解并不能带来整体最优解. 因此,不难让我们想到,此题应该采取DP(dynamic Programing)来求其最优解. 动态规划常常采取从部分整体最

树形DP经典题

题目传送门 题意: 给出一棵树,求离每个节点最远的点的距离 思路: 把无根树转化成有根树分析, 对于上面那棵树,要求距结点2的最长距离,那么,就需要知道以2为顶点的子树(蓝色圈起的部分,我们叫它Tree(2)),距顶点2的最远距离L1 还有知道2的父节点1为根节点的树Tree(1)-Tree(2)部分(即红色圈起部分),距离结点1的最长距离+dist(1,2) = L2,那么最终距离结点2最远的距离就是max{L1,L2} f[i][0],表示顶点为i的子树的,距顶点i的最长距离 f[i][1]

hdu1227 dp 经典

1 //Accepted 420 KB 15 ms 2 //刚开始的时候一直在想把depot放在哪个restaurant,结果一直陷在里面,不知道该怎么转移 3 //现在我们把i到j看成一段处理,就认为i到j由一个depot供应,而与其他depot没有关系,且 4 //这个depot与其他的restaurant也没有关系,那么对于i到j这一段产生的花费就可以计算出来 5 //由cost[i][j]表示,对于i到j这一段,把depot放到(i+j)/2这个restaurant上,产生的花费最少 6

UVA10269 Adventure of Super Mario(Floyd+DP)

UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs to find a way home -- with the princess of course :-) He's very familiar with the 'Super Mario World', so he doesn't need a map, he only needs the be

LCS 最长公共子序列(DP经典问题)

最长公共子序列问题以及背包问题都是DP(动态规划)算法的经典题目,值得深度挖掘以致了解DP算法思想.问题如下: 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最

HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的平均值最小. 解题思路:斜率DP经典题, 详细分析见: NOI2004年周源的论文<浅谈数形结合思想在信息学竞赛中的应用> 还有要注意要用输入输出外挂,不是getchar()版的,是fread()版的,第一次遇到这么变态的题目- -|||. 代码: 1 #include<iostream&g