捞
  • 板块灌水区
  • 楼主yinxiangbo2030
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/7/14 11:38
  • 上次更新2024/7/14 13:54:09
查看原帖
捞
1005576
yinxiangbo2030楼主2024/7/14 11:38

Dijkstra输出方案在n<=500,m<=20000的范围下MLE,求大佬

#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef unsigned us;
typedef long long ll;
ll n,m,u,v,w,ans,d[505],pr[505];
bool vs[505];
vector<pair<ll,ll> >a[505];
void dfs(ll x){
	if(pr[x]==u)cout<<u<<" ";
	else dfs(pr[x]);
	cout<<x<<" ";
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>w;
		a[u].push_back({v,w});
	}
	cin>>u>>v;
	memset(d,0x3f,sizeof d);
	d[u]=0;
	for(int i=1;i<=n;i++){
		ll x=0,mn=0x3f3f3f3f;
		for(int j=1;j<=n;j++)if(!vs[j]&&d[j]<mn)x=j,mn=d[j];
		vs[x]=1;
		for(auto it:a[x])d[it.first]=min(d[it.first],d[x]+it.second),pr[it.first]=x;
	}
	cout<<d[v]<<"\n";
	dfs(v);
	return 0;
}
2024/7/14 11:38
加载中...