HDU2833-WuKong(求不同起点,终点最短路的交点最多数量)
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.
题解:题意题目已给,就是悟空和唐僧分别从s1,s2出发,然后到达t1,t2.求在保证两个人走的都是最短路的前提下,两个人共同经过的地点的数量的最大值;我们可以推出 : 如有几个公共地点,则这些点必定为连续的点;然后,处理一下各个点相互之间的最短距离,若满足dis[s1][i]+dis[i][j]+dis[j][t1]=dis[s1][t1],则i~j即为一段,然后利用DP不断更新,处理处其最大值即可;
AC代码为:
//找分别为s1 t1,s2 t2 最短路径最多相同的地点
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int N,M,U,V,W,A,B,C,D;
int dis[310][310],dp[310][310];
void Folyd()
{
for(int k=1;k<=N;k++)
{
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
if(i==j||j==k||i==k) continue;
if(dis[i][j]>dis[i][k]+dis[k][j])
{
dis[i][j]=dis[i][k]+dis[k][j];
dp[i][j]=dp[i][k]+dp[k][j]-1;
}
else if(dis[i][j]==dis[i][k]+dis[k][j])
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]-1);
}
}
}
}
int work(int s1,int t1,int s2,int t2)
{
int ans=0;
if(dis[s1][t1]>=INF||dis[s2][t2]>=INF) return 0;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
if(dis[s1][i]+dis[i][j]+dis[j][t1]==dis[s1][t1]&&dis[s2][i]+dis[i][j]+dis[j][t2]==dis[s2][t2])
{
ans=max(ans,dp[i][j]);
}
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>N>>M)
{
if(!N && !M) break;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=N;j++)
{
dis[i][j]=INF;
dp[i][j]=2;
}
dis[i][i]=0;
dp[i][i]=1;
}
for(int i=1;i<=M;i++)
{
cin>>U>>V>>W;
dis[U][V]=dis[V][U]=min(W,dis[U][V]);
}
cin>>A>>B>>C>>D;
Folyd();
cout<<work(A,B,C,D)<<endl;
}
return 0;
}
- poj 3328(spfa)多终点多起点的最短路
- Silver Cow Party SPFA+SLF优化 单源起点终点最短路
- “同起点,同终点,打车价格不同”秒懂
- 多起点和终点求最短路
- POJ - 3268 Silver Cow Party解题报告(dijkstra分别求单源起点和单源终点的最短路)
- POJ 3615 floyd 求任意起点终点的最短路
- I - 浪里个浪 FZU - 2261——最短路_spfa算法(多起点-多终点)
- 单源起点最短路+单源终点最短路——pku3268
- POJ2263最短路变形//起点到终点最大载货量
- POJ 3615 floyd 求任意起点终点的最短路
- poj 3328(多起点多终点的最短路)
- poj-3268 Silver Cow Party (最短路-spfa-求到同一起点,同一终点的最短路)
- CF 586B 起点到终点的最短路和次短路之和
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】
- 最短路--bellman-ford--以x为起点和终点的最短路
- poj 3328(多起点多终点的最短路)
- HDU 2833 WuKong 求两条最短路间最多公共点数
- HDU2066 最短路spfa 多起点 多终点 基础题
- HDU 2066 一个人的旅行【最短路,多起点多终点,Dijkstra算法+spfa算法】
- rcgis api for flex求线段的起点,终点和中点