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

输入输出样例

输入样例#1:

4 4
1 2 100
2 4 200
2 3 250
3 4 100
输出样例#1:

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的更多相关文章

  1. 洛谷P2865 [USACO06NOV]Roadblocks G(次短路)

    一个次短路的问题,可以套用dijkstra求最短路的方法,用dis[0][i]表示最短路:dis[1][i]表示次短路,优先队列中存有最短路和次短路,然后每次找到一条道路对他进行判断,更新最短或次短路 ...

  2. P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 最短路(次短路) 直接在dijkstra中维护2个数组:d1(最短路),d2(次短路),然后跑一遍就行了. attention:数据有不同 ...

  3. 洛谷——P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  4. 络谷 P2865 [USACO06NOV]路障Roadblocks

    P2865 [USACO06NOV]路障Roadblocks 题目描述 Bessie has moved to a small farm and sometimes enjoys returning ...

  5. P2865 【[USACO06NOV]路障Roadblocks】(次短路)

    传送门 算法Dijkstra要求次短路 那么在不考虑重复走一条边的情况下 肯定是把最短路中的一段改成另一段 至少要换另一条边到路径里所以可以枚举所有不属于最短路的每条边(a,b) 那么dis(1,a) ...

  6. 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)

    题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...

  7. 洛谷题解 P2865 【[USACO06NOV]路障Roadblocks】

    链接:https://www.luogu.org/problemnew/show/P2865 题目描述 Bessie has moved to a small farm and sometimes e ...

  8. LG2865 [USACO06NOV]路障Roadblocks

    题意 Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. ...

  9. luogu2865 [USACO06NOV]路障Roadblocks 次短路

    注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...

随机推荐

  1. Python 服务器端表单验证插件

    Python格式验证库 Cerberus 作者 MrStranger 关注 2016.08.02 14:44 字数 2140 阅读 79评论 0喜欢 1 Cerberus是一个验证Python对象.M ...

  2. <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<一>

    <Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yoursel ...

  3. C#中堆和栈的区别?

    http://www.jb51.net/article/55306.htm http://www.cnblogs.com/JimmyZhang/archive/2008/01/31/1059383.h ...

  4. <T extends Comparable<? super T>>

    在看Collections源代码中,看到如下代码: public static <T extends Comparable<? super T>> void sort(List ...

  5. Python替换字符串中的反斜杠\

    s = 'cdp\nd' result = eval(repr(s).replace('\\', '@')) print(result) repr() 函数可以将字符串转换为python的原始字符串( ...

  6. 【题解】CF#280 C-Game on Tree

    感觉对期望也一无所知……(:′⌒`)╮(╯﹏╰)╭ 一直在考虑怎么dp,最后看了题解——竟然是这样的???[震惊]但是看了题解之后,觉得确实很有道理…… 我们可以考虑最后答案的组成,可以分开计算不同的 ...

  7. [COGS2652]秘术「天文密葬法」

    description 题面 给个树,第\(i\)个点有两个权值\(a_i\)和\(b_i\),现在求一条长度为\(m\)的路径,使得\(\frac{\sum a_i}{\sum b_i}\)最小 d ...

  8. 【以前的空间】bzoj 1072 [SCOI2007]排列perm

    又颓废了一个下午,最近撸mc撸到丧失意识了,玩的有点恶心,于是找水题做,瞧不起颓废的自己啊. another水题. 这题题意很明显啦,就是找数字排列后组成的数去mod d=0后有多少种. 普通的搜索的 ...

  9. POJ2774:Long Long Message——题解

    http://poj.org/problem?id=2774 给定两个字符串 A 和 B,求最长公共子串. 论文题,把两个串合并起来,比较两个串各自的后缀的height值取最大即可. #include ...

  10. cf 460 E. Congruence Equation 数学题

    cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...