救救孩子! 52pts AC2、5、6
查看原帖
救救孩子! 52pts AC2、5、6
348511
原子げんし楼主2021/1/21 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:
vector < pair <int,int> > g[MAXN];
ll dist[MAXN];
bool vis[MAXN];
priority_queue < pair <ll,int> > q;
int N,M,st;
void iint() {
	REP(i,N) {
		dist[i] = 1e18;
	}
}
void dijkstra(int st) {
	dist[st] = 0;
	q.push({0,st});//cost & id
	while(!q.empty()) {
		pair <int,int> f = q.top();
		q.pop();
		ll cost = -f.first; int x = f.second;
		if(vis[x]) {
			continue;
		}
		vis[x] = 1;
		REP(i,g[x].size()) {
			pair <ll,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});
			}
		}
	}
}
//run:
void solve() {
	cin >> N >> M >> st;
	st--;
	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});
	}
	iint();
	dijkstra(st);
	REP(i,N) {
		cout << dist[i] << ' ';
	}
	cout << endl;
}
//times:
void Times(int T) {
	while(T--) {
		solve();
	}
}
//begin:
int main() {
	int T;
	T = 1;
	//cin >> T;
	Times(T);
	return 0;
}
2021/1/21 22:13
加载中...