accounts_somebody @ 2023-01-01 10:38:34
用的
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
struct Node
{
int dis;
int next;
int to;
}e[N];
int n,m,s,ans[N],head[N],cnt;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
bool vis[N];
void add(int x,int y,int z)
{
e[++cnt]=(Node){y,head[x],z};
head[x]=cnt;
return ;
}
void dijkstra()
{
pq.push(make_pair(0,s));
while(pq.empty()==false)
{
pair<int,int>tmp=pq.top();
pq.pop();
int a=tmp.first,b=tmp.second;
if(vis[a]==true)
continue;
vis[a]=true;
for(int i=head[b];i>0;i=e[i].next)
{
int c=e[i].to;
if(ans[c]>ans[b]+e[i].dis)
{
ans[c]=ans[b]+e[i].dis;
pq.push(make_pair(ans[c],c));
}
}
}
return ;
}
int main()
{
cin>>n>>m>>s;
for(int i=1;i<=n;i++)
ans[i]=INT_MAX;
ans[s]=0;
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
}
dijkstra();
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
return 0;
}
码风不喜勿喷
by FFTotoro @ 2023-01-01 10:46:06
@li_bai_Delete
if(vis[a]==true)continue;
vis[a]=true;
确定不是
by accounts_somebody @ 2023-01-01 10:48:49
@zyc212303 改了之后也不对
by Accelessar @ 2023-01-01 10:56:12
@li_bai_Delete 您的 add 函数第 1 行应改为 e[++cnt]=(Node){z,head[x],y};
by FFTotoro @ 2023-01-01 10:58:04
@li_bai_Delete 为什么我在跑图的时候,
for(int i=head[b];i>0;i=e[i].next){
cout<<tmp.second<<' '<<e[i].to<<endl; // 这一句
int c=e[i].to;
if(ans[c]>ans[b]+e[i].dis)
{
ans[c]=ans[b]+e[i].dis;
pq.emplace(ans[c],c);
}
}
e[i].to
会出现
by accounts_somebody @ 2023-01-01 10:59:53
@zyc212303 @AZN_0975
谢谢,AC了