样例输出0 奆佬求助
查看原帖
样例输出0 奆佬求助
823773
_sh1kong_楼主2023/1/10 11:25

code:

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#define INF 0x3f3f3f3f

const int N = 11000;

using namespace std;

typedef pair <int , int> PII;

int n,m,s,idx;

int w[N],e[N],h[N],ne[N];
int dist[N];

bool st[N];

void add_edge(int x,int y,int z)
{
	w[idx]=z;
	e[idx]=y;
	ne[idx]=h[x];
	h[x]=idx++;
}

void dijkstra()

{
	memset(dist,INF,sizeof dist);
	dist[s]=0;
	priority_queue <PII , vector <PII> , greater<PII> > heap;
	heap.push({0,1});
	while (heap.size())
	{
		PII k=heap.top();
		heap.pop();
		int ver=k.second,dis=k.first;
		if(st[ver]) continue;
		st[ver]=true;
		for (int i=h[ver];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>w[i]+dis)
			{
				dist[j]=w[i]+dis;
				heap.push({dist[j],j});
			}
		}
	}
}

long long qpow(int a,int n)
{
	long long ans=1;
	while (n)
	{
		if(n&1) ans*=a;
		n>>=1;
		a*=a;
	}
	return ans;
}

int main()

{
	memset(h,-1,sizeof h);
	cin >> n >> m >> s;
	while (m --)
	{
		int x,y,w;
		cin >> x >> y >> w;
		add_edge(x,y,w);
	}
	for (int i=1;i<=n;i++)
	{
		if(dist[i]==INF)
		{
			cout << qpow(2,31)-1 << " ";
		}
		else cout << dist[i] << " ";
	}
	return 0;
}
2023/1/10 11:25
加载中...