1267. 路障(block.pas/c/cpp) 
(File IO): input:block.in output:block.out

Time Limits: 1000 ms  Memory Limits: 65536 KB  Detailed Limits  

Goto ProblemSet

Description

  Bessie 来到一个小农场,有时她想回老家看看她的一位好友。她不想太早地回到老家,因为她喜欢途中的美丽风景。她决定选择次短路径,而不是最短路径。
  农村有 R (1 <= R <= 100,000) 条双向的路,每条路连接 N (1 <= N <= 5000) 个结点中的两个。结点的编号是 1..N。Bessie 从结点 1出发,她的朋友(目的地)在结点 N。
  次短路径可以使用最短路径上的路,而且允许退回,即到达一个结点超过一次。次短路径是一种长度大于最短路径的路径(如果存在两条或多条最短路径存在,次短路径就是比它们长,且不比其他任何的路径长的路径)。
 

Input

  Line 1: 两个用空格分隔的整数 N 和 R
  Lines 2..R+1: 每行包含三个用空格分隔的整数: A, B, 和 D表示有一条路连接结点A和B,长度为D (1 <= D <= 5000)。

Output

  Line 1: 结点 1 到结点 N的次短路径长度。
 

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450
 
做法:其实次短路可以用spfa在更新最短路的同时更新一下次短路就好了,但我头铁打了A*
 
代码如下:

 #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdlib>
#define N 400007
using namespace std;
int n, m, tot, ans;
struct edge
{
int to, next, w;
}e[N];
int ls[N], f[N], list[N];
int times[N];
bool v[N];
struct A_node
{
int g, h, p;
bool operator < (A_node x) const
{
return x.g + x.h < g + h;
}
};
priority_queue<A_node>Q; void add(int x, int y, int z)
{
e[++tot].to = y;
e[tot].next = ls[x];
e[tot].w = z;
ls[x] = tot;
e[++tot].to = x;
e[tot].next = ls[y];
e[tot].w = z;
ls[y] = tot;
} void spfa()
{
for (int i = ; i <= n; i++)
f[i] = ;
f[n] = ;
int h = , t = ;
list[++t] = n;
v[n] = ;
while (h < t)
{
int p = list[++h];
for (int i = ls[p]; i; i = e[i].next)
if (f[p] + e[i].w < f[e[i].to])
{
f[e[i].to] = f[p] + e[i].w;
if (!v[e[i].to])
{
v[e[i].to] = ;
list[++t] = e[i].to;
}
}
v[p] = ;
}
} int A_star()
{
A_node t1, tmp;
t1.p = , t1.g = , t1.h = ;
Q.push(t1);
while (!Q.empty())
{
t1 = Q.top(); Q.pop();
times[t1.p]++;
if (times[t1.p] == && t1.p == n) return t1.h + t1.g;
if (times[t1.p] > ) continue;
for (int i = ls[t1.p]; i; i = e[i].next)
{
tmp.p = e[i].to;
tmp.g = f[e[i].to];
tmp.h = e[i].w + t1.h;
Q.push(tmp);
}
} } int main()
{
freopen("block.in", "r", stdin);
freopen("block.out", "w", stdout);
scanf("%d%d", &n, &m);
int x, y, z;
for (int i = ; i<= m; i++)
{
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
}
spfa();
ans = A_star();
printf("%d", ans);
}

JZOJ 1267. 路障的更多相关文章

  1. 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法

    彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...

  2. 洛谷 P3395 路障

    P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个方 ...

  3. (jzoj snow的追寻)线段树维护树的直径

    jzoj snow的追寻 DFS序上搞 合并暴力和,记录最长链和当前最远点,距离跑LCA # include <stdio.h> # include <stdlib.h> # ...

  4. 并发库应用之八 & 循环路障CyclicBarrier应用

    JDK包位置:java.util.concurrent.CyclicBarrier 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及 ...

  5. django插入数据库错误:mysql的1267错误

    错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...

  6. [jzoj]3506.【NOIP2013模拟11.4A组】善良的精灵(fairy)(深度优先生成树)

    Link https://jzoj.net/senior/#main/show/3506 Description 从前有一个善良的精灵. 一天,一个年轻人B找到她并请他预言他的未来.这个精灵透过他的水 ...

  7. [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)

    Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...

  8. [jzoj]5478.【NOIP2017提高组正式赛】列队

    Link https://jzoj.net/senior/#main/show/5478 Description Sylvia 是一个热爱学习的女孩子.       前段时间,Sylvia 参加了学校 ...

  9. [jzoj]1115.【HNOI2008】GT考试

    Link https://jzoj.net/senior/#main/show/1115 Description 申准备报名参加GT考试,准考证号为n位数X1X2X3...Xn-1Xn(0<=X ...

随机推荐

  1. Aura Component Skills & Tools

    本篇参考: https://trailhead.salesforce.com/content/learn/modules/lex_dev_lc_vf_fundamentals 不知不觉已经做了三年多的 ...

  2. 如何添加/移除CSS类

    在网页设计中,我们常常要使用Javascript来改变页面元素的样式.其中一种办法是改变页面元素的CSS类(Class),这在传统的Javascript里,我们通常是通过处理HTML Dom的clas ...

  3. OC 中 self 与 super 总结

    一段代码引发的思考: @implementation Son : Father - (id)init { self = [super init]; if (self) { NSLog(@"% ...

  4. WPF根据数据项获取条目控件的方法-ItemContainerGenerator

    一.方法: ContainerFromIndex:返回 ItemCollection 中指定索引处的项的容器. ContainerFromItem:返回与制定的项对应的容器(ComboxItem等条目 ...

  5. *.rpm is not signed解决

    1.# yum install qemu*报错如下:Package qemu-kvm-tools-0.12.1.2-2.113.el6.x86_64.rpm is not signed2.解决# vi ...

  6. 从零开始的全栈工程师——js篇2.15(offsetLeft)

    元素的属性 Div.attributes 是所有标签属性构成的数据集合 Div.classList 是所有class名构成的数组集合 在classList的原型链上看以看到add()和remove() ...

  7. 常用模块random,time,os,sys,序列化模块

    一丶random模块 取随机数的模块 #导入random模块 import random #取随机小数: r = random.random() #取大于零且小于一之间的小数 print(r) #0. ...

  8. [转]vim 快捷键整理

    Linux中vim编辑器的功能非常强大,许多常用快捷键用起来非常方便,这里将我学vim入门时学的一些常用的快捷键分享给大家一下,希望可以帮助你们.  原文地址:http://blog.csdn.net ...

  9. C# 获取文件夹下所有的文件

    static void getAllFileNameInDir(string path, ref List<string> files) { DirectoryInfo folder = ...

  10. isset或array_key_exists,检查数组键是否存在

    今天在导出报表的时候遇到了一个问题,undefined index:pid,然后就纳闷了,我的数组里面根本就没有pid,为什么会出现这个错误呢,我遍历了一下数组,发现果然有pid这个键,奇怪呀,我有做 ...