题目

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. CSS3新增伪类有那些?

    p:first-of-type 选择属于其父元素的首个元素 p:last-of-type 选择属于其父元素的最后元素 p:only-of-type 选择属于其父元素唯一的元素 p:only-child ...

  2. 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(二)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  3. Java实现 蓝桥杯VIP 算法训练 完数

    问题描述 一个数如果恰好等于它的因子之和,这个数就称为"完数".例如,6的因子为1.2.3,而6=1+2+3,因此6就是"完数".又如,28的因子为1.2.4. ...

  4. Java实现 LeetCode 228 汇总区间

    228. 汇总区间 给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2","4-> ...

  5. Java中Iterator 和ListIterator的区别

    1.Iterator Iterator的定义如下: public interface Iterator {} Iterator是一个接口,它是集合的迭代器.集合可以通过Iterator去遍历集合中的元 ...

  6. java实现棋盘上的麦子

    ** 棋盘上的麦子** 你一定听说过这个故事.国王对发明国际象棋的大臣很佩服,问他要什么报酬,大臣说:请在第1个棋盘格放1粒麦子,在第2个棋盘格放2粒麦子,在第3个棋盘格放4粒麦子,在第4个棋盘格放8 ...

  7. Linux文件处理命令touch、cat、more、head详解

    命令touch详解 命令touch,所在路径及执行权限为: 可以看到,命令的路径为:/usr/bin/touch ,所以它的执行权限为所有用户 命令基本功能是创建空文件 (可以同时创建多个空文件,文件 ...

  8. 彻底搞懂 etcd 系列文章(二):etcd 的多种安装姿势

    0 专辑概述 etcd 是云原生架构中重要的基础组件,由 CNCF 孵化托管.etcd 在微服务和 Kubernates 集群中不仅可以作为服务注册与发现,还可以作为 key-value 存储的中间件 ...

  9. 如何监控 Linux 服务器状态?

    Linux 服务器我们天天打交道,特别是 Linux 工程师更是如此.为了保证服务器的安全与性能,我们经常需要监控服务器的一些状态,以保证工作能顺利开展. 本文介绍的几个命令,不仅仅适用于服务器监控, ...

  10. 轻松解决Github连接缓慢、图裂问题

    1 简介 gayhub(误)github作为全世界最大的开源代码库以及版本控制系统,是用来托管项目以及学习开源技术非常好的平台,是我心中最好的学习网站,我们公众号的众多技术文章对应的数据和代码也都一直 ...