POJ 3463 Sightseeing

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

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 cityA 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

Hint

The first test case above corresponds to the picture in the problem description.

Source

求最短路和次短路。

dijkstra求最短路的方法改一下,同时找最短和次短,如果找到更短的,就记成最短路,而原来的最短路变成次短路

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<vector>
 7 #include<queue>
 8 using namespace std;
 9 const int INF=0xffffff;
10 const int mxn=12000;
11 struct edge{
12     int u,v;
13     int w;
14     int next;
15 }e[mxn*3];
16 int hd[mxn],cnt;
17 void add_edge(int u,int v,int dis){
18     e[++cnt].next=hd[u];e[cnt].v=v;e[cnt].w=dis;hd[u]=cnt;return;
19 }
20 //
21 int n,m;
22 int rnum[mxn][2];//最短路 次短路 
23 int vis[mxn][2];
24 int dis[mxn][2];//最短路 次短路 
25 void dij(int s,int t){
26     memset(vis,0,sizeof vis);
27     memset(rnum,0,sizeof rnum);
28     int i;
29     for(i=1;i<=n;i++) dis[i][0]=dis[i][1]=INF;
30     dis[s][0]=0;    rnum[s][0]=1;//初始化
31     int j,k;
32     int tmp,flag;
33     int ro=2*n-1;//最短路和次短路都要求,所以循环这么多遍 
34     for(i=1;i<=ro;i++){
35         tmp=INF;
36         for(j=1;j<=n;j++){
37             if(!vis[j][0] && tmp>dis[j][0]){
38                 k=j;
39                 flag=0;//找到最短路 
40                 tmp=dis[j][0];
41             }else if(!vis[j][1] && tmp>dis[j][1]){
42                 k=j;
43                 flag=1;//找到次短路
44                 tmp=dis[j][1]; 
45             }
46         }
47         if(tmp==INF)break;
48         vis[k][flag]=1;
49         for(j=hd[k];j;j=e[j].next){//以k中转更新边 
50             int v=e[j].v;
51             if(dis[v][0]>tmp+e[j].w){
52                 dis[v][1]=dis[v][0];//
53                 rnum[v][1]=rnum[v][0];//原先的最短路变成次短路 
54                 dis[v][0]=tmp+e[j].w;
55                 rnum[v][0]=rnum[k][flag];
56             }
57             else if(dis[v][0]==tmp+e[j].w){
58                 rnum[v][0]+=rnum[k][flag];
59             }
60             else if(dis[v][1]>tmp+e[j].w){
61                 rnum[v][1]=rnum[k][flag];
62                 dis[v][1]=tmp+e[j].w;
63             }else if(dis[v][1]==tmp+e[j].w){
64                 rnum[v][1]+=rnum[k][flag];
65             }
66         }
67     }
68     if(dis[t][1]==dis[t][0]+1)rnum[t][0]+=rnum[t][1];
69     printf("%d
",rnum[t][0]);
70     return;
71 }
72 int main(){
73     int T;
74     scanf("%d",&T);
75     while(T--){
76         cnt=0;
77         memset(hd,0,sizeof hd);
78         scanf("%d%d",&n,&m);
79         int u,v,w;
80         int i,j;
81         for(i=1;i<=m;i++){
82             scanf("%d%d%d",&u,&v,&w);
83             add_edge(u,v,w);
84         }
85         int s,t;
86         scanf("%d%d",&s,&t);
87         dij(s,t);
88     }
89     return 0;
90 }
原文地址:https://www.cnblogs.com/SilverNebula/p/5668584.html