求样例,全wa
查看原帖
求样例,全wa
366937
too_simple楼主2021/9/23 17:05
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

const int N=1e5+5,M=2e5+5;
int h[N],ver[M],nex[M],w[M],tot,d[N];
bool st[N];
priority_queue<pair<int,int> > q;

void Add(int x,int y,int z) {
	tot++;
	ver[tot]=y;
	w[tot]=z;
	nex[tot]=h[x];
	h[x]=tot;
}

int main() {
	int n,m,s;
	cin>>n>>m>>s;
	for(int i=1;i<=m;++i) {
		int x,y,z;
		cin>>x>>y>>z;
		Add(x,y,z);
	}
	memset(d,0x7f,sizeof(d));
	d[s]=0;
	q.push(make_pair(0,s));
	while(q.size()) {
		int x=q.top().second;
		q.pop();
		if(st[x]) continue;
		st[x]=true;
		for(int i=h[x];i;i=nex[i]) {
			int y=ver[i],z=w[i];
			if(d[y]>d[x]+z) {
				d[y]=d[x]+z;
				q.push(make_pair(-d[y],y));
			}
		}
	}
	for(int i=1;i<=n;++i) {
		cout<<d[i]<<endl;
	}
	return 0;
}
2021/9/23 17:05
加载中...