Submission #839402

# Submission time Handle Problem Language Result Execution time Memory
839402 2023-08-30T01:31:45 Z Nhoksocqt1 Ceste (COCI17_ceste) C++17
160 / 160
87 ms 3656 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 2003;

struct Edge {
    int u, v, t, c;
} edge[MAXN];

struct State {
    int t, c, u;

    State(int _t = 0, int _c = 0, int _u = 0) : t(_t), c(_c), u(_u) {};

    bool operator < (const State &s) const {
        return (t != s.t) ? t > s.t : c > s.c;
    }

};

ll answer[MAXN];
vector<int> adj[MAXN];
int minCost[MAXN], minCostInHeap[MAXN], numNode, numEdge;

void process() {
    cin >> numNode >> numEdge;
    for (int i = 0; i < numEdge; ++i) {
        int u, v, t, c;
        cin >> u >> v >> t >> c;
        //u = Random(1, numNode - 1), v = Random(u + 1, numNode), t = Random(1, 100), c = Random(1, 100); cout << u << ' ' << v << ' ' << t << ' ' << c << '\n';
        edge[i] = {u, v, t, c};
        adj[u].push_back(i);
        adj[v].push_back(i);
    }

    for (int i = 1; i <= numNode; ++i) {
        minCost[i] = minCostInHeap[i] = 1e9;
        answer[i] = 1e18;
    }

    priority_queue<State> heap;
    heap.push({0, 0, 1});
    minCostInHeap[1] = 0;

    while(sz(heap)) {
        int u(heap.top().u), dt(heap.top().t), dc(heap.top().c);
        heap.pop();

        if(minCost[u] <= dc)
            continue;

        minCost[u] = dc;
        answer[u] = min(answer[u], 1LL * dt * dc);
        for (int it = 0; it < sz(adj[u]); ++it) {
            int id(adj[u][it]), v(u ^ edge[id].u ^ edge[id].v), t(edge[id].t), c(edge[id].c);
            if(minCost[v] > dc + c)
                heap.push({dt + t, dc + c, v});
        }
    }

    for (int i = 2; i <= numNode; ++i) {
        if(answer[i] >= 1e18)
            answer[i] = -1;

        cout << answer[i] << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    process();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 392 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 916 KB Output is correct
2 Correct 8 ms 916 KB Output is correct
3 Correct 8 ms 920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 1356 KB Output is correct
2 Correct 86 ms 3656 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 596 KB Output is correct
2 Correct 81 ms 2124 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 740 KB Output is correct
2 Correct 75 ms 1364 KB Output is correct
3 Correct 68 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 1364 KB Output is correct
2 Correct 68 ms 1364 KB Output is correct
3 Correct 87 ms 1364 KB Output is correct