#10 wa求助
  • 板块CF20C Dijkstra?
  • 楼主NOIPer40
  • 当前回复0
  • 已保存回复0
  • 发布时间2020/10/19 10:23
  • 上次更新2023/11/5 10:25:25
查看原帖
#10 wa求助
185026
NOIPer40楼主2020/10/19 10:23
#include<cstdio>
#define ll long long
#define maxn 200010
using namespace std;
ll n,m,head[maxn],to[maxn],wgh[maxn],nxt[maxn],k,dis[maxn],vis[maxn],path[maxn];
struct heap{
	ll cnt,num[maxn];
	void clear(){
		cnt=0;
	}
	void swap(ll &x,ll &y){
		ll t=x;
		x=y;
		y=t;
	}
	void shift_up(ll i){
		while(i/2){
			ll t=i/2;
			if(dis[num[t]]>dis[num[i]])
				swap(num[t],num[i]);
			i=t;
		}
	}
	void shift_down(ll i){
		while(i*2<=cnt){
			ll t=i*2;
			if(dis[num[t+1]]<dis[num[t]] && t+1<=cnt)
				t++;
			if(dis[num[i]]>dis[num[t]])
				swap(num[t],num[i]);
			i=t;
		}
	}
	void push(ll x){
		num[++cnt]=x;
		shift_up(cnt);
	}
	void pop(){
		swap(num[1],num[cnt--]);
		if(empty())
			return;
		shift_down(cnt);
	}
	ll front(){
		return num[1];
	}
	bool empty(){
		return !cnt;
	}
}h;
void con(ll u,ll v,ll w){
	to[++k]=v;
	wgh[k]=w;
	nxt[k]=head[u];
	head[u]=k;
}
void add(ll a,ll b,ll w){
	con(a,b,w);
	con(b,a,w);
}
void output(ll x){
	if(x==1){
		printf("%lld ",x);
		return;
	}
	output(path[x]);
	printf("%lld ",x);
}
int main(){
	h.clear();
	scanf("%lld%lld",&n,&m);
	for(int i=2;i<=n;i++)
		dis[i]=1e15;
	for(int i=1;i<=m;i++){
		ll a,b,w;
		scanf("%lld%lld%lld",&a,&b,&w);
		add(a,b,w);
	}
	h.push(1);
	for(int i=1;i<n;i++){
		ll f=h.front();
		h.pop();
		if(vis[f])
			continue;
		vis[f]=1;
		for(int i=head[f];i;i=nxt[i]){
			ll ti=to[i],wi=wgh[i];
			if(dis[ti]>dis[f]+wi){
				dis[ti]=dis[f]+wi;
				path[ti]=f;
				h.push(ti);
			}
		}
	}
	if(dis[n]==1e15){
		printf("-1\n");
		return 0;
	}
	output(n);
	return 0;
}
2020/10/19 10:23
加载中...