考虑倒过来计算最短路径长度,设dis[u]表示在最坏情况下,点u到最近的一 个出口的最短路,则p个出口的dis值都是0,答案即为dis[0]。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cctype>
  6. #include <queue>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. #define res register int
  11. #define LL long long
  12. #define inf 0x3f3f3f3f
  13. inline int read()
  14. {
  15. int x(),f(); char ch;
  16. while(!isdigit(ch=getchar())) if(ch=='-') f=-;
  17. while(isdigit(ch)) x=x*+ch-'',ch=getchar();
  18. return f*x;
  19. }
  20.  
  21. inline int max(int x,int y){return x>y?x:y;}
  22. inline int min(int x,int y){return x<y?x:y;}
  23. const int N=+;
  24. int n,m,k,d,tot;
  25. int head[N],ver[N<<],nxt[N<<],edge[N<<];
  26. int dis[N],vis[N];
  27. inline void add(int x,int y,int z){
  28. ver[++tot]=y; nxt[tot]=head[x]; head[x]=tot; edge[tot]=z;
  29. }
  30. struct node{
  31. int id;
  32. LL dd;
  33. bool operator<(const node &n2) const {
  34. return dd>n2.dd;}
  35. };
  36. priority_queue<node> q;
  37.  
  38. inline LL dij()
  39. {
  40. while(q.size())
  41. {
  42. node now=q.top(); q.pop();
  43. int x=now.id,z=now.dd;
  44. if(++vis[x]>d+) continue;
  45. if(vis[x]==d+ || !dis[x])
  46. {
  47. dis[x]=z;
  48. for(res i(head[x]) ; i ; i=nxt[i])
  49. {
  50. int y=ver[i];
  51. if(dis[y]==inf)
  52. q.push((node){ver[i],dis[x]+(LL)edge[i]});
  53. }
  54. }
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. freopen("maze.in","r",stdin);
  61. freopen("maze.out","w",stdout);
  62.  
  63. memset(dis,inf,sizeof(dis));
  64. n=read(); m=read(); k=read(); d=read();
  65. for(res i= ; i<=m ; i++)
  66. {
  67. int x=read(),y=read(),z=read();
  68. add(x,y,z); add(y,x,z);
  69. }
  70. for(res i= ; i<=k ; i++)
  71. {
  72. int x=read(); dis[x]=;
  73. q.push((node){x,});
  74. }
  75. dij();
  76. if(dis[]==inf) puts("-1");
  77. else cout<<dis[]<<endl;
  78.  
  79. return ;
  80. }

2019.2.15 t2的更多相关文章

  1. Data truncation: Incorrect datetime value: 'May 15, 2019 4:15:37 PM

    因为系统在windows下测试过是正常的 windows下的jdk+ windows下安装的mysql 全部cases通过 linux下的jdk + windows下安装的mysql 新增和更新,影响 ...

  2. MyBatis 配置/注解 SQL CRUD 经典解决方案(2019.08.15持续更新)

    本文旨在记录使用各位大神的经典解决方案. 2019.08.14 更新 Mybatis saveOrUpdate SelectKey非主键的使用 MyBatis实现SaveOrUpdate mybati ...

  3. 【2019.8.15 慈溪模拟赛 T2】组合数(binom)(卢卡斯定理+高维前缀和)

    卢卡斯定理 题目中说到\(p\)是质数. 而此时要求组合数向质数取模的结果,就可以用卢卡斯定理: \[C_x^y=C_{x\ div\ p}^{y\ div\ p}\cdot C_{x\ mod\ p ...

  4. 【2019.7.15 NOIP模拟赛 T2】与非树(nand)(树形DP)

    树形\(DP\) 实际上,这道题应该不是很难. 我们设\(f_{x,i,j}\)表示在以\(x\)为根的子树内,原本应输出\(i\),结果输出了\(j\)的情况数. 转移时,为了方便,我们先考虑与,再 ...

  5. T2:中间值(median)———2019.10.15

    代码: #include <bits/stdc++.h> int ri() { , f = ; ; ) + (x << ) - ' + c; return x * f; } ; ...

  6. 纪中OJ 2019.02.15【NOIP提高组】模拟 B 组 梦回三国 比赛题解(第一个)

    声明 旁边的同学小 H(胡)对我说: “哟,比赛拿了 140,强!要知道,如果哥第三题 AC 了,哥就 230 了,你个废柴!!!(比赛实际分数 130 额呵)” 顿时,千万草泥马从我心中奔腾而过:你 ...

  7. 2019.03.15 ZJOI2019模拟赛 解题报告

    得分: \(20+45+15=80\)(三题暴力全写挂...) \(T1\):Lyk Love painting 首先,不难想到二分答案然后\(DP\)验证. 设当前需验证的答案为\(x\),则一个暴 ...

  8. 第十八次CSP认证游记 | 2019.12.15

    CSP认证的考试是Haogod介绍的,取得一定成绩之后能有机会参加CCSP的分赛区和全国决赛.这次来参加认证要感谢老师的奔走为我们申请学校的报销,虽然最终因为这不是比赛所以报名费和差旅费下不来,但是老 ...

  9. Python脱产8期 Day03 2019/4/15

    一 变量的命名规范 1.只能由 字母, 数字,  _, 组成. 2. 不能以数字开头 3.避免与系统关键字重名:重名不会报错,但系统的功能就被自定义的功能屏蔽掉了(严重不建议这样来做) 4.以_开头的 ...

随机推荐

  1. Node.js中流程控制

    Node.js中的流程控制可以使用async,在使用之前需要先安装,使用npm安装 npm install async --g 下面主要介绍4种流程控制的方式: 1.串行无关联:async.serie ...

  2. CloudFoundry 快速上手笔记

    1.登陆cf 2.登陆进入webservice 3.查看ruby版本 4.查看gem版本 5.安装CF 6.配置cf Download the CLI from github: https://git ...

  3. Python 中的 is 和 id-乾颐堂

    (ob1 is ob2) 等价于 (id(ob1) == id(ob2)) 首先id函数可以获得对象的内存地址,如果两个对象的内存地址是一样的,那么这两个对象肯定是一个对象.和is是等价的.Pytho ...

  4. 基于mosquitto的MQTT服务器---SSL/TLS 单向认证+双向认证

    基于mosquitto的MQTT服务器---SSL/TLS 单向认证+双向认证 摘自:https://blog.csdn.net/ty1121466568/article/details/811184 ...

  5. [SHELL]:let 命令详解

    [SHELL]:let 命令详解 摘自:https://blog.csdn.net/happygongzhuo/article/details/6819099 let :简单的计算器  语 法let[ ...

  6. sed命令n,N,d,D,p,P,h,H,g,G,x解析2

    摘自: https://blog.csdn.net/xiexingshishu/article/details/50514132 sed命令n,N,d,D,p,P,h,H,g,G,x解析 2016年0 ...

  7. [GO]使用select实现超时

    package main import ( "fmt" "time" ) func main() { ch := make(chan int) quit := ...

  8. HDU 1104 Remainder (BFS求最小步数 打印路径)

    题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将 ...

  9. 编写高质量代码改善C#程序的157个建议——建议113:声明变量前考虑最大值

    建议113:声明变量前考虑最大值 假设正在开发一个工资系统,其中一个模块负责处理加薪.代码如下: static void Main(string[] args) { ; salary = (); Co ...

  10. [LeetCode 题解]: plusOne

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a no ...