社区讨论
求dalao们帮忙QAQ
P4779【模板】单源最短路径(标准版)参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mi6vvzmh
- 此快照首次捕获于
- 2025/11/20 11:40 4 个月前
- 此快照最后确认于
- 2025/11/20 11:40 4 个月前
CPP
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <queue>
#define ll long long
#define maxn 100000+10
#define maxm 500000+10
#define inf 2147483647
using namespace std;
ll w,dis[maxn];
bool vis[maxn];
int n,m,s,u,v,num,fir[maxn];
struct qwq
{
ll val;
int to,nxt;
}e[maxm<<1];
struct cmp
{
bool operator()(int x,int y)
{
return dis[x]>dis[y];
}
};
priority_queue<int,vector<int>,cmp> q;
ll read()
{
ll xx=0,kk=1;char ch=' ';
while(!isdigit(ch)){ch=getchar();if(ch=='-')kk=-1;}
while(isdigit(ch)){xx=xx*10+ch-'0';ch=getchar();}
return kk*xx;
}
void addedge(int u,int v,int w)
{
e[++num].to=v;
e[num].val=w;
e[num].nxt=fir[u];
fir[u]=num;
}
void dijkstra(int s)
{
dis[s]=0;q.push(s);
while(!q.empty())
{
int sx=q.top();q.pop();
if(vis[sx]) continue;
vis[sx]=true;
for(int i=fir[sx];i;i=e[i].nxt)
{
int tx=e[i].to;
if(!vis[tx]&&dis[tx]>dis[sx]+e[i].val)
{
dis[tx]=dis[sx]+e[i].val;
q.push(tx);
}
}
}
}
int main()
{
n=read(),m=read(),s=read();
for(int i=1;i<=m;++i)
{
u=read(),v=read(),w=read();
addedge(u,v,w);
}
for(int i=1;i<=n;++i) dis[i]=inf;
dijkstra(s);
for(int i=1;i<=n;++i)
printf("%lld ",dis[i]);
return 0;
}
不知道为什么wa了三个点QAQ
回复
共 1 条回复,欢迎继续交流。
正在加载回复...