关闭

hdu 2833 WuKong(floyd变形)

标签: ACMHDU
247人阅读 评论(0) 收藏 举报
分类:

WuKong

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


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.
题意:给出悟空的起点和终点和唐僧的起点和终点,两个人都必须走最短路(可能存在多条),现在问你,两个人最多相遇多长的距离。

思路:用floyd可以求出最短路径上满足两个点之间某个性质的解,这里我们可以求出两点之间最短路径上的最多条数。

而且我们可以知道,因为两个人都必须走最短路,所以他们两个人有且只有一次相遇的机会。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 500
#define INF 9999999
int dp[N][N],d[N][N];
int n,m;
void init()
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            d[i][j]= i==j?0:INF;
    for(int i=1; i<=n; i++) dp[i][i]=1;
}
void floyd()
{
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(d[i][j]>d[i][k]+d[k][j])
                {
                    dp[i][j]=dp[i][k]+dp[k][j]-1;
                    d[i][j]=d[i][k]+d[k][j];
                }
                else if(d[i][j]==d[i][k]+d[k][j])
                    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]-1);
            }
}
int solve(int ws,int we,int ts,int te)
{
    int ans=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(d[ws][i]+d[i][j]+d[j][we]==d[ws][we]&&d[ts][i]+d[i][j]+d[j][te]==d[ts][te])
                ans=max(ans,dp[i][j]);
    return ans;
}
int main()
{
    int u,v,w;
    int ws,we,ts,te;
    while(~scanf("%d %d",&n,&m)&&n)
    {
        init();
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&u,&v,&w);
            if(u==v) continue;
            if(w<d[u][v]) d[u][v]=d[v][u]=w;
            dp[u][v]=dp[v][u]=2;
        }
        scanf("%d %d %d %d",&ws,&we,&ts,&te);
        floyd();
        printf("%d\n",solve(ws,we,ts,te));
    }
    return 0;
}



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Arbitrage(最短路-floyd算法变形求正权)

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16127   Acc...
  • u013486414
  • u013486414
  • 2014-10-24 00:19
  • 555

HDU:2112 HDU Today(floyd求最短路径+变形题)

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • zugofn
  • zugofn
  • 2016-08-04 21:50
  • 517

hdu-1181-变形课

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submi...
  • sxc1414749109
  • sxc1414749109
  • 2015-08-13 20:14
  • 552

HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,floyd水

题目: 1874 http://acm.hdu.edu.cn/showproblem.php?pid=1874 2444 http://acm.hdu.edu.cn/showproblem.php...
  • hcbbt
  • hcbbt
  • 2013-06-16 20:25
  • 2285

HDU 1181 变形课

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Sub...
  • A_ice_
  • A_ice_
  • 2016-07-28 17:53
  • 458

hdu 2833 WuKong(最短路径+记忆化搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=2833 大致题意:给定一个无向图,以及悟空和师傅起点与终点,求它们分别从起点到终点的最短路径中经过相同的...
  • u013081425
  • u013081425
  • 2014-05-18 21:35
  • 858

HDU1181:变形课(DFS)

Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一...
  • libin56842
  • libin56842
  • 2013-05-31 19:45
  • 2884

hdu1317 负权最短路(Floyd+bellman)

首先要读懂题,做这个题时读题看样例琢磨了半天才明白= =; 题目的意思是,一个人从一号房间走到最后一个房间,每个房间有相应的能量值(可正可负),当走到这个房间的时候可以获取这个房间的能量值,如果走到...
  • zs120197
  • zs120197
  • 2016-07-24 12:45
  • 259

floyd 变形或Dijkstra 变形

Frogger Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatus D...
  • qq_33132383
  • qq_33132383
  • 2016-11-23 20:10
  • 141

hdu4568(旅行商变形)

链接:点击打开链接 题意:有一个n*m的棋盘,每一个格子有一个值,代表经过这个格子的花费,给出k个宝藏点的坐标,求从棋盘的任意一个边进入棋盘,经过所有的宝藏点后在走出棋盘所需要的最小花费 代码: #i...
  • stay_accept
  • stay_accept
  • 2016-03-18 08:34
  • 401
    个人资料
    • 访问:131621次
    • 积分:5119
    • 等级:
    • 排名:第6309名
    • 原创:396篇
    • 转载:11篇
    • 译文:0篇
    • 评论:21条
    最新评论