社区讨论

图论萌新求调,0荤

P4779【模板】单源最短路径(标准版)参与者 3已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@misx7hvn
此快照首次捕获于
2025/12/05 21:48
2 个月前
此快照最后确认于
2025/12/07 16:35
2 个月前
查看原帖
?好奇怪,是因为我存边不兑吗?
或者我这么写就是太偷懒了所以不对呢?好奇怪。
另有 , 能面刺寡人代码之过者受上赏( bushi )
代码如下:
CPP
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct node {
	long long u,v,w;
	bool operator > (const node& a) const {
		return w > a.w;
	}
};
int len=0;
priority_queue< node , vector < node > , greater < node > >q;
node a[200010];
bool vis[200010];
long long ans[200010];
void dig() {
	q.push({0,k,0});
	while(!q.empty()) {
		node uu=q.top();
		q.pop();
		if(vis[uu.u])continue;
		vis[uu.u]=1;
		for(int i=1; i<=m; i++) {
			if(vis[a[i].v])continue ;
				if(	ans[a[i].v]>ans[a[i].u]+a[i].w) {
					ans[a[i].v]=ans[a[i].u]+a[i].w;
						q.push(a[i]);
			}
		}
	}
}
int main() {
	cin>>n>>m>>k;
	for(int i=1; i<=m; i++) {
		int x,y,z;
		cin>>x>>y>>z;
		len++;
		a[len].u=x;
		a[len].v=y;
		a[len].w=z;
	}
	for(int i=1;i<=n;i++)
	ans[i]=INT_MAX;
	ans[k]=0;
	dig();
	for(int i=1; i<=n; i++)
		cout<<ans[i]<<" ";
	return 0;
}

回复

6 条回复,欢迎继续交流。

正在加载回复...