求助,我的dij有什么问题?
查看原帖
求助,我的dij有什么问题?
327813
__lyh__楼主2021/9/30 22:15

蒟蒻连dij都打不出来了!

#include <bits/stdc++.h>
#define N 100005
using namespace std;

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while(!isdigit(ch)){if(f == '-') f = -1; ch = getchar();}
	while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}
	return x*f;
}

vector<pair<int,int> > e[N];
int n, m, s, dis[N], vis[N];

struct cmp
{  
    bool operator ()(int &a,int &b)
    {  
        return dis[a] > dis[b];
    }  
};  
priority_queue<int, vector<int>, cmp> q;


inline void add(int x, int y, int z)
{
	e[x].push_back(make_pair(y, z));
}

int main()
{
	memset(dis, 127, sizeof(dis));
	n = read(); m = read(); s = read();
	for (int i = 1; i <= m; i++)
	{
		int x = read(), y = read(), z = read();
		add(x, y, z);
		
	}
	dis[s] = 0;
	q.push(s);
	
	while(!q.empty())
	{
		int u = q.top();
		q.pop();
		if(vis[u])
		{
			continue;
		}
		vis[u] = 1;
		int l = e[u].size();
		for (int i = 0; i < l; i++)
		{
			int to = e[u][i].first;
			int tod = e[u][i].second;
			if(dis[to] > dis[u] + tod)
			{
				dis[to] = dis[u] + tod;
				q.push(to);
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		printf("%d ", dis[i]);
	}
}
2021/9/30 22:15
加载中...