QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#46112#4437. Link with RunningmiaomiaoziAC ✓922ms30064kbC++173.1kb2022-08-25 22:08:552022-08-25 22:08:57

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-25 22:08:57]
  • 评测
  • 测评结果:AC
  • 用时:922ms
  • 内存:30064kb
  • [2022-08-25 22:08:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
// https://space.bilibili.com/672346917

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long LL;
typedef pair <LL, int> PII;

constexpr double EPS = 1e-8;
const double PI = acos(-1);

int multi_cases = 1;

constexpr LL inf = 1e18;
void A_SOUL_AvA () {
    int n, m;
    cin >> n >> m;
    
    vector <array<int, 3>> e[n + 1];
    for (int i = 0, u, v, w, c; i < m; i++) {
        cin >> u >> v >> w >> c;
        e[u].pb({v, w, c});
    }
    
    vector <array<int, 3>> graph[n + 1];
    auto dij = [&]() -> LL {
        vector <LL> dis(n + 1, inf);
        vector <int> vis(n + 1, 0);
        dis[1] = 0;
        priority_queue <PII, vector<PII>, greater<>> q;
        q.push({0, 1});
        
        while (q.size()) {
            auto [d, u] = q.top();
            q.pop();
            if (vis[u]) continue;
            vis[u] = 1;
            for (auto &[v, e, p] : e[u]) {
                if (dis[v] > d + e) {
                    dis[v] = d + e;
                    q.push({dis[v], v});
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (dis[i] == inf) continue;
            for (auto &[j, e, p] : e[i]) {
                if (dis[j] == dis[i] + e) {
                    graph[i].push_back({j, e, p});
                }
            }
        }
        return dis[n];
    };
    
    auto min_energy = dij();
    
    vector <int> dfn(n + 1), low(n + 1), stk(n + 5), st(n + 5);
    vector <int> id(n + 5);
    int now = 0, scc_cnt = 0, top = 0;

    function <void(int)> tarjan = [&](int u) -> void {
        dfn[u] = low[u] = ++now;
        stk[++top] = u, st[u] = 1;
        for (auto &[v, e, p] : graph[u]) {
            if (!dfn[v]) {
                tarjan(v);
                low[u] = min(low[u], low[v]);
            } else if (st[v]) {
                low[u] = min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            int y = 0;
            scc_cnt++;
            do {
                y = stk[top--];
                st[y] = 0;
                id[y] = scc_cnt;
            } while (y != u);
        }
    };

    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) tarjan(i);
    }
    
    vector <array<int, 3>> adj[scc_cnt + 1];
    for (int i = 1; i <= n; i++) {
        for (auto &[j, e, p] : graph[i]) {
            int u = id[i], v = id[j];
            if (u != v) {
                adj[u].push_back({v, e, p});
            }
        }
    }
    vector <LL> f(n + 5);
    for (int i = id[1]; i >= 1; i--) {
        for (auto &[j, e, p] : adj[i]) {
            f[j] = max(f[j], f[i] + p);
        }
    }
    cout << min_energy << " " << f[id[n]] << "\n";
}

int main () {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(12);

    int T = 1;
    for (multi_cases && cin >> T; T; T--) {
        A_SOUL_AvA ();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 922ms
memory: 30064kb

input:

12
100000 200000
1 2 838279516 902819511
1 3 293478832 513256010
2 4 682688353 204481674
2 5 360092507 651108247
5 6 519851939 323803002
6 7 675439277 205804465
7 8 419167205 386168059
6 9 140767493 382483305
9 10 558115401 613738466
9 11 902235661 744659643
9 12 851394758 1720015
12 13 635355827 46...

output:

5927443549 11285847934
2529348 325344428756
2522027 438209666288
250100947 25049026205784
249512452 24966236662852
0 9535132634
0 25375698217
0 1000000000
0 1
0 2
0 1
0 1

result:

ok 12 lines