#7 本地AC,为何洛谷RE
查看原帖
#7 本地AC,为何洛谷RE
999274
CNS_5t0_0r2楼主2024/6/14 19:49
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 9,M = 1e4 + 9;
int n,m,k;
int l,r;
struct tmp_node{
	int u,v,w;
} tmp[M];
struct edge{
	int to,cost,nex;
} e[M << 1];
int head[N],ecnt;
void addedge(int u,int v,int w){
	ecnt++;
	e[ecnt] = (edge){v,w,head[u]};
	head[u] = ecnt;
}

deque<int> q;
bool vis[N];
int dis[N];

void clear(){
	ecnt = 0;
	memset(head,0,sizeof head);
	memset(vis,0,sizeof vis);
	memset(dis,0x3f,sizeof dis);
	dis[1] = 0;
	q.clear();
}

bool check(int x){
	clear();
	for(int i = 1;i <= m;i++){
		addedge(tmp[i].u,tmp[i].v,tmp[i].w > x);
		addedge(tmp[i].v,tmp[i].u,tmp[i].w > x);
	}
	q.push_back(1);
	while(!q.empty()){
		int u = q.front();
		q.pop_front();
		if(u == n)
			return dis[n] <= k;
		vis[u] = true;
		for(int i = head[u];i;i = e[i].nex){
			int v = e[i].to,w = e[i].cost;
			if(!vis[v]){
				if(dis[u] + w < dis[v]){
					dis[v] = dis[u] + w;
					if(w == 0)
						q.push_front(v);
					else
						q.push_back(v);
				}
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m >> k;
	for(int i = 1;i <= m;i++){
		cin >> tmp[i].u >> tmp[i].v >> tmp[i].w;
		r = max(r,tmp[i].w);
	}
	if(!check(r)){
		cout << -1;
		return 0;
	}
	while(l < r){
		int mid = (l + r) >> 1;
		bool f = check(mid);
		//cout << "check(" << mid << ") = " << f << '\n';
		if(f)
			r = mid;
		else
			l = mid + 1;
	}
	cout << l;
	return 0;
}
2024/6/14 19:49
加载中...