# POJ 3463 最短路和次短路的和 dijkstra（）

题解：

（1）如果val小于dist[u][0]，则dist[u][1]=dist[u][0],count[u][1]=count[u][0],dist[u][0]=val.count[u][0]=count[v][flag]。否则转（2）

（2）如果val 等于dis[u][0],则count[u][0] += count[v][flag]; 否则转（3）

（3）如果val小于dis[u][1],则dis[u][1]=val.count[u][1]=count[v][flag]。

（4）如果val等于dis[u][1]，则count[u][1] +=count[v][flag].否则什么都不做。

Sightseeing
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5273 Accepted: 1815

Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

• One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.

• M lines, each with three integers AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.

The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.

• One line with two integers S and F, separated by a single space, with 1 ≤ SF ≤ N and S ≠ F: the starting city and the final city of the route.

There will be at least one route from S to F.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109 = 1,000,000,000.

Sample Input

2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1

Sample Output

3
2
ac代码：

#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int N=1010;
const int M=10010;
#define MAX 0x7fffffff
int numcity,numpath,sp,ep,num=0;
struct edge{
int rp,value,next;
}ee[M];
ee[num].rp=y;
ee[num].value=v;
}
void dijkstra(){
for(int i=0;i<=numcity;++i){
for(int j=0;j<2;++j)
dis[i][j]=MAX;
}
dis[sp][0]=0;
sum[sp][0]=1;
//printf("sss\n");
memset(visted,0,sizeof(visted));
for(int i=1;i<=2*numcity;++i){
//printf("i====%d\n",i);
int temp=MAX,flag=0,mark=0;
for(int j=1;j<=numcity;++j){
//  printf("j==%d\n",j);
if(!visted[j][0]&&dis[j][0]<temp){
temp=dis[j][0];
mark=j;
flag=0;
}
else if(!visted[j][1]&&dis[j][1]<temp){
temp=dis[j][1];
mark=j;
flag=1;
}
}
visted[mark][flag]=1;
//printf("aaa\n");
int y=ee[k].rp;
int vv=ee[k].value;
if(temp+vv<dis[y][0]){
dis[y][1]=dis[y][0];
sum[y][1]=sum[y][0];
dis[y][0]=temp+vv;
sum[y][0]=sum[mark][flag];
}
else if(temp+vv==dis[y][0]){
sum[y][0]+=sum[mark][flag];
}
else if(temp+vv<dis[y][1]){
dis[y][1]=temp+vv;
sum[y][1]=sum[mark][flag];
}
else if(temp+vv==dis[y][1]){
sum[y][1]+=sum[mark][flag];
}
//  printf("sum[0]==%d  sum[1]==%d\n",sum[y][0],sum[y][1]);
}
//printf("sssss\n");
}
}
int main(){
//freopen("in.txt","r",stdin);
int numcase;
scanf("%d",&numcase);
while(numcase--){
scanf("%d%d",&numcity,&numpath);
num=0;
memset(sum,0,sizeof(sum));
int x,y,v;
while(numpath--){
scanf("%d%d%d",&x,&y,&v);
}
scanf("%d%d",&sp,&ep);
dijkstra();
//printf("dis[ep][1]==%d  dis[ep][0]==%d\n",dis[ep][1],dis[ep][0]);
//printf("sum[1]==%d  sum[0]==%d\n",sum[ep][1],sum[ep][0]);
if(dis[ep][1]-1==dis[ep][0])
printf("%d\n",sum[ep][1]+sum[ep][0]);
else
printf("%d\n",sum[ep][0]);
}
return 0;
}

• 本文已收录于以下专栏：

## Dijkstra求最短路与次短路

• qq_25971709
• 2015年04月28日 12:52
• 680

## poj3255 次短路Dijkstra

• NGccc
• 2015年04月15日 16:54
• 572

## poj 3463 最短路和次短路

• Eirlys_North
• 2017年02月28日 19:53
• 137

## Sightseeing_poj3463_dijkstra

DescriptionTour operator Your Personal Holiday organises guided bus trips across the Benelux. Every ...
• jpwang8
• 2016年09月16日 12:31
• 207

## POJ 3463 Sightseeing【次短路，Dijkstra算法，链式前向星建图】

• hurmishine
• 2016年08月26日 10:53
• 779

## POJ 3463 最短路 次短路

• sdj222555
• 2012年06月25日 18:14
• 3085

## 3463 Sightseeing dijkstra求最短路和次短路

SightseeingTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 2864 Accepted: 949DescriptionTo...
• kongming_acm
• 2010年07月17日 15:04
• 801

## poj 3463 dijkstra变形（求最短路和次短路的数量）

• dumeichen
• 2014年10月07日 20:21
• 369

## poj 3463 Sightseeing（最短路&&次短路）

Language: Default Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Su...
• u011699990
• 2015年05月14日 01:08
• 481

## Sightseeing POJ - 3463 求最短路和次短路

• yanga11ang
• 2017年10月06日 15:36
• 59

举报原因： 您举报文章：POJ 3463 最短路和次短路的和 dijkstra（） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)