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的思想,相当于我们可以不断找到更新 ...
随机推荐
- Username for 'https://github.com': remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/GLSmile/pythontest.git/' 问题
使用$ git push -u origin master 进行同步时,提示输入用户名和密码,但是我输入正确的信息后,仍然 会报Username for 'https://github.com': r ...
- Java实现 LeetCode 790 多米诺和托米诺平铺(递推)
790. 多米诺和托米诺平铺 有两种形状的瓷砖:一种是 2x1 的多米诺形,另一种是形如 "L" 的托米诺形.两种形状都可以旋转. XX <- 多米诺 XX <- &q ...
- Java实现 LeetCode 733 图像渲染(DFS)
733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...
- Java实现 LeetCode 306 累加数
306. 累加数 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和. 给定一个只包含数字 ...
- Java中抽象类与接口的详细说明
首先简单的介绍一下抽象类: 定义是很简单的,我们这里不写官方的语言,我自己看着都烦,我们就用白话介绍,抽象类本质是一个类,没问题,那么类里面一般都是有方法的,方法包括方法名和方法体,这是常识对不对,那 ...
- java中Timer类的详细介绍(详解)
一.概念 定时计划任务功能在Java中主要使用的就是Timer对象,它在内部使用多线程的方式进行处理,所以它和多线程技术还是有非常大的关联的.在JDK中Timer类主要负责计划任务的功能,也就是在指定 ...
- Java实现第九届蓝桥杯乘积为零
乘积为零 如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零? 5650 4542 3554 473 946 4114 3871 9073 90 4329 2758 7949 61 ...
- python3 主机实时监控系统
主机实时监控系统(可在局域网访问) 一.思路: 前端: 1.管理员登录(编写一个管理员登录界面) 技术:html+css 2.资源数据显示(用于显示主机资源数据情况) 插件:echarts+jquer ...
- 诸葛亮vs司马懿,排序算法大战谁能笑到最后?
阵前对峙 公元234年,蜀汉丞相诸葛孔明再次北伐. 一日,与司马仲达所率魏军两军相峙,二人阵前舌战. 司马曰:"诸葛村夫,吾与汝相斗数年,斗兵斗阵斗谋略,均已疲乏.今日,何不一改陈规,斗点新 ...
- 心有 netty 一点通!
一.标准的netty线程模型 双池合璧: 1.连接线程池: 连接线程池专门负责监听客户端连接请求,并完成连接的建立(包括诸如握手.安全认证等过程). 连接的建立本身是一个极其复杂.损耗性能的过程,此处 ...