HDU 2833 WuKong

WuKong

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

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.
 
题意:求两对源点的最短路中有多少公共的点,用数组dp记录
 
 1 #include <iostream>
 2 using namespace std;
 3 const int N = 310;
 4 const int inf = 0x3fffffff;
 5 int m,n;
 6 int mapp[N][N],dp[N][N];
 7 void floyd(){
 8     for(int k=1;k<=n;k++){
 9         for(int i=1;i<=n;i++){
10             for(int j=1;j<=n;j++){
11                 if(mapp[i][j]>mapp[i][k]+mapp[k][j]){
12                     mapp[i][j]=mapp[i][k]+mapp[k][j];
13                     dp[i][j]=dp[i][k]+dp[k][j]-1;
14                 }
15             }
16         }
17     }
18 }
19 void init(){
20     for(int i=1;i<=n;i++){
21         for(int j=i+1;j<=n;j++){
22             mapp[i][j]=inf;
23             mapp[j][i]=inf;
24             dp[i][j]=0;
25             dp[j][i]=0;
26         }
27         mapp[i][i]=0;
28         dp[i][i]=1;
29     }
30 }
31 int main(){
32     int a,b,c,s1,e1,s2,e2;
33     cin.sync_with_stdio(false);
34     while(cin>>n>>m&&(n!=0||m!=0)){
35         init();
36         for(int i=0;i<m;i++){
37             cin>>a>>b>>c;
38             if(mapp[a][b]>c){
39                 mapp[a][b]=c;
40                 mapp[b][a]=c;
41             }
42             dp[a][b]=2;
43             dp[b][a]=2;
44         }
45         floyd();
46         int ans=0;
47         cin>>s1>>e1>>s2>>e2;
48         for(int i=1;i<=n;i++){
49             for(int j=1;j<=n;j++){
50                 if(mapp[s1][i]+mapp[i][j]+mapp[j][e1]==mapp[s1][e1]&&mapp[s2][i]+mapp[i][j]+mapp[j][e2]==mapp[s2][e2]){
51                     ans=max(ans,dp[i][j]);
52                 }
53             }
54         }
55         cout<<ans<<endl;
56     }
57     return 0;
58 }

 

2017-01-25 12:16:50
posted @ 2017-01-25 12:19  ガ落涙『不變』  阅读(98)  评论(0编辑  收藏  举报