洛谷 1339 [USACO09OCT]热浪Heat Wave

时间:2021-05-22 16:29:02

洛谷 1339 [USACO09OCT]热浪Heat Wave

【题解】  

  最短路。那么直接写dijkstra就好了。

 

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define rg register
#define N 200010
using namespace std;
int n,m,s,t,tot,last[N],dis[N],pos[N];
struct edge{
int to,pre,dis;
}e[N<<];
struct heap{
int poi,dis;
}h[N<<];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void up(int x){
int fa;
while((fa=(x>>))&&h[fa].dis>h[x].dis){
swap(h[fa],h[x]); swap(pos[h[fa].poi],pos[h[x].poi]);
x=fa;
}
}
inline void down(int x){
int son;
while((son=x<<)<=tot){
if(son<tot&&h[son].dis>h[son+].dis) son++;
if(h[son].dis<h[x].dis){
swap(h[son],h[x]); swap(pos[h[son].poi],pos[h[x].poi]);
x=son;
}
else return;
}
}
inline void dijkstra(int x){
for(rg int i=;i<=n;i++) dis[i]=2e9;
h[tot=pos[x]=]=(heap){x,dis[x]=};
while(tot){
int now=h[].poi; h[]=h[tot--]; if(tot) down();
for(rg int i=last[now],to;i;i=e[i].pre)
if(dis[to=e[i].to]>dis[now]+e[i].dis){
dis[to]=dis[now]+e[i].dis;
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].dis=dis[to];
up(pos[to]);
}
pos[now]=;
}
}
int main(){
n=read(); m=read(); s=read(); t=read();
for(rg int i=;i<=m;i++){
int u=read(),v=read(),d=read();
e[++tot]=(edge){v,last[u],d}; last[u]=tot;
e[++tot]=(edge){u,last[v],d}; last[v]=tot;
}
dijkstra(s);
printf("%d\n",dis[t]);
return ;
}