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 ...
随机推荐
- 小程序页面的四种文件(JSON、WXML、WXSS、JS)加载顺序
一个小程序页面由四种文件组成: 1)json 页面配置文件 2)js 页面逻辑文件(必需) 3)wxml 页面结构文件(必需) 4)wxss 页面样式文件 这四个文件的加载顺序: 第一步: 加载页面j ...
- 【WXS数据类型】Boolean
属性: 名称 值类型 说明 [Boolean].constructor [String] 返回值为“Boolean”,表示类型的结构字符串 方法: 原型:[Boolean].toString() 说明 ...
- Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】
Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-24 | Spring Cloud Confi ...
- [Clr via C#读书笔记]Cp14字符字符串和文本处理
Cp14字符字符串和文本处理 字符 System.Char结构,2个字节的Unicode,提供了大量的静态方法:可以直接强制转换成数值: 字符串 使用最频繁的类型:不可变:引用类型,在堆上分配,但是使 ...
- 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道——最小分割分数
题目: 给 n 个正整数 a_1,…,a_n, 将 n 个数顺序排成一列后分割成 m 段,每一段的分数被记为这段内所有数的和,该次分割的分数被记为 m 段分数的最大值.问所有分割方案中分割分数的最小值 ...
- maven 教程二 深入
一:编写POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- 1.编译azkaban
1.下载azkaban的源码 https://github.com/azkaban/azkaban.git 然后解压得到azkaban-master.zip,解压:unzip azkaban-mast ...
- HDU 1250 Hat's Fibonacci(高精度)
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- PCB各层介绍及AD软件画PCB时的规则
好久没画过板了,最近因为工作关系,硬件软件全部得自己来,不得不重新打开闲置很久的AltiumDesigner.以前做过点乱七八糟的笔记,本来想回头翻看一下,结果哪儿也找不到,估计已经被不小心删掉了. ...
- Python中from module import *语法
from module import *的语法在Python 3.X和Python 2.X中的使用稍有区别: 在Python 3.X中,from module import *无法在函数里面使用,而在 ...