关于我把dij的node结构体改成pair就WA这件事
  • 板块学术版
  • 楼主fresh_boy
  • 当前回复8
  • 已保存回复8
  • 发布时间2021/11/14 18:05
  • 上次更新2023/11/4 00:34:27
查看原帖
关于我把dij的node结构体改成pair就WA这件事
225100
fresh_boy楼主2021/11/14 18:05
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define TO second
#define W first
using namespace std;
const int N=1e6+10,NO=pow(2,31)-1;;
int s,n,m;
int d[N];
bool vis[N];
vector<pair<int,int> > p[N];
inline int read(){
   int x=0,f=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
   while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
   return x*f;
}
void add(int u,int v,int w){
   pair<int,int> t;
   t.TO=v;t.W=w;
   p[u].push_back(t);
}
void dijkstra(int x){
   int i;
   priority_queue<pair<int,int> > q;
   pair<int,int> t,T;
   memset(d,0x3f,sizeof(d));
   t.TO=x;t.W=0;d[x]=0;
   q.push(t);
   while(q.size()){
   	t=q.top();
   	q.pop();
   	x=t.TO;
   	if(vis[x]) continue;
   	vis[x]=1;
   	for(i=0;i<p[x].size();i++){
   		if(d[x]+p[x][i].W<=d[p[x][i].TO]){
   			d[p[x][i].TO]=d[x]+p[x][i].W;
   			T.TO=p[x][i].TO;
   			T.W=d[p[x][i].TO];
   			q.push(T);
   		}
   	}
   }
   
}
int main(){
   int i,u,v,w;
   n=read();m=read();s=read();
   for(i=1;i<=m;i++){
   	u=read();v=read();w=read();
   	add(u,v,w);
   }
   dijkstra(s);
   for(i=1;i<=n;i++){
   	if(d[i]==inf){
   		cout<<NO<<" ";
   	}
   	else cout<<d[i]<<" ";
   }
	return 0;
}

2021/11/14 18:05
加载中...