码迷,mamicode.com
首页 > 其他好文 > 详细

2019.2.15 t2

时间:2019-02-15 21:08:51      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:name   register   fine   namespace   turn   c51   ide   type   color   

技术图片

技术图片

考虑倒过来计算最短路径长度,设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(0),f(1); char ch;
16     while(!isdigit(ch=getchar())) if(ch==-) f=-1;
17     while(isdigit(ch)) x=x*10+ch-0,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=1000000+10;
24 int n,m,k,d,tot;
25 int head[N],ver[N<<1],nxt[N<<1],edge[N<<1];
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+1) continue;
45         if(vis[x]==d+1 || !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=1 ; 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=1 ; i<=k ; i++)
71     {
72         int x=read(); dis[x]=0;
73         q.push((node){x,0});
74     }
75     dij();
76     if(dis[0]==inf) puts("-1");
77     else cout<<dis[0]<<endl;
78 
79     return 0;
80 }
View Code

 

2019.2.15 t2

标签:name   register   fine   namespace   turn   c51   ide   type   color   

原文地址:https://www.cnblogs.com/wmq12138/p/10385907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!