52分求调!!!
查看原帖
52分求调!!!
1069719
Enoch2013楼主2024/9/8 09:09

WA了第1~3个测试点


code:code:

#import <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> PII;
const int N = 1e5 + 5;
long long n, m, s, x, y, z;
struct Edge
{
    long long from, to, len;
    Edge(int from, int to, int len) : from(from), to(to), len(len) {}
};
vector<Edge> edges[N];
long long dis[N];
bool vis[N];
void dijkstra()
{
	memset(dis, 0x3f, sizeof(dis));
	priority_queue<PII, vector<PII>, greater<PII> > q; 
	q.push(PII(0, s));
	dis[s] = 0;
	while (!q.empty())
	{
		PII u = q.top();
		q.pop();
		int to = u.second;
		long long to_dis = u.first;
		if (vis[to])
            continue;
        vis[to] = true;
        if (to == n)
            return;
		for (int i = 0; i < edges[to].size(); i++)
		{
			Edge y = edges[to][i];
			long long len = to_dis + y.len;
			if (vis[y.to])
				continue;
			if (dis[y.to] > len)
			{
				dis[y.to] = len;
				q.push(PII(dis[y.to], y.to));
			}
		}
	}
}
int main()
{
	scanf("%lld%lld%lld", &n, &m, &s);
	for (int i = 1; i <= m; i++)
	{
		scanf("%lld%lld%lld", &x, &y, &z);
		edges[x].push_back(Edge(x, y, z));
	}
	dijkstra();
	for (int i = 1; i <= n; i++)
		printf("%lld ", dis[i]);
	return 0;
}
2024/9/8 09:09
加载中...