Language:
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 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:
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. |
旅游团从S出发到F 为了节省费用需要选择最短的路径或者比最短路径长一个单位的路径 求满足条件的路径的数量
同样使用Dijkstra算法
主要的改变就是数组都开到了二维,第二维用来表示是最短路还是次短路
比如dis[][]数组和vis[][]数组
而cnt数组使用来存取最短路和次短路的次数
那么最外层的循环就要到2*n-1次了,其中n-1次是用来求最短路的,还有n次是次短路的
然后松弛的条件就要改变了,有四种情况
1.比最短路短2.等于最短路3.长于最短路但短于次短路4.等于次短路
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 20010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
struct Edge
{
int v,next,w;
}edge[MAXM];
int dis[MAXN][2],n,m;
int head[MAXN],tot;
bool vis[MAXN][2];
int cnt[MAXN][2];
void init()
{
tot=0;
MEM(head,-1);
}
void addedge(int u,int v,int w)
{
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
int Dijkstra(int s,int t)
{
MEM(vis,0); MEM(cnt,0);
for(int i=1;i<=n;i++)
dis[i][0]=dis[i][1]=INF;
cnt[s][0]=1;
dis[s][0]=0;
for(int i=1;i<2*n;i++)
{
int mi=INF;
int flag,u;
for(int j=1;j<=n;j++)
{
if(!vis[j][0]&&dis[j][0]<mi)
{
u=j;
flag=0;
mi=dis[j][0];
}
else if(!vis[j][1]&&dis[j][1]<mi)
{
u=j;
flag=1;
mi=dis[j][1];
}
}
if(mi==INF) break;
vis[u][flag]=1;
for(int j=head[u];j!=-1;j=edge[j].next)
{
int w=edge[j].w;
int v=edge[j].v;
if(dis[v][0]>mi+w)
{
dis[v][1]=dis[v][0];
cnt[v][1]=cnt[v][0];
dis[v][0]=mi+w;
cnt[v][0]=cnt[u][flag];
}
else if(dis[v][0]==mi+w) cnt[v][0]+=cnt[u][flag];
else if(dis[v][1]>mi+w)
{
dis[v][1]=mi+w;
cnt[v][1]=cnt[u][flag];
}
else if(dis[v][1]==mi+w) cnt[v][1]+=cnt[u][flag];
}
}
int ans=0;
if(dis[t][1]==dis[t][0]+1) ans=cnt[t][1]+cnt[t][0];
else ans=cnt[t][0];
return ans;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
init();
scanf("%d%d",&n,&m);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
int s,t;
scanf("%d%d",&s,&t);
int ans=Dijkstra(s,t);
printf("%d\n",ans);
}
return 0;
}