MLE?
查看原帖
MLE?
348511
原子げんし楼主2021/1/20 22:13
#include<bits/stdc++.h>
using namespace std;
//define:
#define REP(i,n) for(int i = 0; i < n; i++)
#define ll long long
//const:
const int MAXN = 100010;
//something:
int pre[MAXN];
vector < pair <int,int> > g[MAXN];
bool vis[MAXN];
ll dist[MAXN];
priority_queue < pair <ll,int> > q;
int N,M;
void print(int x) {
	if(x) {
		print(pre[x]);
	}
	cout << x + 1 << ' ';
}
void iint() {
	REP(i,N) {
		dist[i] = 1e18;
	}
}
void dijkstra() {
	dist[0] = 0;
	q.push({0,0});//cost & id
	while(!q.empty()) {
		pair <int,int> f = q.top();
		q.pop();
		int cost = -f.first; int x = f.second;
		if(cost != dist[x]) {
			continue;
		}
		REP(i,g[x].size()) {
			pair <int,int> nf = g[x][i];
			int nx = nf.first; int ncost = nf.second;
			if(ncost + cost < dist[nx]) {
				dist[nx] = ncost + cost;
				q.push({-dist[nx],nx});
				pre[nx] = x;
			}
		}
	}
}
//run:
void solve() {
	cin >> N >> M;
	REP(i,M) {
		int x,y,z;
		cin >> x >> y >> z;
		x--; y--;
		g[x].push_back({y,z});
		g[y].push_back({x,z});
	}
	/*REP(i,N) {
		cout << i << ":";
		REP(j,g[i].size()) {
			cout << g[i][j].first << ' ';
		}
		cout << endl;
	}*/
	iint();
	dijkstra();
	if(dist[N - 1] == 1e18) {
		cout << -1 << endl;
		return;
	}
	//cout << dist[N - 1] << endl;
	print(N - 1);
}
//times:
void Times(int T) {
	while(T--) {
		solve();
	}
}
//begin:
int main() {
	int T;
	T = 1;
	//cin >> T;
	Times(T);
	return 0;
}

2021/1/20 22:13
加载中...