Sightseeing
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 Bwhich 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:
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
|
题意:
找s到e的最短路和长度为最短路+1的路径条数。
思路:
找次短路将dp改为二维就好了,第二维01表示是最短路、次短路。
然后每次找最近的状态(哪个点、01)。
最后根据边松弛,分四种情况。
min+w<dp[v][0] 则更新dp[v][0] 且将dp[v][0]的值给到dp[v][1]。
min+w==dp[v][0] 累计个数。
min+w<dp[v][1] 更新dp[v][1]。
min==dp[v][1] 累计个数。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 20005 #define mod 1000000007 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 typedef long long ll; using namespace std; int n,m,ans,cnt,tot,flag; int s,e; int pp[maxn],dist[maxn][maxn],dp[maxn][2],num[maxn][2]; bool vis[maxn][2]; struct Node { int v,w,next; }edge[MAXN]; void addedge(int u,int v,int w) { cnt++; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=pp[u]; pp[u]=cnt; } void solve() { int i,j,t,now,cur,mi,u,v; memset(dp,0x3f,sizeof(dp)); memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); now=s; num[now][0]=1; dp[now][0]=0; for(i=1;i<=2*n;i++) { mi=INF; for(j=1;j<=n;j++) { if(!vis[j][0]&&mi>dp[j][0]) { now=j,flag=0; mi=dp[j][0]; } if(!vis[j][1]&&mi>dp[j][1]) { now=j,flag=1; mi=dp[j][1]; } } if(mi==INF) break ; // printf("now:%d flag:%d\n",now,flag); vis[now][flag]=1; for(j=pp[now];j;j=edge[j].next) { v=edge[j].v; if(mi+edge[j].w<dp[v][0]) { dp[v][1]=dp[v][0]; num[v][1]=num[v][0]; dp[v][0]=mi+edge[j].w; num[v][0]=num[now][flag]; } else if(mi+edge[j].w==dp[v][0]) { num[v][0]+=num[now][flag]; } else if(mi+edge[j].w<dp[v][1]) { dp[v][1]=mi+edge[j].w; num[v][1]=num[now][flag]; } else if(mi+edge[j].w==dp[v][1]) { num[v][1]+=num[now][flag]; } } } ans=num[e][0]; if(dp[e][1]==dp[e][0]+1) ans+=num[e][1]; } int main() { int i,j,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(pp,0,sizeof(pp)); cnt=0; int u,v,w; for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } scanf("%d%d",&s,&e); solve(); printf("%d\n",ans); } return 0; } /* 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 */