dijkstra ZQUOJ 21463&&POJ 3463 Sightseeing

馋奶兔 提交于 2020-03-05 04:33:02

题目:Sightseeing

来源:ZQUOJ  21463&&POJ  3463

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

 

题目大意:求源点S到终点F的最短路的数量和比最短路长1的数量。

解题思路:我们可以利用dijstra算法的思想,只需在其中进行一些改进即可。可以先定义一个二维的数组D[N][2]。D[i][0]代表源点S到点i的最短路,D[i][1]代表源点S到点i的次短路。初始化D[S][0]=0,其余的都初始化为无穷大。然后定义一个二维数组cnt[N][2]记录路径方法数,cnt[S][0]=1,其余初始为0。再定义一个标记数组vis[N][2],初始vis[S][0]被标记已访问,其余未访问。采用dijstra算法的思想,每次从dist[N][2]中选择一个未被标记且值最小的点D[v][flag](可能这个值是这个点的最短路,也可能是次短路,只有当此点的最短路被标记了次才可能选中此点的次短路)。再用这个值cost去更新此点的邻接点u。

更新状态时:
1)新值小于最短路径长:更新最短路径长,计数;次短路径长,计数
2)新值等于最短路径长:更新最短路径计数
3)新值大于最短路径长,小于次短路径长:更新次短路径长,计数
4)新值等于次短路径长:更新次短路径计数

联想到最小,次小的一种更新关系:
    if(x<最小)更新最小,次小
    else if(==最小)更新方法数
    else if(x<次小)更新次小
    else if(x==次小)更新方法数

值得注意的是,题目图有重边,所以不能用邻接矩阵存储。

这样循环计算2*n-1次就可以计算出源点到所有点的最短路和次短路的方法数了,而对于终点F,如果次短路比最短路大1则答案为最短路和次短路的方法数之和,否则就为最短路的方法数。

AC代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define INF 1000000000
 4 typedef struct
 5 {
 6     int v,cost,next;
 7 }Node;
 8 Node e[10000];
 9 int n,m,s,f;
10 int first[1001],D[1001][2],cnt[1001][2],vis[1001][2];
11 int dijkstra()
12 {
13     int i,j,k,min,v,flag,cost;
14     memset(vis,0,sizeof(vis));
15     for(i=1;i<=n;i++)
16         D[i][0]=D[i][1]=INF;
17     D[s][0]=0;
18     cnt[s][0]=1;
19     for(i=1;i<2*n;i++)
20     {
21         min=INF;
22         for(j=1;j<=n;j++)
23         {
24             if(!vis[j][0]&&D[j][0]<min)
25             {
26                 min=D[j][0];
27                 k=j;
28                 flag=0;
29             }
30             else if(!vis[j][1]&&D[j][1]<min)
31             {
32                  min=D[j][1];
33                  k=j;
34                 flag=1;
35             }
36         }
37         if(min==INF)
38             break;
39         vis[k][flag]=1;
40         for(j=first[k];j!=-1;j=e[j].next)
41         {
42             v=e[j].v;
43             cost=e[j].cost;
44             if(min+cost<D[v][0])
45             {
46                 D[v][1]=D[v][0];
47                 cnt[v][1]=cnt[v][0];
48                 D[v][0]=min+cost;
49                 cnt[v][0]=cnt[k][flag];
50             }
51             else if(min+cost==D[v][0])
52                 cnt[v][0]+=cnt[k][flag];
53             else if(min+cost<D[v][1])
54             {
55                 D[v][1]=min+cost;
56                 cnt[v][1]=cnt[k][flag];
57             }
58             else if(min+cost==D[v][1])
59                 cnt[v][1]+=cnt[k][flag];
60         }
61     }
62     if(D[f][0]+1==D[f][1])
63         return cnt[f][0]+cnt[f][1];
64     return cnt[f][0];
65 }
66 int main()
67 {
68     int t,g,i,u,v,w;
69     scanf("%d",&t);
70     while(t--)
71     {
72         scanf("%d%d",&n,&m);
73         g=0;
74         memset(first,-1,sizeof(first));
75         for(i=0;i<m;i++)
76         {
77             scanf("%d%d%d",&u,&v,&w);
78             e[g].v=v;
79             e[g].cost=w;
80             e[g].next=first[u];
81             first[u]=g;
82             g++;
83         }
84         scanf("%d%d",&s,&f);
85         printf("%d\n",dijkstra());
86     }
87     return 0;
88 }

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!