Sightseeing HDU - 1688 最短路+次短路

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 10^9 = 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

题意:有n个点m条边,每条边都是单向的,给你一个起点和一个终点,如果最短路+1==次短路,输出最短路和次短路条数之和,否则输出最短路条数

思路:

  • 我们用dis[i][0]表示起点到i点的最短路,dis[i][1]表示起点到i点的次短路
  • 我们用book[i][0]表示i点被最短路访问过,book[i][1]表示i点被次短路访问过
  • 我们用num[i][0]表示最短路的条数,num[i][1]表示次短路的条数

我们在进行松弛的时候

  1. 松弛最短路时,当松弛后的距离比未松弛时小时,我们把次短路更新为未进行松弛的时候的距离,并把此短路条数更新为未松弛时最短路的条数,然后更新最短路,并更新最短路条数
  2. 当松弛后的距离等于未松弛时的距离,更新最短路条数(并不是加1,而是加上num[fa][flag],因为从上个点更新过来)。
  3. 松弛次短路时,当松弛后的距离比未松弛时小时,更新次短路距离,并更新最短路条数
  4. 松弛次短路时,当松弛后的距离等于未松弛时的距离,更新次短路条数
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10009;
const int inf=1e9;
int head[maxn],dis[maxn][2],book[maxn][2],cnt,num[maxn][2],n,m;
struct node{
	int id;
	int val;
	int flag;
	int next;
	node(){}
	node(int id,int flag):id(id),flag(flag){}
	friend bool operator < (node x,node y)
	{
		return dis[x.id][x.flag]>dis[y.id][y.flag];
	}
}side[maxn];
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
}
void add(int x,int y,int d)
{
	side[cnt].id=y;
	side[cnt].val=d;
	side[cnt].next=head[x];
	head[x]=cnt++;
}
void dij(int sx,int ex)
{
	memset(book,0,sizeof(book));
	memset(num,0,sizeof(num));
	for(int i=1;i<=n;i++)
		dis[i][0]=dis[i][1]=inf;
	priority_queue<node> q;
	dis[sx][0]=0;
	num[sx][0]=1;
	q.push(node(sx,0));
	while(q.size())
	{
		node now=q.top();
		q.pop();
		if(book[now.id][now.flag]) continue;
		book[now.id][now.flag]=1;
		if(now.id==ex) break;
		for(int i=head[now.id];i!=-1;i=side[i].next)
		{
			int y=side[i].id;
			if(dis[y][0]>dis[now.id][now.flag]+side[i].val)
			{
				if(dis[y][0]!=inf)
				{
					dis[y][1]=dis[y][0];
					num[y][1]=num[y][0];
					q.push(node(y,1));
				}
				dis[y][0]=dis[now.id][now.flag]+side[i].val;
				num[y][0]=num[now.id][now.flag];
				q.push(node(y,0));
			}
			else if(dis[y][0]==dis[now.id][now.flag]+side[i].val)
				num[y][0]+=num[now.id][now.flag];
			else if(dis[y][1]>dis[now.id][now.flag]+side[i].val)
			{
				dis[y][1]=dis[now.id][now.flag]+side[i].val;
				num[y][1]=num[now.id][now.flag];
				q.push(node(y,1));
			}
			else if(dis[y][1]==dis[now.id][now.flag]+side[i].val)
				num[y][1]+=num[now.id][now.flag];
		}
	}
}
int main()
{
	int t,x,y,z,s,e;
	scanf("%d",&t);
	while(t--)
	{
		init();
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
		}
		scanf("%d%d",&s,&e);
		dij(s,e);
		int ans=num[e][0];
		//printf("^^^^^^%d\n",dis[e][0]);
		if(dis[e][0]==dis[e][1]-1) ans+=num[e][1];
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/89295216
今日推荐