52分求调!!!

P4779 【模板】单源最短路径(标准版)

Enoch2013 @ 2024-09-08 09:09:00

WA了第1~3个测试点

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;
}

by lanzheng @ 2024-10-06 17:32:15

要连无向边


|