Sightseeing
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10004   Accepted: 3523

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 AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ 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 ≤ SF ≤ 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

这题我主要用来学习如何写dijkstar  我习惯写spfa但是容易被卡
这题主要是改变松弛
更新最短路径的适合顺便更新次短路径
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 2e9 + ;
int t, n, m, tot;
int head[], d[][], cnt[][], vis[][];
struct node {
int v, w, next;
} edge[maxn];
void init() {
tot = ;
memset(head, -, sizeof(head));
}
void add(int u, int v, int w) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
struct node1 {
int v, p, d;
node1(int v, int d, int p): v(v), d(d), p(p) {}
bool operator <(const node1 & a ) const {
return d>a.d;
}
};
void dijkstar(int st) {
memset(vis, , sizeof(vis));
memset(cnt, , sizeof(cnt));
for (int i = ; i <= n ; i++) d[i][] = d[i][] = INF;
priority_queue<node1>q;
d[st][] = , cnt[st][] = ;
q.push(node1(st, , ));
while(!q.empty()) {
node1 t = q.top();
q.pop();
int u = t.v, p = t.p;
if (vis[u][p]) continue;
vis[u][p] = ;
for (int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].v, w = edge[i].w;
if (d[v][] > d[u][p] + w) {
d[v][] = d[v][];
cnt[v][] = cnt[v][];
d[v][] = d[u][p] + w;
cnt[v][] = cnt[u][p];
q.push(node1(v, d[v][], ));
q.push(node1(v, d[v][], ));
} else if (d[v][] == d[u][p] + w) cnt[v][] += cnt[u][p];
else if (d[v][] > d[u][p] + w) {
d[v][] = d[u][p] + w;
cnt[v][] = cnt[u][p];
q.push(node1(v,d[v][],));
} else if (d[v][] == d[u][p] + w) cnt[v][] += cnt[u][p];
}
}
}
int main() {
scanf("%d", &t);
while(t--) {
init();
scanf("%d%d", &n, &m);
for (int i = ; i < m ; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
int st, ed;
scanf("%d%d", &st, &ed);
dijkstar(st);
if (d[ed][] + == d[ed][]) printf("%d\n", cnt[ed][] + cnt[ed][]);
else printf("%d\n", cnt[ed][]);
}
return ;
}

最新文章

  1. VS2013菜单栏文字全大写的问题
  2. Permutation
  3. 4,SFDC 管理员篇 - 数据模型 - 基本对象
  4. check member function
  5. Pattern Lab - 构建先进的原子设计系统
  6. 解决oralce 11g dg搭建报错:ORA-16664、ORA-16714、ORA-16810问题--转
  7. CListView虚拟列表
  8. c++11信号量实现
  9. C++实现RTMP协议发送H.264编码及AAC编码的音视频(转)
  10. C-最长回文子串(1)
  11. css 初始化
  12. servlet笔记
  13. Nginx+uWSGI+Django环境配置
  14. 20155205 郝博雅 Exp 8 Web基础
  15. C# Task用法
  16. 第二十节,使用RNN网络拟合回声信号序列
  17. 洛谷P1809 过河问题_NOI导刊2011提高(01)
  18. 深入浅出的webpack构建工具---webpack基本配置(一)
  19. 【Java】【3】BeanUtils.copyProperties();将一个实体类的值复制到另外一个实体类
  20. Js代码一些要素

热门文章

  1. Intro to Probabilistic Model
  2. leetcode个人题解——#20 Valid Parentheses
  3. STM32串口通信UART使用
  4. 普通Java类获取Spring的Bean的方法
  5. POJ 3693 Maximum repetition substring(后缀数组)
  6. ava中普通代码块,构造代码块,静态代码块区别及示例
  7. iOS- &lt;项目笔记&gt; UIApplication常见属性与方法总结
  8. python学习第一天-语法学习
  9. [Leetcode] 2.Add Two Numbers(List To Long,模拟)
  10. window.navigator.standalone 检测iOS WebApp是否运行在全屏模式