POJ 3463 Sightseeing
2016-07-13 21:39
681 查看
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.
【题目分析】
求最短路和次短路,用求最短路的方法同时求两种路径就可以,再略微的维护一下就好了。
需要注意的地方是 不能去重(WA X1)要清零 (WA X2).
【代码】
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.
【题目分析】
求最短路和次短路,用求最短路的方法同时求两种路径就可以,再略微的维护一下就好了。
需要注意的地方是 不能去重(WA X1)要清零 (WA X2).
【代码】
#include <cstdio> #include <cstring> int tt,n,m,en,S,T,a,b,c; const int inf=0x3f3f3f3f; int h[2001],to[40001],ne[40001],w[40001]; inline void add(int a,int b,int c) {to[en]=b;ne[en]=h[a];w[en]=c;h[a]=en++;} int vis[2001][2],dis[2001][2]; int count[2001][2]; int main() { scanf("%d",&tt); for (int z=1;z<=tt;++z) { en=0; memset(h,-1,sizeof h); scanf("%d%d",&n,&m); for (int zz=1;zz<=m;++zz) scanf("%d%d%d",&a,&b,&c),add(a,b,c); scanf("%d%d",&S,&T); memset(vis,0,sizeof vis); memset(count,0,sizeof count); for (int i=1;i<=n;++i) dis[i][0]=inf,dis[i][1]=inf; dis[S][0]=0;count[S][0]=1; int tmp,flag,k; for (int i=1;i<=2*n-1;++i) { tmp=inf; for (int j=1;j<=n;++j) if (!vis[j][0]&&tmp>dis[j][0]) { k=j; flag=0; tmp=dis[j][0]; } else if (!vis[j][1]&&tmp>dis[j][1]) { k=j; flag=1; tmp=dis[j][1]; } if (tmp==inf) break; vis[k][flag]=1; for (int j=h[k];j!=-1;j=ne[j]) { int v=to[j]; if (dis[v][0]>tmp+w[j]) { dis[v][1]=dis[v][0]; count[v][1]=count[v][0]; dis[v][0]=tmp+w[j]; count[v][0]=count[k][flag]; } else if (dis[v][0]==tmp+w[j]) { count[v][0]+=count[k][flag]; } else if (dis[v][1]>tmp+w[j]) { dis[v][1]=tmp+w[j]; count[v][1]=count[k][flag]; } else if (dis[v][1]==tmp+w[j]) { count[v][1]+=count[k][flag]; } } } if (dis[T][1]==dis[T][0]+1) count[T][0]+=count[T][1]; printf("%d\n",count[T][0]); } }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- [数论]poj2635__The Embarrassed Cryptographer
- [二分图匹配]poj2446__Chessboard
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题