感受到了 STL 的常数
查看原帖
感受到了 STL 的常数
1281794
postpone楼主2024/8/18 00:21
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;

const ll inf = (1 << 31) - 1;

namespace QuickRead {
    char buf[1 << 21], *p1 = buf, *p2 = buf;
    inline int getc() {
        return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
    }
    template <typename T> void Cin(T &a) {
        T ans = 0;
        bool f = 0;
        char c = getc();
        for (; c < '0' || c > '9'; c = getc()) {
            if (c == '-') f = 1;
        }
        for (; c >= '0' && c <= '9'; c = getc()) {
            ans = ans * 10 + c - '0';
        }
        a = f ? -ans : ans;
    }
    template <typename T, typename... Args> void Cin(T &a, Args &...args) {
        Cin(a), Cin(args...);
    }
    template <typename T> void write(T x) {
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
}

using namespace QuickRead;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n, m, s;
    Cin(n, m, s);
    s--;

    vector<vector<pair<int, ll>>> adj;
    adj.assign(n, {});

    for (int i = 0; i < m; i++) {
        int u, v;
        ll w;
        Cin(u, v, w);
        u--, v--;
        adj[u].push_back({v, w});
    }

    vector<ll> dist(n, inf);
    bitset<50005> vis(0);

    dist[s] = 0;
    for (int i = 0; i < n; i++) {
        int u = 0;
        for (int j = 0; j < n; j++) {
            if (vis[u] || (!vis[j] and dist[j] < dist[u])) {
                u = j;
            }
            vis[u] = 1;
            for (auto &&[v, w] : adj[u]) {
                dist[v] = min<ll>(dist[v], dist[u] + w);
            }
        }
    }

    for (auto &i : dist) {
        write(i);
        putchar(' ');
    }

    return 0;
}

上面的代码 T 三个点,用快读也没用。

下面这个就能AC,数据最大的测试点用时330ms左右。

也算是第一次感受到了 STL 的常数 =_=

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
using ll = long long;

const ll inf = (1 << 31) - 1;

const int N = 1e4 + 10;
struct Edge {
    int v, w;
};
vector<Edge> g[N];
ll d[N];
bitset<N> vis;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    
	int n, m, s;
    cin >> n >> m >> s;
    for (int i = 1; i <= n; i++)
        d[i] = inf;
    for (int i = 1; i <= m; i++) {
		int u, v;
		ll w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
    }
    d[s] = 0;
    for (int i = 1; i <= n; i++) {
        int u = 1;
        for (int j = 1; j <= n; j++)
            if (vis[u] || (!vis[j] && d[j] < d[u]))
                u = j;
        vis[u] = 1;
        for (auto &[v, w] : g[u])
            if (d[v] > d[u] + w)
                d[v] = d[u] + w;
    }
    for (int i = 1; i <= n; i++)
        cout << d[i] << ' ';

    return 0;
}
2024/8/18 00:21
加载中...