蒟蒻dij全输出-1
查看原帖
蒟蒻dij全输出-1
372219
PeyNiKge楼主2022/1/19 16:20
#include<bits/stdc++.h>
using namespace std;
const long long N=10010000;
long long n,m,u,v,w,k;
long long hd[N],cnt;
long long dis[N];
struct edge{
	long long nt,to,w,w1;
}e[N];
void lb(long long x,long long y,long long z){
	e[++cnt].nt=hd[x];
	hd[x]=cnt;
	e[cnt].to=y;
	e[cnt].w=z;
}
void lb(long long x,long long y){
	e[++cnt].nt=hd[x];
	hd[x]=cnt;
	e[cnt].to=y;
}
long long l=0,r=0x7fffff,bj[N];
struct node{
	int v,w;
	inline bool operator<(const node &x)const{
		return w>x.w;
	}
};
priority_queue<node> q;

int check(int s){
	
	for(int i=1;i<=cnt;i++){
		if(e[i].w>s){
			e[i].w1=1;
		}
		else{
			e[i].w1=0;
		}
	}
	for(int i=1;i<=n;i++){
		dis[i]=0x7ffffffff;
		bj[i]=0;
	}
	dis[1]=0;
	bj[1]=1;
	q.push((node){1,0});
	while(!q.empty()){
		node sk=q.top();
		q.pop();
		u=sk.v;
		if(bj[u]!=1){
			bj[u]=1;
			for(int i=hd[u];i;i=e[i].nt){
				v=e[i].to;
				w=e[i].w1;
				if(dis[v]>dis[u]+w){
					dis[v]=dis[u]+w;
					q.push((node){v,dis[v]});
				}
			}	
		} 
		
	}
	if(dis[n]>k){
		return 1;
	}
	return 0;
}
int main(){
	scanf("%lld%lld%lld",&n,&m,&k);
	for(long long i=1;i<=m;i++){
		scanf("%lld%lld%lld",&u,&v,&w);
		lb(u,v,w);
		lb(v,u,w);
	}
	long long ans=-1;
	while(l<=r){
		if(check((l+r)>>1)==0){
			r=((l+r)>>1)-1;
			ans=r+1;
		}
		else{
			l=((l+r)>>1)+1;
		}
	}
	printf("%lld",ans);
	return 0;
}
2022/1/19 16:20
加载中...