社区讨论

60pts ,蒻籍求救

P10726[GESP202406 八级] 空间跳跃参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mhj4465k
此快照首次捕获于
2025/11/03 20:24
4 个月前
此快照最后确认于
2025/11/03 20:24
4 个月前
查看原帖

码风良好 , 60pts , 求调

CPP
#include<bits/stdc++.h>
#define inf 4557430888798830399
using namespace std;

int const N=1e3+5;

struct Board{
	long long l,r,h;
}L[N];

vector<int>vl[N];
vector<int>vr[N];

struct node{
	int u;
	long long w;
	int x;
	bool operator<(const node&nd)const{
		return w>nd.w;
	}
};

long long d[N];

void Dijkstra(int st){
	
	priority_queue<node>q;
	q.push({ st,0,L[st].l });
	memset(d,0x3f,sizeof d);
	d[st]=0;
	
	while(!q.empty()){
		int u=q.top().u;
		long long w=q.top().w;
		int x=q.top().x;
		q.pop();
		
	//	if(d[u]<w)continue;
		
		for(int v:vl[u]){
			long long len=( L[u].h-L[v].h )+( x-L[u].l );
			int nx=L[u].l;
			
			if(d[v]> w+len ){
				d[v]=w+len;
				q.push({ v,d[v],nx });
			}
			
		}
		
		for(int v:vr[u]){
			long long len=( L[u].h-L[v].h )+( L[u].r-x );
			int nx=L[u].r;
			
			if(d[v]>w+len){
				d[v]=w+len;
				q.push({ v,d[v],nx });
			}
			
		}
		
	}
	
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int n,st,fh;
	cin>>n>>st>>fh;
	
	for(int i=1;i<=n;i++)
		cin>>L[i].l>>L[i].r>>L[i].h;
	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			if(i==j|| L[i].h<L[j].h )continue;
			
			if( L[j].l<=L[i].l && L[i].l<=L[j].r )
				vl[i].push_back(j);
			if( L[j].l<=L[i].r && L[i].r<=L[j].r )
				vr[i].push_back(j);
			
		}
	Dijkstra(st);
	if(d[fh]==inf)
		cout<<-1;
	else cout<<d[fh];
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...