社区讨论
救救蒟蒻
P4779【模板】单源最短路径(标准版)参与者 4已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mi85vzzy
- 此快照首次捕获于
- 2025/11/21 09:08 4 个月前
- 此快照最后确认于
- 2025/11/21 09:08 4 个月前
CPP
3 4 WA了
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
char ch;
while((ch=getchar())<'0'||ch>'9');
int res=ch-48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-48;
return res;
}
const int maxn=100005,oo=24000000;
int n,m,s,cnt;
int dis[maxn],h[4*maxn];
bool vis[maxn];
struct Edge{
int to,next,w;
}edge[4*maxn];
void add(int u,int v,int w){
edge[++cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=h[u];
h[u]=cnt;
}
struct node{
int dis;
int pos;
bool operator <(const node&x)const
{
return x.dis<dis;
}
};
std::priority_queue<node>q;
void dj(){
for(ll i=1;i<=n;i++)
dis[i]=oo;
dis[s]=0;
q.push((node){0,s});
while(!q.empty()){
node tmp=q.top();
q.pop();
int now=tmp.pos,deep=tmp.dis;
if(vis[now])continue;
vis[now]=true;
for(ll i=h[now];i;i=edge[i].next){
int to=edge[i].to;
if(dis[to]>dis[now]+edge[i].w){
dis[to]=dis[now]+edge[i].w;
q.push((node){dis[to],to});
}
}
}
}
int main(){
int x,y,z;
scanf("%d%d%d",&n,&m,&s);
for(ll i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
//add(y,x,z);
}
dj();
for(ll i=1;i<=n;i++)
printf("%d ",dis[i]);
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...