使用模板为何一直0分
查看原帖
使用模板为何一直0分
945669
wsh1c6楼主2024/3/27 21:12
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
const int N=5e5+10;
typedef pair<int,int> pii;
int n,m,s,idx,h[N],e[N],ne[N],w[N];
int dist[N];
const int inf=2147483647;
bool st[N];
void add(int a,int b,int c)
{
	e[idx]=b;
	ne[idx]=h[a];
	w[idx]=c;
	h[a]=idx++;
}
void dijkstra()
{
	for(int i=1;i<=n;i++)dist[i]=inf;
	dist[s]=0;
	priority_queue<pii,vector<pii>,greater<pii>>heap;
	heap.push({0,1});
	while(heap.size())
	{
		auto t=heap.top();
		heap.pop();
		int d=t.first,ver=t.second;
		if(st[ver])continue;
		st[ver]=true;
		for(int i=h[ver];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dist[ver]+w[i])
			{
				dist[j]=dist[ver]+w[i];
				heap.push({dist[j],j});
			}
		}
	}
}
int main()
{
	scanf("%d %d %d",&n,&m,&s);
	memset(h,-1,sizeof h);
	while(m--)
	{
		int a,b,c;
		scanf("%d %d %d",&a,&b,&c);
		add(a,b,c);
	}
	dijkstra();
	for(int i=1;i<=n;i++)
	{
		printf("%d ",dist[i]);
	}
	return 0;
}

在acw上运行没问题,为何一直0分

2024/3/27 21:12
加载中...