poj 3463 最短路与次短路的方案数求解

Sightseeing

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8968   Accepted: 3139

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, BN, AB 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, FN and SF: 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

BAPC 2006 Qualification

题意:

题解:

  1 /******************************
  2 code by drizzle
  3 blog: www.cnblogs.com/hsd-/
  4 ^ ^    ^ ^
  5  O      O
  6 ******************************/
  7 //#include<bits/stdc++.h>
  8 #include<map>
  9 #include<set>
 10 #include<cmath>
 11 #include<queue>
 12 #include<bitset>
 13 #include<math.h>
 14 #include<vector>
 15 #include<string>
 16 #include<stdio.h>
 17 #include<cstring>
 18 #include<iostream>
 19 #include<algorithm>
 20 //#pragma comment(linker, "/STACK:102400000,102400000")
 21 using namespace std;
 22 #define  A first
 23 #define B second
 24 const int mod=1000000007;
 25 const int MOD1=1000000007;
 26 const int MOD2=1000000009;
 27 const double EPS=0.00000001;
 28 typedef __int64 ll;
 29 const ll M22OD=1000000007;
 30 const int INF=1000000010;
 31 const ll MAX=1ll<<55;
 32 const double eps=1e-14;
 33 const double inf=~0u>>1;
 34 const double pi=acos(-1.0);
 35 typedef double db;
 36 typedef unsigned int uint;
 37 typedef unsigned long long ull;
 38 struct node
 39 {
 40     int v,next,w;
 41 }edge[500005];
 42 int d[1005][2],e,n,m;
 43 int cnt[1005][2];
 44 int head[1005];
 45 bool vis[1005][2];
 46 void init()
 47 {
 48     e=0;
 49     memset(head,0,sizeof(head));
 50 }
 51 void insert(int x,int y,int w)
 52 {
 53     e++;
 54     edge[e].v=y;
 55     edge[e].w=w;
 56     edge[e].next=head[x];
 57     head[x]=e;
 58 }
 59 int dijkstra(int s,int t)
 60 {
 61     int flag,u;
 62     memset(vis,0,sizeof(vis));
 63     memset(cnt,0,sizeof(cnt));
 64     for(int i=1;i<=n;i++){
 65         d[i][0]=d[i][1]=INF;
 66     }
 67     cnt[s][0]=1;
 68     d[s][0]=0;
 69     for(int i=1;i<=2*n;i++)
 70     {
 71         int mini=INF;
 72         for(int j=1;j<=n;j++)
 73         {
 74             if(!vis[j][0]&&d[j][0]<mini)
 75             {
 76                 u=j;
 77                 flag=0;
 78                 mini=d[j][0];
 79             }
 80             else if(!vis[j][1]&&d[j][1]<mini)
 81             {
 82                 u=j;
 83                 flag=1;
 84                 mini=d[j][1];
 85             }
 86         }
 87         if(mini==INF) break;
 88         vis[u][flag]=1;
 89         for(int j=head[u];j;j=edge[j].next)
 90         {
 91             int w=edge[j].w;
 92             int v=edge[j].v;
 93             if(d[v][0]>mini+w){
 94                 d[v][1]=d[v][0];
 95                 cnt[v][1]=cnt[v][0];
 96                 d[v][0]=mini+w;
 97                 cnt[v][0]=cnt[u][flag];
 98             }
 99             else if(d[v][0]==mini+w) cnt[v][0]+=cnt[u][flag];
100             else if(d[v][1]>mini+w){
101                 d[v][1]=mini+w;
102                 cnt[v][1]=cnt[u][flag];
103             }
104             else if(d[v][1]==mini+w) cnt[v][1]+=cnt[u][flag];
105         }
106     }
107     int ans=0;
108     if(d[t][1]==d[t][0]+1) ans=cnt[t][1]+cnt[t][0];
109     else  ans=cnt[t][0];
110     return ans;
111 }
112 int main()
113 {
114     int s,t, T,x,y,w;
115     scanf("%d",&T);
116      while(T--)
117      {
118          init();
119          scanf("%d %d",&n,&m);
120          for(int i=1;i<=m;i++)
121          {
122              scanf("%d %d %d",&x,&y,&w);
123              insert(x,y,w);
124          }
125          scanf("%d %d",&s,&t);
126          printf("%d\n",dijkstra(s,t));
127      }
128     return 0;
129 }
时间: 2024-08-05 19:48:50

poj 3463 最短路与次短路的方案数求解的相关文章

poj 3463 Sightseeing (dij 求最短路和次短路并计数)

dijkstra求最短路和次短路的求法和计算  模板 dijkstra求最短路的变形. 外循环要循环2*n-1次,因为dis[n][2]有2*n个状态,而dis[s][0]已经用过一次. 算法: 1.如果比最短路短就更新最短路和次短路. 2.如果和最短路相等,更新最短路的计数. 3.如果和次短路相等,更新次短路的方法数. 4.如果比次短路短,更新次短路. #include<cstdio> #include<iostream> #include<cstring> #inc

poj 3463 Sightseeing(最短路+次短路)

http://poj.org/problem?id=3463 大致题意:给出一个有向图,从起点到终点求出最短路和次短路的条数之和. 解法: 用到的数组:dis[i][0]:i到起点的最短路,dis[i][1]:i到起点的严格次短路 vis[i][0],vis[i][1]:同一维的vis数组,标记距离是否已确定 sum[i][0]:i到起点的最短路条数,sum[i][1]:i到起点的次短路条数 同一维dijkstra,内循环先找出最短的距离(次短路或最短路)d,然后枚举与该点相连的点: if(d

poj 3463 Sightseeing——次短路计数

题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不要区分k的fx. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=1005,M=10005; int T

poj 3463 Sightseeing(次短路+条数统计)

/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #define maxn 1010 using namespace std; int T,n,m,num,head[m

poj 3463 最短路+次短路

独立写查错不能,就是维护一个次短路的dist 题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. Sample Input25 81 2 31 3 21 4 52 3 12 5 33 4 23 5 44 5 31 55 62 3 13 2 13 1 104 5 25 2 75 2 74 1 Sample Output32 2015-05-14 1 #include<cstdio> 2 #include<iostream> 3 #include<algo

poj 3463 次短路

题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. 当年数据结构课程设计用A*做过,现在忘光了,2333 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<queue> 5 #define VM 1005 6 #define EM 10010 7 using namespace std; 8 const int inf=0x3f3f3f3f

(最短路深入)POJ 3463 - Sightseeing

题意: 给一个有向图,计算最短路和比最短路少1的路的条数的和. 分析: 这题真的写死我了. 因为之前很少接触最短路问题,所谓偶尔遇到一次也是套的模板,根本没有细细思考过dijsktra算法.所以栽在了这题上. 这题就是求最短路和次短路. 核心思想在于修改最短路松弛的条件,并且每个节点同时维护最短路和次短路. 很多博主写的很详细,我也不多说了,只是写个博文记录一下自己有多渣,在学习算法的道路上自己真的思考的不够多,也不够努力. 代码: 1 #include <set> 2 #include &l

POJ 3463 Sightseeing (最短路 次短路)

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 mak

POJ 1062 昂贵的聘礼 最短路

Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000金币就行了."探险家就跑到大祭司那里,向他要求皮袄或水晶球,大祭司要他用金币来换,或者替他弄来其他的东西,他可以降低价格.探险家于是又跑到其他地方,其他人也提出了类似的要求