Epi4any @ 2023-02-28 17:37:32
(很可能是某些sb的错误)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int maxm=2e5+10;
const int INF=1e9+7;
int n,m,s,tot,h[maxn],dis[maxn],vis[maxn];
struct Edge{
int v,w,next;
}e[maxm];
void addedge(int u,int v,int w) {
e[++tot]={v,w,h[u]};
h[u]=tot;
}
struct Node{
int ind,dis;
bool operator < (const Node &x)const {
return dis<x.dis;
}
};
priority_queue<Node> q;
void Dijkstra(int st) {
for(int i=1;i<=n;i++) dis[i]=INF;
dis[st]=0;
q.push({st,0});
while(!q.empty()) {
Node cur=q.top(); q.pop();
if(vis[cur.ind]) continue;
vis[cur.ind]=1;
for(int i=h[cur.ind];i;i=e[i].next) {
if (dis[e[i].v] > dis[cur.ind] + e[i].w) {
dis[e[i].v] = dis[cur.ind] + e[i].w;
if (vis[e[i].v] == 0)
q.push({dis[e[i].v], e[i].v});
}
}
}
}
int main() {
cin>>n>>m>>s;
for(int i=1;i<=m;i++) {
int u,v,w; cin>>u>>v>>w;
addedge(u,v,w), addedge(v,u,w);
}
Dijkstra(s);
for(int i=1;i<=n;i++) {
cout<<dis[i]<<" ";
}
cout<<endl;
return 0;
}
by Dream_weavers @ 2023-02-28 17:45:50
开longlong
by Dream_weavers @ 2023-02-28 17:48:39
开两倍的图
by HHH6666666666 @ 2023-02-28 18:10:20
首先,你加的是双向边
by HHH6666666666 @ 2023-02-28 18:12:26
其次,
struct Node{
int ind,dis;
bool operator < (const Node &x)const {
return dis<x.dis;
}
该段代码中比较反了,应改为
struct Node{
int ind,dis;
bool operator < (const Node &x)const {
return dis>x.dis;
}
};
by HHH6666666666 @ 2023-02-28 18:14:43
还有,
if (vis[e[i].v] == 0)
q.push({dis[e[i].v], e[i].v});
这里的结构体初始化反了,改为:
if (vis[e[i].v] == 0)
q.push({e[i].v, dis[e[i].v]});
三个问题改完就A了。
by Epi4any @ 2023-04-04 14:52:50
@Interstellar @HHH6666666666 多谢多谢
回来号都凉了