JZOJ 1267. 路障
1267. 路障(block.pas/c/cpp)
(File IO): input:block.in output:block.out
Description
农村有 R (1 <= R <= 100,000) 条双向的路,每条路连接 N (1 <= N <= 5000) 个结点中的两个。结点的编号是 1..N。Bessie 从结点 1出发,她的朋友(目的地)在结点 N。
次短路径可以使用最短路径上的路,而且允许退回,即到达一个结点超过一次。次短路径是一种长度大于最短路径的路径(如果存在两条或多条最短路径存在,次短路径就是比它们长,且不比其他任何的路径长的路径)。
Input
Lines 2..R+1: 每行包含三个用空格分隔的整数: A, B, 和 D表示有一条路连接结点A和B,长度为D (1 <= D <= 5000)。
Output
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
#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. 路障的更多相关文章
- 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法
彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...
- 洛谷 P3395 路障
P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个方 ...
- (jzoj snow的追寻)线段树维护树的直径
jzoj snow的追寻 DFS序上搞 合并暴力和,记录最长链和当前最远点,距离跑LCA # include <stdio.h> # include <stdlib.h> # ...
- 并发库应用之八 & 循环路障CyclicBarrier应用
JDK包位置:java.util.concurrent.CyclicBarrier 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及 ...
- django插入数据库错误:mysql的1267错误
错误信息: django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IM ...
- [jzoj]3506.【NOIP2013模拟11.4A组】善良的精灵(fairy)(深度优先生成树)
Link https://jzoj.net/senior/#main/show/3506 Description 从前有一个善良的精灵. 一天,一个年轻人B找到她并请他预言他的未来.这个精灵透过他的水 ...
- [jzoj]3468.【NOIP2013模拟联考7】OSU!(osu)
Link https://jzoj.net/senior/#main/show/3468 Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: ...
- [jzoj]5478.【NOIP2017提高组正式赛】列队
Link https://jzoj.net/senior/#main/show/5478 Description Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校 ...
- [jzoj]1115.【HNOI2008】GT考试
Link https://jzoj.net/senior/#main/show/1115 Description 申准备报名参加GT考试,准考证号为n位数X1X2X3...Xn-1Xn(0<=X ...
随机推荐
- Spring AOP——Spring 中面向切面编程
前面两篇文章记录了 Spring IOC 的相关知识,本文记录 Spring 中的另一特性 AOP 相关知识. 部分参考资料: <Spring实战(第4版)> <轻量级 JavaEE ...
- c语言字符串操作总结(转)
本文转自:http://www.jb51.net/article/37410.htm 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 s ...
- Shell笔试题2
1. 从a.log文件中提取包含"WARNING"或"FATAL",同时不包含"IGNOR"的行,然后提取以":"分割的 ...
- Cause: java.sql.SQLException: 无法转换为内部表示(Mybatis)
公司开发档案系统使用框架:Spring+Struts2+Mybatis+EasyUI,在开发过程中出现sql异常:“Cause: java.sql.SQLException: 无法转换为内部表示”,错 ...
- 前端专业术语: shim 和 Polyfill,了解下
在学习和使用 JavaScript 的时候,我们会经常碰到两个术语:shim 和 polyfill.它们有许多定义和解释,意思相近又有差异. Shim Shim 指的是在一个旧的环境中模拟出一个新 A ...
- Pod管理的iOS项目修改工程名
声明:本文大部分内容来自于以下网址,其余的部分是自己尝试的总结和补充. http://www.jianshu.com/p/5f088acecf64 完整修改iOS工程名1 http://www.cnb ...
- sql单列合并
有一组这样的数据 1 a 10 2 b 2 4 c 5 1 a 5 在应用中,我们可能需要把出现a的数据合并显示: 1 a 10,5 sqlite上实现: SELECT ...
- 《Head First 设计模式》之命令模式——遥控器
命令模式(Command) ——将“请求”封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象.命令模式也支持可撤销的操作. 要点 将发出请求的对象和执行请求的对象解耦. 被解耦的两者之间通过 ...
- 韦东山笔记之用busybox构建根文件系统
1 百度搜索busybox进入busybox官网(https://busybox.net/)作者:恒久力行 QQ:624668529 点击左侧DownloadSource下载最新稳定版的busybo ...
- A promise tomorrow is worth a lot less than trying today.
A promise tomorrow is worth a lot less than trying today.明日的承诺远不及今日的行动.