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. Source |
题意::旅行团每天固定的从S地出发到达T地,为了省油要求尽量走最短路径或比最短路径长1单位距离的路径,求满足条件的路径条数;
思路:不要用邻接矩阵。因为有重边。
d[x][2]:d[x][0]表示起点到x的最短路、d[x][1]表示起点到x的次短路;
cnt[x][2]:arr[x][0]表示起点到x的最短路条数、cnt[x][1]表示起点到x的次短路的条数;
vis[x][2]对应于x和0、1功能为记录该点是否被访问!
那么如何更新最小和次小路呢?显然我们容易想到下面的方法:
1.if(x<最小)更新最小,次小;
2.else if(x==最小)更新方法数;
3.else if(x<次小)更新次小;
4.else if(x==次小)更新方法数;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1005;
const int M=10005;
const int maxn=0xfffffff;
struct node{
int next,now,w;
}str[M];
int cnt[N][2],d[N][2],head[N],vis[N][2];
int n,m;
int start,end;
void dij()
{
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
d[i][0]=d[i][1]=maxn;
cnt[start][0]=1;
d[start][0]=0;
for(int i=1;i<2*n;i++)
{
int p=-1,k=-1,minn=maxn;
for(int j=1;j<=n;j++)
{
if(!vis[j][0]&&d[j][0]<minn)
{
p=0;
k=j;
minn=d[j][0];
}
else if(!vis[j][1]&&d[j][1]<minn)
{
p=1;
k=j;
minn=d[j][1];
}
}
if(p==-1)
break;
vis[k][p]=1;
for(int j=head[k];j!=-1;j=str[j].next)
{
int zx=str[j].now;
int w=str[j].w;
if(!vis[zx][0]&&d[zx][0]>d[k][p]+w)
{
d[zx][1]=d[zx][0];
cnt[zx][1]=cnt[zx][0];
d[zx][0]=d[k][p]+w;
cnt[zx][0]=cnt[k][p];
}
else if(!vis[zx][0]&&d[zx][0]==d[k][p]+w)
cnt[zx][0]+=cnt[k][p];
else if(!vis[zx][1]&&d[zx][1]>d[k][p]+w)
{
d[zx][1]=d[k][p]+w;
cnt[zx][1]=cnt[k][p];
}
else if(!vis[zx][1]&&d[zx][1]==d[k][p]+w)
cnt[zx][1]+=cnt[k][p];
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
str[i].now=b;
str[i].w=w;
str[i].next=head[a];
head[a]=i;
}
scanf("%d%d",&start,&end);
dij();
if(d[end][0]==d[end][1]-1)
printf("%d\n",cnt[end][0]+cnt[end][1]);
else
printf("%d\n",cnt[end][0]);
}
}