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 ...
随机推荐
- pygame-KidsCanCode系列jumpy-part3-重力及碰撞检测
这个游戏叫jumpy,大致玩法就是模拟超级玛丽一样,可以不停在各个档板上跳动,同时受到重力的作用,会向下掉,如果落下时,没有站在档板上,就挂了. 这节,我们加入重力因素,继续改造sprites.py ...
- linux > 和 >> 、< 区别
linux中经常会用到将内容输出到某文件当中,只需要在执行命令后面加上>或者>>号即可进入操作. 大于号:将一条命令执行结果(标准输出,或者错误输出,本来都要打印到屏幕上面的)重定向 ...
- Android的Databinding-资源绑定
databinding还能对布局的资源文件进行绑定. <data class="ResourceBinding"> <variable name="la ...
- 怎样在 Ubuntu 16.04 强制 APT 包管理器使用 IPv4 | Linux 中国
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/F8qG7f9YD02Pe/article/details/82879401 https://mmbi ...
- t-io 集群解决方案以及源码解析
t-io 集群解决方案以及源码解析 0x01 概要说明 本博客是基于老谭t-io showcase中的tio-websocket-showcase 示例来实现集群.看showcase 入门还是挺容易的 ...
- [Python设计模式] 第18章 游戏角色备份——备忘录模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 用代码模拟以下场景,一个游戏角色有生命力,攻击力,防御力等数据,在打Bos ...
- 跟着柴毛毛学Spring(3)——简化Bean的配置
通过前面的学习.我们会感觉到对于一个有较多Bean的大项目,Spring的配置会比較复杂. 那么接下来我们就介绍怎样简化Spring的配置. 简化Spring的配置主要分为两类: 1. 自己主动装配 ...
- git merge dryrun
As noted previously, pass in the --no-commit flag, but to avoid a fast-forward commit, also pass in ...
- 每天一个linux命令(6):cp
1.命令简介 cp(Copy file):将源文件复制至目标文件,或将多个源文件复制至目标目录. 2.用法 cp [选项]... [-T] 源文件 目标文件 或:cp [选项]... 源文件... 目 ...
- k8s应用首页临时改成升级维护页面
在本地虚拟机 产生一个nginx配置文件 [root@centos-01 dockerfile]# cat weifeng_maintain.conf server { listen 443; ser ...