HDU 1688 Sightseeing
来源:互联网 发布:加工中心3d编程培训 编辑:程序博客网 时间:2024/06/04 19:27
Sightseeing
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1050 Accepted Submission(s): 461
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.
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.
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
题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数。
还是dijkstra算法,只不过要求两种路径和路径条数,而且多了几种状态,当找出距离s最短的点的时候,然后进行更新,更新分四种情况:
1.新路径比最短路径长度要小,那么最短路和次短路的长度和次数都要更新。
2.新路径等于最短路的长度,那么只需要更新最短路的条数。
3.新路径比最短路要长但是比次短路要短,那么需要更新次短路的长度和条数。
4.新路径等于次短路径的长度,那么只需要更新次短路径的条数。
1 #include <iostream> 2 #include <vector> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 #define inf 0X3f3f3f3f 7 #define N 1010 8 using namespace std; 9 int n,m; 10 struct node{ 11 int x,y,len;//x:下一个点,y':状态是最短还是次短,len:路径的长度 12 node(int a,int b,int c):x(a),y(b),len(c){}; 13 }; 14 struct edge{ 15 int x,len; 16 edge(int a,int b):x(a),len(b){}; 17 }; 18 bool operator < (const node a,const node b){ 19 return a.len>b.len; 20 } 21 int len[N][2],cnt[N][2]; 22 bool vis[N][2]; 23 vector<edge> mapp[N]; 24 void dij(int x,int y){ 25 for(int i=0;i<=n;i++){ 26 len[i][0]=len[i][1]=inf; 27 } 28 memset(vis,false,sizeof(vis)); 29 memset(cnt,0,sizeof(cnt)); 30 priority_queue<node> q; 31 len[x][0]=0; 32 cnt[x][0]=1; 33 q.push(node(x,0,0)); 34 while(!q.empty()){ 35 node aa=q.top(); 36 q.pop(); 37 if(vis[aa.x][aa.y]) continue; 38 vis[aa.x][aa.y]=true; 39 for(int i=0;i<mapp[aa.x].size();i++){ 40 if(aa.y==0){ 41 if(len[aa.x][0]+mapp[aa.x][i].len<=len[mapp[aa.x][i].x][0]){ 42 if(len[aa.x][0]+mapp[aa.x][i].len==len[mapp[aa.x][i].x][0]){ 43 cnt[mapp[aa.x][i].x][0]+=cnt[aa.x][0]; 44 } 45 else{ 46 cnt[mapp[aa.x][i].x][1]=cnt[mapp[aa.x][i].x][0]; 47 cnt[mapp[aa.x][i].x][0]=cnt[aa.x][0]; 48 len[mapp[aa.x][i].x][1]=len[mapp[aa.x][i].x][0]; 49 q.push(node(mapp[aa.x][i].x,1,len[mapp[aa.x][i].x][1])); 50 } 51 len[mapp[aa.x][i].x][0]=len[aa.x][0]+mapp[aa.x][i].len; 52 q.push(node(mapp[aa.x][i].x,0,len[mapp[aa.x][i].x][0])); 53 } 54 else if(len[aa.x][0]+mapp[aa.x][i].len<=len[mapp[aa.x][i].x][1]){ 55 if(len[aa.x][0]+mapp[aa.x][i].len==len[mapp[aa.x][i].x][1]){ 56 cnt[mapp[aa.x][i].x][1]+=cnt[aa.x][0]; 57 } 58 else{ 59 cnt[mapp[aa.x][i].x][1]=cnt[aa.x][0]; 60 } 61 len[mapp[aa.x][i].x][1]=len[aa.x][0]+mapp[aa.x][i].len; 62 q.push(node(mapp[aa.x][i].x,1,len[mapp[aa.x][i].x][1])); 63 } 64 } 65 else{ 66 if(len[aa.x][1]+mapp[aa.x][i].len<=len[mapp[aa.x][i].x][1]){ 67 if(len[aa.x][1]+mapp[aa.x][i].len==len[mapp[aa.x][i].x][1]){ 68 cnt[mapp[aa.x][i].x][1]+=cnt[aa.x][1]; 69 } 70 else{ 71 cnt[mapp[aa.x][i].x][1]=cnt[aa.x][1]; 72 } 73 len[mapp[aa.x][i].x][1]=len[aa.x][1]+mapp[aa.x][i].len; 74 q.push(node(mapp[aa.x][i].x,1,len[mapp[aa.x][i].x][1])); 75 } 76 } 77 } 78 } 79 } 80 int main(int argc, char const *argv[]){ 81 int T,x,y,a,b,c; 82 cin>>T; 83 while(T--){ 84 cin>>n>>m; 85 for(int i=1;i<=n;i++){ 86 mapp[i].clear(); 87 } 88 for(int i=0;i<m;i++){ 89 cin>>a>>b>>c; 90 mapp[a].push_back(edge(b,c)); 91 } 92 cin>>x>>y; 93 dij(x,y); 94 if(len[y][0]+1==len[y][1]){ 95 cout<<cnt[y][0]+cnt[y][1]<<endl; 96 } 97 else{ 98 cout<<cnt[y][0]<<endl; 99 }100 }101 return 0;102 }
阅读全文
1 0
- HDU 1688 Sightseeing
- hdu 1688 Sightseeing
- HDU-1688-Sightseeing
- HDU 1688 Sightseeing
- hdu 1688 Sightseeing
- HDU 1688 Sightseeing 【次短路条数】
- POJ 3463 && HDU 1688 Sightseeing 次短路
- HDU-1688 Sightseeing(最短路+次短路)
- HDU-1688 Sightseeing 最短路与“次短路”条数
- hdu 1688 Sightseeing【最短路,次短路条数】
- hdu 1688 Sightseeing(最短路+次短路条数)
- Sightseeing
- Sightseeing
- HDU 1688 Sightseeing 求最短路和次短路条数之和
- HDU 1688 Sightseeing 最短路 及次短路 路径长度和路径数
- hdu 1956 || poj 1637 Sightseeing tour (混合图欧拉回路)
- HDU 1599 find the mincost route 、 poj 1734 Sightseeing trip
- hdu 1956 Sightseeing tour(混合图欧拉回路)
- Future结合RxJava 实现异步调用
- HDU 3339 In Action(最短路+01背包)
- HDU 2722 Here We Go(relians) Again
- HDU 1839 Delay Constrained Maximum Capacity Path(最短路+二分搜索)
- HDU 4179 Difficult Routes
- HDU 1688 Sightseeing
- HDU 3873 Invade the Mars
- HDU 3311 Dig The Wells(spfa模板)
- HDU 2888 Check Corners
- C# AsyncIO
- HDU 2377 Bus Pass
- 如何使用Kotlin进行Android开发
- string
- HDU 2833 WuKong