poj3255 Roadblocks
Roadblocks
Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path. The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N. The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path). Input Line 1: Two space-separated integers: N and R
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000) Output Line 1: The length of the second shortest path between node 1 and node N
Sample Input 4 4 Sample Output 450 Hint Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
Source |
[Submit] [Go Back] [Status] [Discuss]
把求最短路的算法稍微改一下,每次记录节点的最短路和次短路。
在求最短路的时候会一次次地更新到那个结点的距离,在更新到真正的最短的距离时候,他的上一个就是次短距离,每次把那个值记录下来就行了。
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>
#define for(i,x,n) for(int i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007 using namespace std; int MAX_V,MAX_E;
struct edge{int to,cost;};
typedef pair<int,int> P;//最短距离,编号
vector<edge> G[];
int d[];//最短距离
int d2[];//次短距离 void dijkstra(int s){
priority_queue<P,vector<P>,greater<P> > que;
fill(d,d+MAX_V,INF);
fill(d2,d2+MAX_V,INF);
d[s]=;
que.push(P(d[s],s));
while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second;
for(i,,G[v].size()){
int to=G[v][i].to;
int dd=p.first+G[v][i].cost;
if(dd<d[to]){
d[to]^=dd;
dd^=d[to];
d[to]^=dd;
que.push(P(d[to],to));
}
if(dd<d2[to] && dd>d[to]){
d2[to]=dd;
que.push(P(d2[to],to));
}
}
}
} int main()
{
int x,y,z;
scanf("%d %d",&MAX_V,&MAX_E);
for(i,,MAX_E){
scanf("%d %d %d",&x,&y,&z);
x-=;
y-=;
edge ee;
ee.to=y; ee.cost=z;
G[x].push_back(ee);
ee.to=x; ee.cost=z;
G[y].push_back(ee);
}
dijkstra();
printf("%d",d2[MAX_V-]);
return ;
}
poj3255 Roadblocks的更多相关文章
- POJ3255 Roadblocks [Dijkstra,次短路]
题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...
- poj3255 Roadblocks 次短路
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10098 Accepted: 3620 Descr ...
- POJ3255 Roadblocks 【次短路】
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760 Accepted: 2848 Descri ...
- POJ3255(Roadblocks)--次短路径
点这里看题目 3228K 485MS G++ 2453B 根据题意和测试用例知道这是一个求次短路径的题目.次短路径,就是比最短路径长那么一丢丢的路径,而题中又是要求从一点到指定点的次短路径,果断Dij ...
- POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- cogs 315. [POJ3255] 地砖RoadBlocks
315. [POJ3255] 地砖RoadBlocks ★★★ 输入文件:block.in 输出文件:block.out 简单对比时间限制:1 s 内存限制:128 MB Descri ...
- 【POJ3255/洛谷2865】[Usaco2006 Nov]路障Roadblocks(次短路)
题目: POJ3255 洛谷2865 分析: 这道题第一眼看上去有点懵-- 不过既然要求次短路,那估计跟最短路有点关系,所以就拿着优先队列优化的Dijkstra乱搞,搞着搞着就通了. 开两个数组:\( ...
- POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks
http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15680 ...
随机推荐
- Introducing stapbpf – SystemTap’s new BPF backend
https://developers.redhat.com/blog/2017/12/13/introducing-stapbpf-systemtaps-new-bpf-backend/
- 使用linux kernel代码编译perf工具
环境:Qemu + ARMv8 perf是一款综合性分析工具,大到系统全局性性能,再小到进程线程级别,甚至到函数及汇编级别. 在内核源码目录下执行编译脚本: #!/bin/bash cross_com ...
- Gradle初识
一.安装配置 gradle官方网站https://gradle.org/,下载下来是一个压缩包,解压到合适的目录即可,然后配置环境变量(GRADLE_HOME,Path),略去. 二.IDEA配置 N ...
- git stash命令详解
git stash命令用于将更改储藏在脏工作目录中. 使用语法 git stash list [<options>] git stash show [<stash>] git ...
- 微信小程序登录逻辑
wx.getStorage({ key: 'session_id', success: function(res) { //如果本地缓存中有session_id,则说明用户登陆过 console.lo ...
- Docker 集群Swarm创建和Swarm Web管理
关于Docker Swarm更多的介绍请查看<Docker管理工具-Swarm部署记录> 一.环境配置 1.安装环境 # cat /etc/redhat-release CentOS Li ...
- CAS 单点登录【2】自定义用户验证
基础不太熟的同学可以先去看:CAS 单点登录[1]入门 方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验证用户的处理器. 1.QueryDatabaseAuthe ...
- 安装了 R2 Integration Servic 之后,SQL Server 2008 Management Studio报错
问题产生 IM数据库服务器未安装Integration Servic,影响备份.在安装了安装了 SQL Server 2008 R2 Integration Servic 之后,SQL Server ...
- xsd文件规则和语法
1.简介 XSD即XML结构定义, XML Schemas Definition.其本身就是用xml描述的, 且遵循xml语法规则.一份XML schema文件描述了XML文档的结构. 基本规则: ...
- 【html+css3】在一张jpg图片上,显示多张透明的png图片
1.需求:在一个div布局里面放置整张jpg图片,然后在jpg图片上显示三张水平展示的透明png图片,且png外层用a标签包含菜单 2.效果图: 3.上图,底层使用蓝色jpg图片,[首页].[购物车] ...