社区讨论
蒟蒻SPFA错误,求大佬纠正!
学术版参与者 3已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @lo8sfs27
- 此快照首次捕获于
- 2023/10/27 23:48 2 年前
- 此快照最后确认于
- 2023/10/27 23:48 2 年前
样例都过不了
CPP#include<bits/stdc++.h>
using namespace std;
#define inf 1234567890
int n,m,s,cnt=1;
int head[100];
int dis[100],vis[100];
queue <int> q;
struct aa {
int v,w,next;
} ff[100];
void adde(int u,int v,int w) {
ff[cnt].v=v;
ff[cnt].w=w;
ff[cnt].next=head[u];
head[u]=cnt++;
}
void spfa() {
for(int i=1; i<=n; i++) dis[i]=inf;
dis[s]=0;
q.push(s);
vis[s]=1;
int uu,vv;
while(q.empty()) {
uu=q.front();
q.pop();
vis[uu]=1;
for(int i=head[uu]; i; i=ff[i].next) {
vv=ff[i].v;
if(dis[uu]+ff[i].w<dis[vv]) {
dis[vv]=dis[uu]+ff[i].w;
if(vis[i]!=1) {
vis[i]=1;
q.push(vv);
}
}
}
}
}
int main() {
cin>>n>>m>>s;
memset(head,-1,sizeof(head));
for(int i=1; i<=m; i++) {
int u,v,w;
adde(u,v,w);
}
spfa();
for(int i=1; i<=n; i++) cout<<dis[i]<<" ";
return 0;
}
数组大小先不管
回复
共 4 条回复,欢迎继续交流。
正在加载回复...