全TLE求助

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

lwx20211103 @ 2022-10-23 22:27:35

#include <bits/stdc++.h>//stupid windows,stupid dev-c++
using namespace std;//I love linux!

int n, m, s;
int vis[114514], dis[114514], inf = 0x7fffffff;
vector<pair<int, int> > photo[114514];

void dijkstra()
{
    int i;
    for (i = 1;i <= n;i++)
    {
        dis[i] = inf;
    }
    dis[s] = 0;
    for (int j = 1;j <= n;j++)
    {
        int temp = inf, k;
        for (int j = 1;j <= n;j++)
        {
            if (!vis[j] && dis[j] < temp)
            {
                temp = dis[j];
                k = j;
            }
        }
        vis[k] = 1;
        for (int j = 0;j < photo[k].size();j++)
        {
            int p = photo[k][j].first;
            int w = photo[k][j].second;
            if (vis[p]) continue;
            if (dis[k] + w < dis[p])
            {
                dis[p] = dis[k] + w;
            }
        }
    }
}

int main()
{
    cin >> n >> m >> s;
    int u, v, w;
    int i;
    for (i = 0;i < m;i++)
    {
        cin >> u >> v >> w;
        photo[u].push_back(make_pair(v, w));
    }
    dijkstra();
    for (i = 1;i <= n;i++)
    {
        cout << dis[i] << " ";
    }
    return 0;
}

by FFTotoro @ 2022-10-23 22:49:13

@lwx20211103 我不确定是否需要使用堆优化


by Amadeus004 @ 2022-10-23 23:05:32

应该要堆优吧,不堆优时间复杂度O(n^2),这题n^2有1e10,个人认为会完全T掉


by SnapYust @ 2022-10-25 22:52:34

需要优化兄弟@lwx20211103 全T肯定要想到优化啊


|