Sightseeing(dijlstar) 计算最短路和次短路的条数
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 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 这题我主要用来学习如何写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 ;
}
Sightseeing(dijlstar) 计算最短路和次短路的条数的更多相关文章
- 最短路和次短路的条数(dijstra算法或spfa算法)POJ3463
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- hdu1688(dijkstra求最短路和次短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题意:第k短路,这里要求的是第1短路(即最短路),第2短路(即次短路),以及路径条数,最后如果最 ...
- HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ---3463 Sightseeing 记录最短路和次短路的条数
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9247 Accepted: 3242 Descr ...
- POJ 3463 Sightseeing 【最短路与次短路】
题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...
- POJ - 3463 Sightseeing 最短路计数+次短路计数
F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...
- poj 3463 Sightseeing(次短路+条数统计)
/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...
- HDU 1688 Sightseeing 【输出最短路+次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...
随机推荐
- Github协作图想
首先 git pull 从远程拉下代码,并在本地与本地代码自动合并 在本地解决冲突后,可将本地代码进行远程推送 版本库的Repository中存储的是版本树状链,每一根链接线代表每一次的修改,每一个节 ...
- StreamSets小白踩过的一些坑
由于公司业务上的需求,需要实时监控mysql数据库的数据的增长,并将数据同步到另一个平台,所以就问老大使用什么工具比较好,老大推荐使用StreamSets,还说在测试环境都已经部署好了StreamSe ...
- zabbix 2.2.2 安装部署
zabbix 2.2.2版本与1.8.3版本安装过程略有不同,下面为实施步骤: 服务端:172.16.1.61 客户端:172.16.1.8 搭建zbbix软件 安装LAMP环境及依赖包 [root@ ...
- Hybrid APP基础篇(五)->JSBridge实现示例
说明 JSBridge实现示例 目录 前言 参考来源 楔子 JS实现部分 说明 实现 Android实现部分 说明 JSBridge类 实现 Callback类 实现 Webview容器关键代码 实现 ...
- "Hello world!"团队第八次会议
Scrum会议 今天是我们"Hello world!"团队第八次召开会议,博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 ...
- Linux 路由 学习笔记 之一 相关的数据结构
http://blog.csdn.net/lickylin/article/details/38326719 从现在开始学习路由相关的代码,在分析代码之前, 我们还是先分析数据结构,把数据结构之间的关 ...
- springMVC视图有哪些?-009
html,json,pdf等. springMVC 使用ViewResolver来根据controller中返回的view名关联到具体的view对象. 使用view对象渲染返回值以生成最终的视图,比如 ...
- 如何高效的使用Google
文章再转自知乎:http://www.zhihu.com/question/20161362
- Java线程模型
并发不一定要依赖多线程(如PHP中很常见的多进程并发),但是在Java里面谈论并发,大多数都与线程脱不开关系. 线程是比进程更轻量级的调度执行单位,线程的引入,可以把一个进程的资源分配和执行调度分开, ...
- 使用dom4j修改XML格式的字符串
XML格式 <data> <ryzd> <record> <western> <record> <diagnoses> < ...