WuKong

WuKong

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 213 Accepted Submission(s): 91
 
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
 
Recommend
gaojie
 
/*
题意:给你一个图,然后给出悟空的起始位置,末位置,唐僧的初位置,末位置,然后让你计算一下,在重复最多城市
的最短路的重复城市的数量。 初步思路:dp,dp[i][j]表示从i到j重复的城市数量 */
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int mapn[][];
int dp[][];
int n,m;
int Ts,Te,Ws,We;
int u,v,val;
void floyd(){
for(int u=;u<=n;u++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(mapn[i][j]>mapn[i][u]+mapn[u][j]){
mapn[i][j]=mapn[i][u]+mapn[u][j];
dp[i][j]=dp[i][u]+dp[u][j];
}else if(mapn[i][j]==mapn[i][u]+mapn[u][j]){
dp[i][j]=max(dp[i][u]+dp[u][j],dp[i][j]);
}
}
}
}
}
void init(){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i==j) mapn[i][j]=;
else mapn[i][j]=INF;
dp[i][j]=;
}
}
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
init();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&val);
//cout<<u<<" "<<v<<" "<<val<<endl;
if(mapn[u][v]>val){//重边
mapn[u][v]=mapn[v][u]=val;
dp[u][v]=dp[v][u]=;//初始化相同的城市
}
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// cout<<mapn[i][j]<<" ";
// }
// cout<<endl;
// }
scanf("%d%d%d%d",&Ts,&Te,&Ws,&We);
floyd();
int res=-;
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// cout<<mapn[i][j]<<" ";
// }
// cout<<endl;
// }
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(dp[i][j]>res&&( mapn[Ts][Te]==mapn[Ts][i]+mapn[i][j]+mapn[j][Te] )&&(
mapn[Ws][We]==mapn[Ws][i]+mapn[i][j]+mapn[j][We]) ){
res=dp[i][j];
// cout<<res<<endl;
}
}
}
printf("%d\n",res+);
}
return ;
}
上一篇:深入理解JVM(三)垃圾收集器和内存分配策略


下一篇:CSharp设计模式读书笔记(8):桥接模式(学习难度:★★★☆☆,使用频率:★★★☆☆)