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 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.
题意
询问最短路径和次短路径的条数,(次短路径的长度必须是最短路径的单位长度+1)。
题解
1.if(x<最小)更新最短路和次短路
2.if(x==最小)更新最短路数量
3.if(x<次小)更新次短路
4.if(x==次小)更新次短路数量
记录数量用cnt数组记录一下, 需要更新某个结点最短路的时候把前驱结点的数组赋值给此结点,如果恰好距离相等,就是加法原理了。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=1005;
#define ms(a,n) memset(a,n,sizeof(a))
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{
int s,cost,pos;
bool operator <(const edge& x)const
{
return cost>x.cost;//边权小的优先
}
};
typedef pair<int,int> P;//first为顶点编号,second为权值
int n,m;
int dist[maxn][2];
int cnt[maxn][2];
bool vis[maxn][2];
vector<P> G[maxn];
void dijkstra(int s,int f)
{
priority_queue<edge> que;
ms(dist,INF);
ms(cnt,0);
ms(vis,false);
cnt[s][0]=1;
dist[s][0]=0;
que.push((edge){s,0,0});
while(!que.empty())
{
edge p=que.top();que.pop();
if(vis[p.s][p.pos])
continue;
int u=p.s,t=p.pos;
vis[u][t]=true;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].first,d=G[u][i].second;
if(dist[v][0]>d+dist[u][t])
{
dist[v][1]=dist[v][0];
cnt[v][1]=cnt[v][0];
dist[v][0]=d+dist[u][t];
cnt[v][0]=cnt[u][t];
que.push((edge){v,dist[v][0],0});
que.push((edge){v,dist[v][1],1});
}
else if(dist[v][0]==d+dist[u][t])
cnt[v][0]+=cnt[u][t];
else if(dist[v][1]>d+dist[u][t])
{
dist[v][1]=d+dist[u][t];
cnt[v][1]=cnt[u][t];
que.push((edge){v,dist[v][1],1});
}
else if(dist[v][1]==d+dist[u][t])
cnt[v][1]+=cnt[u][t];
}
}
int ans=cnt[f][0];
if(dist[f][1]==dist[f][0]+1)
ans+=cnt[f][1];
printf("%d\n",ans);
}
void clear()
{
for(int i=1;i<=n;i++)
G[i].clear();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
clear();
for(int i=0;i<m;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
G[u].push_back(P(v,c));
}
int s,f;
scanf("%d%d",&s,&f);
dijkstra(s,f);
}
return 0;
}
原文地址:https://www.cnblogs.com/orion7/p/8570221.html