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 --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
随机推荐
- TestFlight无法访问怎么办?TF如何链接AppStoreConnect解决教程
最近苹果商店半天下架近3万款应用的新闻闹的沸沸扬扬,很多被下架应用的运营商转将APP上架了TestFlight内测商店(TF签名上架),但是从7月31日起很多用户就开始反映TestFlight内测应用 ...
- 电力规约DL/T 654 2007多功能电表通信协议 调试工具
DL/T 654 2007多功能电表通信协议 调试工具 最近调试DL/T654 2007电力规约,在网上找到一个比较好用的工具,分享给大家,希望对大家有帮助. CSDN需要积分,我传百度网盘了, 内含 ...
- GitHub/Git配置与简单的使用
今天我开始了初步的学习,首先从陌生的开始下手,GitHub,自己通过查询网络上的资料有了初步的理解与认识.进行了Git与GitHub的配置. 一.前期准备 首先下载Git,Git官网->http ...
- Docker 快速搭建 MySQL8 开发环境
使用 Docker 快速搭建一个 MySQL8 开发环境 步骤 获取镜像 docker pull mysql:8 启动容器,密码 123456,映射 3306 端口 docker run --name ...
- 从零搭建Spring Boot脚手架(4):手写Mybatis通用Mapper
1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文把国内最流行的ORM框架Mybatis也集成了进去.但是很多时候我们希望有一些开箱即用的通用Mapper来简化我们的开发.我 ...
- 类型SQL注入实验 Part1
准备为PHPstudy环境 <?php $id = $_GET['t']; $conn = mysql_connect("127.0.0.1","root" ...
- 如何解决java高并发详细讲解
对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并发问题是绝大部分的程序员头疼的问题, 但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们一起来研 ...
- leetcode刷题笔记-3. 无重复字符的最长子串(java实现)
题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "ab ...
- C#LeetCode刷题之#521-最长特殊序列 Ⅰ(Longest Uncommon Subsequence I)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3949 访问. 给定两个字符串,你需要从这两个字符串中找出最长的特 ...
- peer_manager_handler: Connection security failed: role: Peripheral, conn_handle: 0x0, procedure: Bonding, error: 133
调试nordic 52840 hids_keyboard 例程时,和手机配对,配对失败,提示:peer_manager_handler: Connection security failed: rol ...