您的位置:首页 > 其它

3463 Sightseeing dijkstra求最短路和次短路

2010-07-17 15:04 316 查看
Sightseeing

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 2864Accepted: 949
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:

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.

/*
本题若开始直接在Dijkstra中加入针对加1的递推式, 必须在Dijkstra的过程中加入DFS, 很繁琐. 改换求次短路, 然后判断次短路是否比最短路长1.

求次短路需要深刻理解Dijkstra的定标技术, 思想就是每次都将某个状态的标记(即长度)永久固定, 普通的Dijkstra的状态是一维的, 即顶点编号占一维, 每次确定一个顶点编号下的最短路. 现在将状态扩展到二维, 第一维仍然是顶点编号, 第二维的长度只有2, 用于分别记录最短路和次短路, 直观点说就是开一个二维数组, 第一维是顶点数, 第二维0号位置放最短路, 1号位置放次短路. 这样的二维数组有两个, 一个记录距离, 一个用于计数. 同样的, 用于记录状态的数组变成了二维, 放进堆中的状态也必须是"二维"的, 这里的"二维"并不是要你开个二维数组, 而是需要在放入堆中的结构体里多加一个标记变量, 用于标识到底是最短路还是次短路, 当然, 用于标记已经确定最短路的点的closed表同样要变成二维的, 循环结束条件必须是堆为空, 因为要计数, 就必须遍历所有情况.

具体在做状态转移的时候, 拿到当前状态, 扩展下一状态, 设当前状态长度为d, 下一状态最短路和次短路状态分别是d0和d1, 则:

1. d小于d0, 则d1=d0,d0=d, 计数都重置为所赋的值对应的计数.

2. d等于d0, 则累加最短路计数.

3. d小于d1, 则d1=d, 重置次短路计数为所赋的值对应的计数.

4. d等于d1, 则累加次短路计数
*/
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
class Node
{
public:
int e,w;//表示终点和边权
};
const int inf=(1<<25);
int main()
{
int ci;
cin>>ci;
while(ci--)
{
vector<Node> G[1005];//用邻接表存边
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
Node q;int u;
cin>>u>>q.e>>q.w;
G[u].push_back(q);
}
int s,f;//起点和终点
cin>>s>>f;
//dijkstra 求最短路和次短路
int flag[1005][2];
int dis[1005][2],cnt[1005][2];//0表示最短路,1表示次短路
memset(flag,0,sizeof(flag));
for(int i=1;i<=n;i++) dis[i][0]=dis[i][1]=inf;
dis[s][0]=0;cnt[s][0]=1;//初始化
for(int c=0;c<2*n;c++) //找最短路和次短路,故要进行2*n次循环 也可以改成while(1)
{
int temp=inf,u=-1,k;//找s-S'集合中的最短路径,u记录点的序号,k记录是最短路或者是次短路
for(int j=1;j<=n;j++)
{
if(flag[j][0]==0&&temp>dis[j][0]) temp=dis[j][0],u=j,k=0;
else if(flag[j][1]==0&&temp>dis[j][1]) temp=dis[j][1],u=j,k=1;
}
if(temp==inf) break;//S'集合为空或者不联通,算法结束
//更新路径
flag[u][k]=1;
for(int l=0;l<G[u].size();l++)
{
int d=dis[u][k]+G[u][l].w,j=G[u][l].e;//important
// cout<<d<<"...."<<j<<endl;
//4种情况
if(d<dis[j][0])
{
dis[j][1]=dis[j][0];cnt[j][1]=cnt[j][0];
dis[j][0]=d;cnt[j][0]=cnt[u][k];
}
else if(d==dis[j][0])
{
cnt[j][0]+=cnt[u][k];
}
else if(d<dis[j][1])
{
dis[j][1]=d;cnt[j][1]=cnt[u][k];
}
else if(d==dis[j][1])
{
cnt[j][1]+=cnt[u][k];
}
}
}

int num=cnt[f][0];
if(dis[f][0]+1==dis[f][1]) num+=cnt[f][1];
cout<<num<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: