[USACO06NOV] Roadblocks
https://www.luogu.org/problem/show?pid=2865
题目描述
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).
贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友。贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路。 贝茜所在的乡村有R(1<=R<=100,000)条双向道路,每条路都联结了所有的N(1<=N<=5000)个农场中的某两个。贝茜居住在农场1,她的朋友们居住在农场N(即贝茜每次旅行的目的地)。 贝茜选择的第二短的路径中,可以包含任何一条在最短路中出现的道路,并且,一条路可以重复走多次。当然咯,第二短路的长度必须严格大于最短路(可能有多条)的长度,但它的长度必须不大于所有除最短路外的路径的长度。
输入输出格式
输入格式:
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)
输出格式:
Line 1: The length of the second shortest path between node 1 and node N
输入输出样例
4 4
1 2 100
2 4 200
2 3 250
3 4 100
450
说明
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
边可以重复走
严格次短路
A* 算法求第二短
#include<queue>
#include<cstdio>
#include<cstring>
#define N 5001
#define M 200001
using namespace std;
int n,s,t;
int dis1[N];
bool vis[N];
int front[N],to[M],nxt[M],val[M],tot;
struct node
{
int num,dis;
bool operator < (node p) const
{
return dis+dis1[num]>p.dis+dis1[p.num];
}
}now,nt;
void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w;
}
void init()
{
int m,u,v,w;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
}
void spfa()
{
memset(dis1,,sizeof(dis1));
queue<int>q;
dis1[n]=;
vis[n]=true;
q.push(n);
int now;
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=false;
for(int i=front[now];i;i=nxt[i])
if(dis1[to[i]]>dis1[now]+val[i])
{
dis1[to[i]]=dis1[now]+val[i];
if(!vis[to[i]])
{
q.push(to[i]);
vis[to[i]]=true;
}
}
}
}
void Astar()
{
if(dis1[]>1e9)
{
printf("-1");
return;
}
int cnt=,last=-;
priority_queue<node>q;
now.num=;
now.dis=;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(now.num==n)
{
cnt++;
if(cnt> && now.dis!=last)
{
printf("%d",now.dis);
return;
}
else last=now.dis;
}
for(int i=front[now.num];i;i=nxt[i])
{
nt.num=to[i];
nt.dis=now.dis+val[i];
q.push(nt);
}
}
printf("-1");
}
int main()
{
init();
spfa();
Astar();
}
[USACO06NOV] Roadblocks的更多相关文章
- 洛谷P2865 [USACO06NOV]Roadblocks G(次短路)
一个次短路的问题,可以套用dijkstra求最短路的方法,用dis[0][i]表示最短路:dis[1][i]表示次短路,优先队列中存有最短路和次短路,然后每次找到一条道路对他进行判断,更新最短或次短路 ...
- P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...
- 洛谷——P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- 络谷 P2865 [USACO06NOV]路障Roadblocks
P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...
- P2865 【[USACO06NOV]路障Roadblocks】(次短路)
传送门 算法Dijkstra要求次短路 那么在不考虑重复走一条边的情况下 肯定是把最短路中的一段改成另一段 至少要换另一条边到路径里所以可以枚举所有不属于最短路的每条边(a,b) 那么dis(1,a) ...
- 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)
题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...
- 洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】
链接:https://www.luogu.org/problemnew/show/P2865 题目描述 Bessie has moved to a small farm and sometimes e ...
- LG2865 [USACO06NOV]路障Roadblocks
题意 Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. ...
- luogu2865 [USACO06NOV]路障Roadblocks 次短路
注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...
随机推荐
- NFC学习总结二
移动支付这事情热了总归还是会回归理性,就如同之前的10几年间的几次轮回一样.字面上看,移动支付比支付大也不大可能,有相同,有扩展,有交集有不通才是. NFC这事情也是说了快十年了,真心希望它能回归到其 ...
- c# windows service 程序
service服务程序:可以长时间运行可执行应用程序.没有用户界面.可以自动启动和手动启动.适用于在服务器上或需要干扰其他工作的用户可以在同一台计算机上长时间的运行此功能. C#创建service服务 ...
- Python实用技巧
1.改变工作目录 import os os.chdir('C:/Users/Mr.Zhao') 2.搜索制定目录下的文件 1 import glob 2 glob.glob('C:/User/Mr.Z ...
- JavaScript初探系列之日期对象
时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.它是一个内置对象——而不是其它对象的属性,允许用户执行各种使用日期和时间的过程. 一 Date 日期对象 ...
- MVC4 DropDownList (二) — 省市联动
1.添加省份和城市类 //省份 public class Province { public int Id { get; set; } public string Name { get; set; } ...
- Swagger字段说明
常用字段说明 字段 说明 schemes 使用协议(如:http.https) host 项目地址,这个地址会作为每个接口的url base,拼接起来一起作为防伪地址 consumes 接口默认接收的 ...
- Axure RP 的安装与卸载
官网:http://www.axure.com/download 支持Windows和Mac
- RT-thread main函数分析
RT-thread系统的main函数位于startup.c文件中. /** * This function will startup RT-Thread RTOS. */ void rtthread_ ...
- RT-thread内核之小内存管理算法
一.动态内存管理 动态内存管理是一个真实的堆(Heap)内存管理模块,可以在当前资源满足的情况下,根据用户的需求分配任意大小的内存块.而当用户不需要再使用这些内存块时,又可以释放回堆中供其他应用分配 ...
- 2011 Multi-University Training Contest 8 - Host by HUST
Rank:56/147. 开场看B,是个线段树区间合并,花了2hour敲完代码...再花了30min查错..发现push_down有问题.改了就AC了. 然后发现A过了很多人.推了个公式,发现是个分段 ...