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
1 #include <map>
2 #include <set>
3 #include <list>
4 #include <stack>
5 #include <queue>
6 #include <deque>
7 #include <cmath>
8 #include <ctime>
9 #include <string>
10 #include <limits>
11 #include <cstdio>
12 #include <vector>
13 #include <iomanip>
14 #include <cstdlib>
15 #include <cstring>
16 #include <istream>
17 #include <iostream>
18 #include <algorithm>
19 #define ci cin
20 #define co cout
21 #define el endl
22 #define Scc(c) scanf("%c",&c)
23 #define Scs(s) scanf("%s",s)
24 #define Sci(x) scanf("%d",&x)
25 #define Sci2(x, y) scanf("%d%d",&x,&y)
26 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
27 #define Scl(x) scanf("%I64d",&x)
28 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
29 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
30 #define Pri(x) printf("%d\n",x)
31 #define Prl(x) printf("%I64d\n",x)
32 #define Prc(c) printf("%c\n",c)
33 #define Prs(s) printf("%s\n",s)
34 #define For(i,x,y) for(int i=x;i<y;i++)
35 #define For_(i,x,y) for(int i=x;i<=y;i++)
36 #define FFor(i,x,y) for(int i=x;i>y;i--)
37 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
38 #define Mem(f, x) memset(f,x,sizeof(f))
39 #define LL long long
40 #define ULL unsigned long long
41 #define MAXSIZE 100005
42 #define INF 0x3f3f3f3f
43
44 const int mod=1e9+7;
45 const double PI = acos(-1.0);
46
47
48 using namespace std;
49
50 typedef pair<int,int>pii;
51 struct edge
52 {
53 int to,w;
54 edge(int x,int y)
55 {
56 to=x;
57 w=y;
58 }
59 };
60 vector<edge>G[MAXSIZE];//邻接表储存
61 int dis[MAXSIZE];//最短
62 int dis2[MAXSIZE];//次短
63
64 int n,r;
65 void solve()
66 {
67 priority_queue<pii,vector<pii>,greater<pii> >q;//注意这个队列first存的是到每点的最短距离,second存的这个点
68 Mem(dis,INF);
69 Mem(dis2,INF);
70 q.push(pii(0,1));
71 dis[1]=0;
72 while(!q.empty())
73 {
74 pii p=q.top();
75 q.pop();
76 int v=p.second,d=p.first;
77 if(dis2[v]<d)
78 continue;//到v的距离比次短路短(肯定也比最短路短),终止本次循环
79 for(int i=0; i<G[v].size(); i++)
80 {
81 edge &e=G[v][i];
82 int d2=e.w+d;
83 if(dis[e.to]>dis[v]+e.w)//更新最短路径if(dis[e.to]>d2)
84 {
85 swap(dis[e.to],d2);
86 q.push(pii(dis[e.to],e.to));
87 }
88 if(dis2[e.to]>d2&&dis[e.to]<d2)//注意这个两个条件,后面那个条件可以不要
89 {
90 swap(dis2[e.to],d2);
91 q.push(pii(dis2[e.to],e.to));
92 }
93 }
94 }
95 Pri(dis2[n]);
96 }
97 int main()
98 {
99 Sci2(n,r);
100 For_(i,1,r)
101 {
102 int u,v,w;
103 Sci3(u,v,w);
104 G[u].push_back(edge(v,w));
105 G[v].push_back(edge(u,w));
106 }
107 solve();
108 return 0;
109 }
Roadblocks的更多相关文章
- poj 3255 Roadblocks
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 969 Solved: 468[S ...
- BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 768 Solved: 369[S ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )
从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...
- 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 835 Solved: 398[S ...
- poj3255 Roadblocks
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13594 Accepted: 4783 Descr ...
- P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
随机推荐
- 生成requirements.txt
requirements.txt文件 requirements.txt 文件是项目的依赖包及其对应版本号的信息列表,即记载你这个项目所安装的依赖. 作用:用来重新构建项目或者记录项目所需要的运行环境依 ...
- elasticsearch 聚合之 date_histogram 聚合
目录 1.背景 2.bucket_key如何计算 3.前置知识 4.日历和固定时间间隔 4.1 Calendar intervals 日历间隔 4.2 Fixed intervals 固定间隔 5.数 ...
- MySQL的select for update用法
MySQL中的select for update大家应该都有所接触,但什么时候该去使用,以及有哪些需要注意的地方会有很多不清楚的地方,我把我如何使用和查询到的文档在此记录. 作用 select本身是一 ...
- 【开源库推荐】#4 Poi-办公文档处理库
原文:[开源库推荐] #4 Poi-办公文档处理库 - Stars-One的杂货小窝 github仓库apache/poi Apache POI是Apache软件基金会的开放源码函式库,POI提供AP ...
- phpMyAdmin给非技术人员一个查阅数据库的窗口
背景 管理数据库的界面工具. 开发团队中一般有非技术背景人员,比如: 产品,功能测试人员: 对他们来说,可能安装数据库管理工具客户端都很麻烦,需要一款在线的网页工具能方便他们查阅数据. 本地docke ...
- VmWare安装Centos后配置Net网络SSH链接问题看这一遍就够了
1:首先安装VmWare 2:启动时在安装对应的Linux版本,网络就默认 net即可 3:都安装好了之后,注意有一个大坑,输入的账号密码都不能准确登录 最后发现是linux默认的输入法没有启用电脑键 ...
- VS2022,VS2019最新安裝方法
直接参照: https://www.bilibili.com/read/cv12364240/ 2022年5月30浩,亲测可用 1.下载notepad++ (必须) 2. 去微软官网下载 对应的安装 ...
- CONDITION EVALUATION DELTA热部署启动失效
1.问题描述 我在启动一个SpringBoot项目的时候,在启动中控制台不停的打印日志(如下图所示) 2.产生原因 当时我是看了这篇文章后CONDITION EVALUATION DELTA_苦逼码农 ...
- Ventoy制作启动盘和使用VMware测试启动盘(论文版)
- 1 Ventoy 1.1 Ventoy是什么 Ventoy是可用于制作启动U盘的开源工具,在占用少量引导分区容量后,其他空间依旧可以正常当一般的U盘读写文件.它的最大特点是只要将iso.win. ...
- python之路38 SQL注入问题 索引触发器 事务 存储过程 函数 流程控制
SQL注入问题 怪像1:输对用户名就可以登录成功 怪像2:不需要对的用户名和密码也可以登录成功 SQL注入:利用特殊符号的组合产生特殊的含义 从而避开正常的业务逻辑 select * from use ...