填坑填坑填坑……链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2763
题意:有$m$次免费机会,求出最小值。
二维最短路没什么说的。注意时间很坑人,要用双端队列优化$SPFA$(我再说一遍堆优化SPFA是不存在的……)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxm=50005,maxt=15,maxn=10005; 7 struct pii 8 { 9 int pos,tim; 10 bool operator <(const pii &b)const 11 { 12 return pos>b.pos; 13 } 14 }; 15 #include<deque> 16 deque<pii>heap; 17 int dis[maxn][maxt],n,m,k,s,t; 18 bool inqueue[maxn][maxt]; 19 struct node 20 { 21 int from,to,dis,next; 22 }edge[maxm<<1]; 23 int head[maxn],tot; 24 void addedge(int u,int v,int w) 25 { 26 edge[++tot]=(node){u,v,w,head[u]};head[u]=tot; 27 } 28 void Push(pii a) 29 { 30 if(a<heap.front())heap.push_front(a); 31 else heap.push_back(a); 32 } 33 void spfa() 34 { 35 dis[s][k]=0;Push((pii){s,k}); 36 inqueue[s][k]=1; 37 while(!heap.empty()) 38 { 39 pii s=heap.front();heap.pop_front(); 40 int pos=s.pos,tim=s.tim,dist=dis[pos][tim];inqueue[pos][tim]=0; 41 for(int i=head[pos];i;i=edge[i].next) 42 { 43 int v=edge[i].to; 44 if(dis[v][tim]>dist+edge[i].dis) 45 { 46 dis[v][tim]=dist+edge[i].dis; 47 if(!inqueue[v][tim])Push((pii){v,tim}),inqueue[v][tim]=1; 48 } 49 if(tim&&dis[v][tim-1]>dist) 50 { 51 dis[v][tim-1]=dist; 52 if(!inqueue[v][tim-1])Push((pii){v,tim-1}),inqueue[v][tim-1]=1; 53 } 54 } 55 } 56 } 57 int haha() 58 { 59 scanf("%d%d%d",&n,&m,&k);scanf("%d%d",&s,&t); 60 for(int i=1;i<=m;i++) 61 { 62 int x,y,z;scanf("%d%d%d",&x,&y,&z); 63 addedge(x,y,z);addedge(y,x,z); 64 } 65 memset(dis,0x3f,sizeof(dis)); 66 spfa(); 67 int ans=0x7fffffff; 68 for(int i=0;i<=k;i++)ans=min(ans,dis[t][i]); 69 printf("%d\n",ans); 70 } 71 int sb=haha(); 72 int main(){;}