POJ 3463 Sightseeing 【最短路与次短路】
题目
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 【最短路与次短路】的更多相关文章
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ - 3463 Sightseeing 最短路计数+次短路计数
F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...
- poj 3463 Sightseeing——次短路计数
题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...
- poj 3463 Sightseeing(次短路+条数统计)
/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...
- 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 (次短路经数)
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions:10005 Accepted: 3523 Descr ...
- POJ 3463 Sightseeing
最短路+次短路(Dijkstra+priority_queue) 题意是要求你找出最短路的条数+与最短路仅仅差1的次短路的条数. 開始仅仅会算最短路的条数,和次短路的长度.真是给次短路条数跪了.ORZ ...
- POJ 3463 有向图求次短路的长度及其方法数
题目大意: 希望求出走出最短路的方法总数,如果次短路只比最短路小1,那也是可取的 输出总的方法数 这里n个点,每个点有最短和次短两种长度 这里采取的是dijkstra的思想,相当于我们可以不断找到更新 ...
随机推荐
- Java实现 LeetCode 733 图像渲染(DFS)
733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...
- 用js实现简单的抛物线运动
前言 老早就看过一些购物车的抛物线效果,也想自己凑热闹动手来实现一遍. 然后(lll¬ω¬) 书到用时方恨少,发现高中学到物理啊.数学啊,都忘光了,抛物线公式都忘了0 0. 顺手百度一波,从百度可知: ...
- SQL手工注入绕过过滤
1.考虑闭合:单引号 --> %27 空格-->%20 井号--> %23 : 构造闭合函数 %27teacher%23 2.判断过滤内容:union --> uniu ...
- STM32串口打印的那些知识
常规打印方法 在STM32的应用中,我们常常对printf进行重定向的方式来把打印信息printf到我们的串口助手.在MDK环境中,我们常常使用MicroLIB+fputc的方式实现串口打印功能,即: ...
- Elasticsearch 别管原理,先run起来
少点代码,多点头发 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 看文章有两点需要注意: 本公 ...
- LeetCode 76,一题教会你面试算法时的思考套路
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第45篇文章,我们一起来看看LeetCode的76题,最小窗口子串Minimum Window Substrin ...
- ES 复合查询
ES在查询过程中比较多遇到符合查询,既需要多个字段过滤也需要特殊情况处理,本文简单介绍几种查询组合方便快捷查询ES. bool布尔查询有一个或者多个布尔子句组成 filter 只过滤符合条件的 ...
- 面试官:说说Redis的Hash底层 我:......(来自阅文的面试题)
redis源码分析系列文章 [Redis源码系列]在Liunx安装和常见API 为什么要从Redis源码分析 String底层实现——动态字符串SDS Redis的双向链表一文全知道 前言 hello ...
- MyBatis使用模糊查询用户信息及log4j配置文件详解
1.1 根据用户名称模糊查询用户信息 根据用户名模糊查询用户信息,只需要我们更改映射文件中的sql语句.其他的内容跟上一篇的内容是一样的 1.2添加根据用户名称模糊查询用户信息的sql语句 实例中是查 ...
- @uoj - 164@ 【清华集训2015】V
目录 @description@ @solution@ @accepted code@ @details@ @description@ Picks博士观察完金星凌日后,设计了一个复杂的电阻器.为了简化 ...