Sightseeing

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 969    Accepted Submission(s): 409

Problem 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 A, B and L, separated by single spaces, with 1 ≤ A, B ≤ N, A ≠ 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 ≤ S, F ≤ 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 10^9 = 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

题意:有T组测试数据,单向边,求S到T的最短路径方案数和次短路径方案数

如果次短路径长度=最短路径长度+1,输出最短路径数+次短路路径数

否则输出最短路径数

题解:

用dijkstra求得方案数以及道路长度

在松弛的时候搞些事情:

用一个二维数组d[i][0/1]记录每一个节点距离起始点的最短距离和次短距离;

再开一个二维数组sum[i][0/1]记录路径数

更新状态时:

1)新值小于最短路径长:更新最短路径长,计数和次短路径长,计数

2)新值等于最短路径长:更新最短路径计数

3)新值大于最短路径长,小于次短路径长:更新次短路径长,计数

4)新值等于次短路径长:更新次短路径计数

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<cmath>
 7 #define R register
 8 #define ll long long
 9 #define inf 2047483600
10 #define mod 100003
11 #define DB double
12 using namespace std;
13 inline int read()
14 {
15     R int x=0,w=1;char ch=getchar();
16     while(!isdigit(ch)){if(ch==‘-‘) w=-1;ch=getchar();}
17     while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch-‘0‘),ch=getchar();
18     return x*w;
19 }
20 const int N=4001000;
21 struct node{
22     int u,v,c,ne;
23 }e[N];
24 int h[N],n,m,tot;
25 void add(R int u,R int v,R int c)
26 {
27     tot++;e[tot]=(node){u,v,c,h[u]};h[u]=tot;
28 }
29 int T,s,t;
30 int sum[N][2],d[N][2];
31 bool v[N][2];
32 struct kk{
33     int id,fg;
34     bool operator<(const kk&x)const{
35     return d[id][fg]>d[x.id][x.fg];
36     }
37 };
38 priority_queue<kk>q;
39 void dijkstra()
40 {
41     for(int i=1;i<=n;++i)
42     {
43         d[i][0]=d[i][1]=inf;
44         v[i][0]=v[i][1]=0;
45         sum[i][0]=sum[i][1]=0;
46     }
47     d[s][0]=0;sum[s][0]=1;
48     q.push((kk){s,0});
49     while(!q.empty())
50     {
51         int ff=q.top().id,fg=q.top().fg;q.pop();
52         if(v[ff][fg]) continue;
53         v[ff][fg]=1;
54         for(int i=h[ff];i;i=e[i].ne)
55         {
56             int rr=e[i].v,dis=d[ff][fg]+e[i].c;
57             if(dis<d[rr][0])
58             {
59                 if(d[rr][0]!=inf)
60                 {
61                     d[rr][1]=d[rr][0];sum[rr][1]=sum[rr][0];
62                     q.push((kk){rr,1});
63                 }
64                 d[rr][0]=dis;sum[rr][0]=sum[ff][fg];
65                 q.push((kk){rr,0});
66             }else if(dis==d[rr][0]) sum[rr][0]+=sum[ff][fg];
67             else if(dis<d[rr][1])
68                  {
69                      d[rr][1]=dis;sum[rr][1]=sum[ff][fg];
70                      q.push((kk){rr,1});
71                  }else if(dis==d[rr][1]) sum[rr][1]+=sum[ff][fg];
72         }
73     }
74     if(d[t][0]+1==d[t][1]) cout<<sum[t][0]+sum[t][1]<<endl;
75     else cout<<sum[t][0]<<endl;
76 }
77 int main()
78 {
79     T=read();
80     while(T--)
81     {
82         n=read();m=read();
83         tot=0;
84         for(int i=1;i<=n;++i) h[i]=0;
85         for(int i=1;i<=m;++i)
86         {
87             int x,y,z;x=read();y=read();z=read();
88             add(x,y,z);
89         }
90         s=read();t=read();
91         dijkstra();
92     }
93     return 0;
94 }

消杀的妄心尽而后真心现。

原文地址:https://www.cnblogs.com/adelalove/p/8464076.html

时间: 2024-11-11 00:16:38

Sightseeing的相关文章

poj1734 Sightseeing trip

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6919   Accepted: 2646   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ 1637 Sightseeing tour (混合图欧拉回路)

Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visit

1999 Central European Olympiad in Informatics - Sightseeing Trip

算法提示 最小环问题 题目大意 在一张带权无向图上,找出至少含 3 个点且权值和最小的环,并按环上的循序输出环上的点.存在重边,无自环. 做法分析 参考最小环问题,在更新 dist[i][j] 时,记录更新其的点 k,便于回溯路径. 参考代码 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 #include <algorithm> 6

HDU1688 Sightseeing(SPFA 求最短路与次短路的路径条数)可用作模板

Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 718    Accepted Submission(s): 293 Problem Description Tour operator Your Personal Holiday organises guided bus trips across the Bene

poj3621 Sightseeing Cows --- 01分数规划

典型的求最优比例环问题 参考资料: http://blog.csdn.net/hhaile/article/details/8883652 此题中,给出每个点和每条边的权值,求一个环使 ans=∑点权/∑边权 最大. 因为题目要求一个环,而且必然是首尾相接的一个我们理解的纯粹的环,不可能是其他样子的环, 所以我们可以把一条边和指向的点看做整体处理. 上面方程可以化为:ans×e[i]-p[i]=0 以它为边权二分答案,spfa求负环,有负环则该ans可行,增大下界. 若一直不可行,则无解. #i

POJ 1734:Sightseeing trip

Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6831 Accepted: 2612 Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions

URAL 1004 Sightseeing Trip(最小环)

Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this a

poj 1734 Sightseeing trip判断最短长度的环

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ 1734 Sightseeing trip (Floyd 最小环+记录路径)

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5040   Accepted: 1932   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ1734 Sightseeing trip【Floyd】【最小环】

Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5038 Accepted: 1930 Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions