P4568 [JLOI2011]飞行路线

传送门:https://www.luogu.org/problem/P4568

就是一个分层图

 1 #include
 2 using namespace std;
 3 
 4 int n,m,k;
 5 int s,t;
 6 int dis[15][50009];
 7 int vis[15][50009];
 8 struct node{
 9     int numk,num,val;
10     bool operator <(const node &a)const
11     {
12         return a.val<val;
13     }
14 };
15 struct edge{
16     int next,to,w;
17 }e[3000009];int tot;
18 int first[50009];
19 inline void add_edge(int a,int b,int c)
20 {
21     e[++tot].next=first[a];
22     e[tot].to=b;
23     e[tot].w=c;
24     first[a]=tot;
25 }
26 inline int read()
27 {
28     int x=0,f=1;char ch=getchar();
29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
30     while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
31     return x*f;
32 }
33 inline void dijkstra()
34 {
35     priority_queueq;
36     memset(dis,0x3f3f3f3f,sizeof(dis));
37     dis[0][s]=0;
38     q.push((node){0,s,0});
39     while(!q.empty())
40     {
41         node uall=q.top();
42         int uk=uall.numk;
43         int u=uall.num;
44         q.pop();
45         if(vis[uk][u])continue;
46         vis[uk][u]=true;
47         for(register int i=first[u];i;i=e[i].next)
48         {
49             int v=e[i].to;
50             if(uk+1<=k&&dis[uk][u]1][v])
51             {
52                 dis[uk+1][v]=dis[uk][u];
53                 if(vis[uk+1][v]==false)q.push((node){uk+1,v,dis[uk+1][v]});
54             }
55             if(dis[uk][u]+e[i].w<dis[uk][v])
56             {
57                 dis[uk][v]=dis[uk][u]+e[i].w;
58                 if(vis[uk][v]==false)q.push((node){uk,v,dis[uk][v]});
59             }
60         }
61     }
62 }
63 int main()
64 {
65     n=read(),m=read(),k=read();
66     s=read(),t=read();
67     for(register int i=1,a,b,c;i<=m;i++)
68     {
69         a=read(),b=read(),c=read();
70         add_edge(a,b,c);
71         add_edge(b,a,c);
72     }
73     dijkstra();
74     int ans=99999999;
75     for (register int i=0;i<=k;i++)
76         ans=min(ans,dis[i][t]);
77     cout<<ans;
78 }

P4568 [JLOI2011]飞行路线_第1张图片

 

你可能感兴趣的:(P4568 [JLOI2011]飞行路线)