POJ---3463 Sightseeing 记录最短路和次短路的条数
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9247 | Accepted: 3242 |
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.
这题好像用spfa写起来特别的复杂 还是用dijkstra吧
以后还是多用dijkstar 他们都说这个快一些
spfa 以后判断环再写这个
题意:t组数据,每组输入点n和边m个数,
输入m条边,再输入起点ST和终点EN,
求从ST到EN最短路和比最短路长1的路的总条数。
这题改变松弛条件就行了
写起来繁琐一点
- if(x<最小)更新最短路和次短路
- if(x==最小)更新最短路数量
- if(x<次小)更新次短路
- if(x==次小)更新次短路数量
#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 n, m, t, u, v, w, f, s, 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;
tot++;
}
struct node1 {
int u, d, p;
node1(int u, int d, int p): u(u), d(d), p(p) {}
bool operator < (const node1 & a)const {
return d > a.d;
}
};
void dijkstra(int s) {
priority_queue<node1>q;
memset(vis, , sizeof(vis));
memset(cnt, , sizeof(cnt));
for (int i = ; i <= n ; i++) d[i][] = d[i][] = INF;
q.push(node1(s, , ));
d[s][] = , cnt[s][] = ;
while(!q.empty()) {
node1 now = q.top();
q.pop();
int u = now.u, p = now.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++) {
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
scanf("%d%d", &s, &f);
dijkstra(s);
if (d[f][] + == d[f][]) printf("%d\n", cnt[f][] + cnt[f][]);
else printf("%d\n", cnt[f][]);
}
return ;
}
POJ---3463 Sightseeing 记录最短路和次短路的条数的更多相关文章
- POJ - 3463 Sightseeing 最短路计数+次短路计数
F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- poj 3463 Sightseeing——次短路计数
题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...
- POJ 3463 Sightseeing (次短路经数)
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions:10005 Accepted: 3523 Descr ...
- POJ 3463 Sightseeing (次短路)
题意:求两点之间最短路的数目加上比最短路长度大1的路径数目 分析:可以转化为求最短路和次短路的问题,如果次短路比最短路大1,那么结果就是最短路数目加上次短路数目,否则就不加. 求解次短路的过程也是基于 ...
- POJ 3463 Sightseeing 【最短路与次短路】
题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...
- poj 3463 Sightseeing(次短路+条数统计)
/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...
- POJ 3463 Sightseeing 题解
题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...
- POJ 3463 Sightseeing
最短路+次短路(Dijkstra+priority_queue) 题意是要求你找出最短路的条数+与最短路仅仅差1的次短路的条数. 開始仅仅会算最短路的条数,和次短路的长度.真是给次短路条数跪了.ORZ ...
- Sightseeing(poj 3463)
题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...
随机推荐
- TW实习日记:第17天
今天又改了一堆bug,比如界面的显示bug,Html字符串的处理.优化了一些交互界面,处理了一个模块数据传输的问题.我发现这些bug真的有一半是粗心导致的,真的真的是,写代码一定要细心细心再细心,不然 ...
- 【springmvc+mybatis项目实战】杰信商贸-5.生产厂家DAO+SERVICE+CONTROLLER+JSP+配置文件
上一篇我们创建了工程和一个Factory的po对象(javaBean),我们也写好了Mapper的映射文件,接下来我们来完成生产厂家的DAO与SERVICE,以及CONTROLLER,还有做显示的JS ...
- 【转】unity3d 在UGUI中制作自适应调整大小的滚动布局控件
转自 http://blog.csdn.net/rcfalcon/article/details/43459387 在游戏中,我们很多地方需要用到scroll content的概念:我们需要一个容器, ...
- Spark- 根据IP获取城市(java)
开源 IP 地址定位库 ip2region 1.4 ip2region 是准确率 99.9% 的 IP 地址定位库,0.0x毫秒级查询,数据库文件大小只有 2.7M,提供了 Java.PHP.C.Py ...
- tensorflow模型持久化保存和加载--深度学习-神经网络
模型文件的保存 tensorflow将模型保持到本地会生成4个文件: meta文件:保存了网络的图结构,包含变量.op.集合等信息 ckpt文件: 二进制文件,保存了网络中所有权重.偏置等变量数值,分 ...
- docker容器学习笔记
docker是通过内核虚拟化技术来提供容器的资源隔离与安全保障. docker组成: docker client.docker server.docker组件(镜像(image).容器(contain ...
- 由一个hash字符串生成多个子hash字符串
通过存储一个head hash,然后把子hash放到网络中 当然,也可以像默克尔树那样的,生成多级的子hash ,可以通过规则配置不同的hash 生成方式.倒置的默克尔树 我有一个文件,然后我把她分隔 ...
- eos教程如何创建eos测试账号并且使用scatter插件
EOS代币租赁平台 --- Chintai平台已经在Jungle测试网络上部署了,欢迎大家来体验. 地址见: Chintai 公测版 官网是: Chintai 目前测试网络上面需要用到Scatter插 ...
- 20145214实验四 Android开发基础
20145214实验四 Android开发基础 实验内容及步骤 安装 JDK 并配置 JDK 环境变量 找到之前path变量中的jdk文件所在位置并复制. 用复制的变量名新建一个 JAVA_HOME ...
- matlab 直方图均衡化(含rgb)
步骤: 统计原图像素每个像素的个数 统计原图像<每个灰度级的像素的累积个数 家里灰度级得映射规则 将原图每个像素点的灰度映射到新图 代码: clear all I=imread('1.jpg') ...