#5仅超时0.02ms,求佬优化
查看原帖
#5仅超时0.02ms,求佬优化
1573746
wyxing楼主2025/7/29 09:16

C++20 开启O2优化#5超时0.02ms

全是Floyd题解吗,我迪杰斯特拉也未尝不利

#include<bits/stdc++.h>
#define p pair<int,int>
#define ll long long
using namespace std;
const int N=300+5;

inline int rd() {
	int x=0;
	char d=getchar_unlocked();
	while(d<'0'||d>'9')d=getchar_unlocked();
	while(d>='0'&&d<='9') {
		x=(x<<3)+(x<<1)+(d^48);
		d=getchar_unlocked();
	}
	return x;
}

int n,m,t,u,v,w,e1,e2,k;
int val[N];
bool fl[N];
struct edge {
	int to,w;
} e;
vector<edge> g[N];
struct cmp {
	bool operator ()(const edge& a,const edge& b)const {
		return a.w>b.w;
	}
};
int dij() {
	priority_queue<edge,vector<edge>,cmp> q;
	q.push({e1,0});
	memset(val,0x3f3f3f3f,sizeof(val));
	memset(fl,0,sizeof(fl));

	while(!q.empty()) {
		e=q.top();
		q.pop();
		if(e.to==e2)return e.w;
		if(fl[e.to])continue;
		fl[e.to]=true;
		for(auto &x:g[e.to]) {
			k=max(e.w,x.w);
			if(k<val[x.to]) {
				q.push({x.to,k});
				val[x.to]=k;
			}
		}

	}
	return -1;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	n=rd(),m=rd(),t=rd();
	while(m--) {
		u=rd(),v=rd(),w=rd();
		g[u].push_back({v,w});
	}
	while(t--) {
		e1=rd(),e2=rd();
		printf("%d\n",dij());
	}
	return 0;
}
2025/7/29 09:16
加载中...