hdu 1688 Sightseeing (最短路径)

Sightseeing


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

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

Source

华东区大学生程序设计邀请赛_热身赛

Recommend

lcy   |   We have
carefully selected several similar problems for you:  2833 2433 2363 2482 2377

题意就是 求最短路径数和比最短路径大1的路径数的和。

和3191几乎一样+ +自己写了一下次短路,结果wa了好多次...不过这种解法效率真的很高!


 1 //15MS    528K    2372 B    C++
2 #include<iostream>
3 #include<queue>
4 #include<vector>
5 #define inf 0x3fffffff
6 #define N 1005
7 using namespace std;
8 struct edge{
9 int v,w;
10 edge(int a,int b){
11 v=a;w=b;
12 }
13 };
14 struct node{
15 int v,w;
16 int mark;
17 bool operator < (const node &p) const{
18 return p.w<w;
19 //return p.v<v;
20 }
21 };
22 vector<edge>V[N];
23 int n,m,s,e;
24 int d[N][3];
25 int dp[N][3];
26 int vis[N][3];
27 void dijkstra(int s)
28 {
29 for(int i=0;i<=n;i++)
30 d[i][1]=d[i][2]=inf;
31 memset(dp,0,sizeof(dp));
32 memset(vis,0,sizeof(vis));
33 priority_queue<node>Q;
34 node p,q;
35 d[s][1]=0;
36 dp[s][1]=1;
37 p.w=0,p.mark=1,p.v=s;
38 Q.push(p);
39 while(!Q.empty()){
40 p=Q.top();
41 Q.pop();
42 if(vis[p.v][p.mark]) continue;
43 vis[p.v][p.mark]=1;
44 int m=V[p.v].size();
45 for(int i=0;i<m;i++){
46 int v=V[p.v][i].v;
47 int w=V[p.v][i].w;
48 if(!vis[v][1] && d[v][1]>p.w+w){
49 if(d[v][1]!=inf){
50 d[v][2]=d[v][1];
51 dp[v][2]=dp[v][1];
52 q.v=v,q.mark=2,q.w=d[v][2];
53 Q.push(q);
54 }
55 d[v][1]=p.w+w;
56 dp[v][1]=dp[p.v][p.mark];
57 q.v=v,q.mark=1,q.w=d[v][1];
58 Q.push(q);
59 }else if(!vis[v][1] && d[v][1]==p.w+w){
60 dp[v][1]+=dp[p.v][p.mark];
61 }else if(!vis[v][2] && d[v][2]>p.w+w){
62 d[v][2]=p.w+w;
63 dp[v][2]=dp[p.v][p.mark];
64 q.v=v,q.mark=2,q.w=d[v][2];
65 Q.push(q);
66 }else if(!vis[v][2] && d[v][2]==p.w+w){
67 dp[v][2]+=dp[p.v][p.mark];
68 }
69 }
70 }
71 }
72 int main(void)
73 {
74 int u,v,w;
75 int t;
76 scanf("%d",&t);
77 while(t--)
78 {
79 scanf("%d%d",&n,&m);
80 for(int i=0;i<=n;i++)
81 V[i].clear();
82 for(int i=0;i<m;i++){
83 scanf("%d%d%d",&u,&v,&w);
84 V[u].push_back(edge(v,w));
85 //V[v].push_back(edge(u,w));
86 }
87 scanf("%d%d",&s,&e);
88 dijkstra(s);
89 int ans=dp[e][1];
90 //printf("*%d %d %d\n",ans,d[e][1],d[e][2]);
91 if(d[e][2]==d[e][1]+1) ans+=dp[e][2];
92 printf("%d\n",ans);
93 }
94 return 0;
95 }

时间: 2024-10-05 22:30:15

hdu 1688 Sightseeing (最短路径)的相关文章

HDU 1688 Sightseeing

题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio.h> #include <string.h> #include <iostream> #include <vector> using namespace std; #define maxn 1010 struct Node { int to, val; Node(in

hdu 1688 Sightseeing【最短路,次短路条数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题意:求最短路和次短路条数如果次短路长度=最短路长度+1 这输出次短路条数+最短路条数,否则输出最短路条数 分析:这是到模版题,献上模版: #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #include<

HDU 1688 Sightseeing 【输出最短路+次短路条数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思路: 1.最短路和次短路是紧密相连的,在最短路松弛操作中,当我们找到一条更短的路径,也就意味着之前的路径不再是最短路,而成为了次短路,利用这个关系可以实现状态的转移. 2.好久没写优先队列了,都忘记了加个 priority_queue, 这样才能写重载,才能排序. 注释在代码里: 1 #includ

POJ 3255 &amp;&amp; HDU 1688 &amp;&amp; HDU 3191 次短路问题

POJ 3255 Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7627   Accepted: 2798 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old h

HDU 2112 HDU Today,最短路径算法,Dijkstra

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13396    Accepted Submission(s): 3144 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HD

hdu 2833 WuKong(最短路径+记忆化搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=2833 大致题意:给定一个无向图,以及悟空和师傅起点与终点,求它们分别从起点到终点的最短路径中经过相同的点的最大个数. 思路:首先dijkstra求出最短路,那么如果有dis[a] + map[a][b] = dis[b],则边(a,b)一定在最短路径上.根据这一定理可以求出所有最短路径.然后类似于求最长公共子序列求经过的相同点的最大个数. 即若a==b ,dp[a][b] = max(dp[i][j]+1)

hdu 1688

最短路与次短路条数 #include <stdio.h> #include <string.h> #define N 10005 #define INF 0x3f3f3f3f struct Edge{ int u,val,next; }e[2*N]; int p[N],vis[N][2],d[N][2],cnt[N][2]; void add(int n,int m) { int i,x,y,cost,cout = 1; for(i = 1 ; i <= n ; i++) p

HDU 2112 HDU Today 【最短路径 dijkstra &amp; floyed】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19487    Accepted Submission(s): 4578 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

【HDOJ】1688 Sightseeing

Dijkstra求解次短路径,使用cnt和dis数组记录最小.次小的个数和长度.重写更新操作. 1 /* 1688 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <vector> 8 #include <algorithm> 9 #include <