POJ-3255-Roadblocks(次短路的另一种求法)
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
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
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std; struct node
{
int pos,w;
node(int x,int y)
{
pos=x;
w=y;
}
bool friend operator<(node x,node y )
{
return x.w>y.w;
}
};
struct edge
{
int u,v;
ll cost;
int nxt;
}Edge[maxn<<];
int cnt; ll dis[],dis2[];
int head[],vis[];
void Add(int u,int v,ll w)
{
Edge[cnt].u=u;
Edge[cnt].v=v;
Edge[cnt].cost=w;
Edge[cnt].nxt=head[u];
head[u]=cnt++;
}
void Dijkstra(int u)
{
dis[u]=;
priority_queue<node>q;
q.push(node(u,));
while(!q.empty())
{
node now=q.top();
q.pop();
if(vis[now.pos])
{
continue;
}
vis[now.pos]=;
for(int t=head[now.pos];t!=-;t=Edge[t].nxt)
{
if(dis[now.pos]+Edge[t].cost<dis[Edge[t].v])
{
dis[Edge[t].v]=dis[now.pos]+Edge[t].cost;
q.push(node(Edge[t].v,dis[Edge[t].v]));
}
}
}
return ;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(dis,0x3f3f3f3f,sizeof(dis));
cnt=;
int u,v;
ll w;
for(int t=;t<m;t++)
{
scanf("%d%d%lld",&u,&v,&w);
Add(u,v,w);
Add(v,u,w);
}
Dijkstra();
for(int t=;t<=n;t++)
{
dis2[t]=dis[t];
}
memset(dis,0x3f3f3f3f,sizeof(dis));
memset(vis,,sizeof(vis));
Dijkstra(n);
int ans=0x3f3f3f3f;
for(int t=;t<cnt;t++)
{
if(dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost<ans&&(dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost)!=dis2[n])
{
ans=dis[Edge[t].u]+dis2[Edge[t].v]+Edge[t].cost;
}
}
printf("%d\n",ans);
system("pause");
return ;
}
POJ-3255-Roadblocks(次短路的另一种求法)的更多相关文章
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 3255 Roadblocks (次短路 SPFA )
题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- poj - 3225 Roadblocks(次短路)
http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...
- 次最短路径 POJ 3255 Roadblocks
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
- poj 3255 Roadblocks
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...
- POJ 3255 Roadblocks --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
随机推荐
- Java项目中经常遇到的一些异常情况
一. 1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存 ...
- CentOS7安装Redis的两种方式
1. 源码安装方式(不推荐): https://www.cnblogs.com/zuidongfeng/p/8032505.html https://www.cnblogs.com/zerotomax ...
- 从零搭建Spring Boot脚手架(4):手写Mybatis通用Mapper
1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文把国内最流行的ORM框架Mybatis也集成了进去.但是很多时候我们希望有一些开箱即用的通用Mapper来简化我们的开发.我 ...
- springboot多环境配置文件
一.关于springboot的配置文件 springboot的配置文件主要有两种:properties文件和yml文件,我们只要选择一种使用就可以了.我们通过properties文件介绍一下配置的方式 ...
- [leetcode/lintcode 题解] 有效回文 II · Valid Palindrome II
[题目描述] 给一个非空字符串 s,你最多可以删除一个字符.判断是否可以把它变成回文串. 在线评测地址: https://www.lintcode.com/problem/valid-palindro ...
- 2020-07-13:es是去查id再根据id去查数据库这种方式好,还是所有数据都放es,直接去查es好?
福哥答案2020-07-13: 有人觉得第一种方法好,也有人觉得第二种方法好.如果搜索字段远小于显示字段,比如搜索字段为3个,显示字段有20个,这个时候用第一种方法好.es+hbase,一般这样搭配. ...
- C#LeetCode刷题之#344-反转字符串(Reverse String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...
- 基于Linux系统geth的安装
转载地址 https://blog.csdn.net/qq_36124194/article/details/83658580 基于Linux系统geth的安装 安装ethereum sudo apt ...
- java线程的五大状态,阻塞状态详解
一.状态简介 一个线程的生命周期里有五大状态,分别是: 新生 就绪 运行 死亡 运行后可能遇到的阻塞状态 二.相关方法 2.1 新生状态 Thread t = new Thread(); 正如我们前面 ...
- 微信小程序内置组件web-view的缓存问题探讨
前言:博客或者论坛上面,还有自习亲身经历,发现微信小程序的webview组件的页面缓存问题相当严重,对开发H5的小童鞋来说应该困扰了不少.很多小童鞋硬是抓破脑袋也没有办法解决这个问题,那我们今天就来探 ...