60分求调,三个re,一个wa
查看原帖
60分求调,三个re,一个wa
1233392
RY111楼主2024/7/18 10:05
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m,s;
int h[maxn],ne[maxn],e[maxn],w[maxn],idx;
int dist[maxn],cnt[maxn];
bool st[maxn];
void add(int a,int b,int c)
{
	e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}

void spfa()
{
	memset(dist,0x3f,sizeof dist);
	
	dist[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		st[t]=false;
		for(int i=h[t];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dist[t]+w[i])
			{
				dist[j]=dist[t]+w[i];
				if(!st[j])
				{
					q.push(j);
					st[j]=true;
				}
			}
		}
	}
//	if(dist[n]==0x3f3f3f3f)
//	return -1;
//	else
//	return dist[n];
	
	
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m>>s;
	memset(h,-1,sizeof h);
	//最开始所有头节点指向-1; 
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		add(a,b,c); 
		
	}
	spfa( );
//	if(t==-1)
//	puts("impossilbe");
//	else
//	cout<<t;
//int a[maxn];
//int j;
//for(int i=h[s];i!=-1;i=ne[i])
//{j=0;
//a[j]=dist[e[i]];
//j++;
//}
//for(;j>=0;j--)
//{
//	cout<<a[j]<<" ";
//}
for (int i = 1; i <= n; i++) {
       
            cout << dist[i] << " ";
    }
	return 0;
	}
2024/7/18 10:05
加载中...