1. 程式人生 > >hdu 2833 WuKong(最短路 + dp)

hdu 2833 WuKong(最短路 + dp)

WuKong

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


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.題意:悟空和唐憎各自要走一段路,給出各自的起點和終點A和B,C和D,問他們各自的起點到終點的最短路徑上最多能有多少個公共點。思路:設cnt[i][j]為從i走到j所經過的最多的點數(不包括i),在用floyd求出每兩點之間的最短路徑時,更新cnt[i][j]的值,跑完floyd後,列舉每兩點i和j,若G[A][B] == G[A][i] + G[i][j] + G[j][B]且G[C][D] == G[C][i] + G[i][j] + G[j][D],則說明邊(i,j)均在悟空和唐憎各自的最短路徑上,那麼根據cnt[i][j],就能更新答案的值。
AC程式碼:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <map>
#include <algorithm>
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const int INF = 1e9 + 7;
const int maxn = 305;

int n, m, s1, e1, s2, e2;
int G[maxn][maxn], cnt[maxn][maxn];
int main()
{
    int a, b, c;
    while(scanf("%d%d", &n, &m), n || m)
    {
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            G[i][j] = i == j ? 0 : INF;
        }
        while(m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            if(G[a][b] < c) continue;
            G[a][b] = G[b][a] = c;
            cnt[a][b] = cnt[b][a] = 1;
        }
        for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            if(G[i][k] + G[k][j] < G[i][j])
            {
                G[i][j] = G[i][k] + G[k][j];
                cnt[i][j] = cnt[i][k] + cnt[k][j];
            }
            else if(G[i][k] + G[k][j] == G[i][j] && cnt[i][j] < cnt[i][k] + cnt[k][j])
            cnt[i][j] = cnt[i][k] + cnt[k][j];
        }
        scanf("%d%d%d%d", &s1, &e1, &s2, &e2);
        int ans = -1;
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        if(ans < cnt[i][j] && G[s1][e1] == G[s1][i] + G[i][j] + G[j][e1] && G[s2][e2] == G[s2][i] + G[i][j] + G[j][e2])
        ans = cnt[i][j];
        printf("%d\n", ans + 1);
    }
    return 0;
}