题目

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的路,然后问有几条路满足条件。

直接用dijkstra算出最短路和次短路,数组开二维来维护最短路和次短路。

更新条件:
1,起点到x距离小于最小距离,那么两个都要更新。
2,距离小于次小,更新次小距离。
3,这个距离与最小或者相等,方法数+1。

在运行Dijkstra时,因为最小和次小都要查找,一个是n-1次,另一个需要n次遍历,所以一共循环2n-1次。

代码

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int maxe=;
const int maxn=;
const int Inf=0x3f3f3f3f; struct Edge{
int to,next;
int v;
}e[maxn<<]; int n,m,cnt,head[maxe];
int cdis[maxe][],dis[maxe][],vis[maxe][];
int mdd,des; void adde(int u,int v,int w){
e[cnt].to=v;
e[cnt].v=w;
e[cnt].next=head[u];
head[u]=cnt++;
} void Dijkstra(){
memset(vis,,sizeof(vis));
memset(cdis,,sizeof(cdis));
for(int i=;i<=n;i++){
dis[i][]=Inf;
dis[i][]=Inf;
}
dis[mdd][]=;
cdis[mdd][]=;
int k,tmp,flag;
for(int i=;i<=*n-;i++){
tmp=Inf;
for(int j=;j<=n;j++)
if(!vis[j][] && tmp>dis[j][]){
k=j;
flag=;
tmp=dis[j][];
}else if(!vis[j][] && tmp>dis[j][]){
k=j;
flag=;
tmp=dis[j][];
}
if(tmp==Inf)
break;
vis[k][flag]=;
for(int j=head[k];j!=-;j=e[j].next){
int v=e[j].to;
if(dis[v][]>tmp+e[j].v){
dis[v][]=dis[v][];
cdis[v][]=cdis[v][];
dis[v][]=tmp+e[j].v;
cdis[v][]=cdis[k][flag];
}else if(dis[v][]==tmp+e[j].v)
cdis[v][]+=cdis[k][flag];
else if(dis[v][]>tmp+e[j].v){
dis[v][]=tmp+e[j].v;
cdis[v][]=cdis[k][flag];
}else if(dis[v][]==tmp+e[j].v)
cdis[v][]+=cdis[k][flag];
}
}
} int main(){
int t;
scanf("%d",&t);
while(t--){
cnt=;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);
}
scanf("%d%d",&mdd,&des);
Dijkstra();
int ans=cdis[des][];
if(dis[des][]==dis[des][]+)
ans+=cdis[des][];
printf("%d\n",ans);
}
return ;
}

POJ 3463 Sightseeing 【最短路与次短路】的更多相关文章

  1. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  2. POJ - 3463 Sightseeing 最短路计数+次短路计数

    F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...

  3. poj 3463 Sightseeing——次短路计数

    题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...

  4. poj 3463 Sightseeing(次短路+条数统计)

    /* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...

  5. POJ 3463 Sightseeing (次短路)

    题意:求两点之间最短路的数目加上比最短路长度大1的路径数目 分析:可以转化为求最短路和次短路的问题,如果次短路比最短路大1,那么结果就是最短路数目加上次短路数目,否则就不加. 求解次短路的过程也是基于 ...

  6. POJ 3463 Sightseeing 题解

    题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...

  7. POJ 3463 Sightseeing (次短路经数)

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:10005   Accepted: 3523 Descr ...

  8. POJ 3463 Sightseeing

    最短路+次短路(Dijkstra+priority_queue) 题意是要求你找出最短路的条数+与最短路仅仅差1的次短路的条数. 開始仅仅会算最短路的条数,和次短路的长度.真是给次短路条数跪了.ORZ ...

  9. POJ 3463 有向图求次短路的长度及其方法数

    题目大意: 希望求出走出最短路的方法总数,如果次短路只比最短路小1,那也是可取的 输出总的方法数 这里n个点,每个点有最短和次短两种长度 这里采取的是dijkstra的思想,相当于我们可以不断找到更新 ...

随机推荐

  1. (Java实现) 洛谷 P1071 潜伏者

    题目描述 R国和 S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S国的 R 国间谍小 C终于摸清了 S 国军用密码的编码规则: 1. S 国军方内部欲发送的原信息经过 ...

  2. Java实现 LeetCode 722 删除注释(暴力筛选)

    722. 删除注释 给一个 C++ 程序,删除程序中的注释.这个程序source是一个数组,其中source[i]表示第i行源码. 这表示每行源码由\n分隔. 在 C++ 中有两种注释风格,行内注释和 ...

  3. Java实现蓝桥杯凑算式(全排列)

    题目6.凑算式 凑算式 B DEF A + - + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中AI代表19的数字,不同的字母代表不同的数字. 比如: ...

  4. Java实现 蓝桥杯 算法训练 纪念品分组

    问题描述 元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作.为使得参加晚会的同学所获得的纪念品价值 相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品,并且每组纪念品的价 ...

  5. Java实现LeetCode_0028_ImplementStrStr

    package javaLeetCode.primary; import java.util.Scanner; public class ImplementStrStr_28 { public sta ...

  6. call,apply,bind的理解

    2020-03-19 call,apply,bind的理解 先说区别call, apply基本上没什么不一样,唯一不一样的地方是传参方式不同 但是bind和call,apply有区别.bind是重新绑 ...

  7. 2019-02-02 Python学习——生成器杨辉三角,迭代器与可迭代对象的区别

    练习 杨辉三角定义如下: 1 / \ 1 1 / \ / \ 1 2 1 / \ / \ / \ 1 3 3 1 / \ / \ / \ / \ 1 4 6 4 1 / \ / \ / \ / \ / ...

  8. 动作函数-web_url

    web_url("WebTours", "URL=http://127.0.0.1:1080/WebTours/", "TargetFrame=&qu ...

  9. 【JMeter_10】JMeter逻辑控制器__ForEach控制器<ForEach Controller>

    ForEach控制器<ForEach Controller> 业务逻辑: ForEach控制器一般与用户自定义变量/JDBC结果变量一起使用,可以认为他就是一种遍历型循环,该节点下的脚本内 ...

  10. cc40a_demo_Cpp_智能指针c++_txwtech

    //40_21days_Cpp_智能指针c++_cc40a_demo.cpp_txwtech //智能指针.auto_ptr类//*常规指针-容易产生内存泄漏,内存被占满,程序就死机,或者系统死机// ...