Submission #446107

# Submission time Handle Problem Language Result Execution time Memory
446107 2021-07-20T23:33:09 Z JovanB Robot (JOI21_ho_t4) C++17
100 / 100
1377 ms 83884 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int N = 100000;

map <int, ll> dist[N+5];
map <int, ll> cost[N+5];

map <int, vector <pair <int, int>>> graf[N+5];

const ll INF = 1000000000000000000LL;

const int M = 200000;

ll edg[M+5];
int clr[M+5];

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n, m;
    cin >> n >> m;
    for(int i=1; i<=n; i++){
        dist[i][0] = INF;
    }
    for(int i=1; i<=m; i++){
        int a, b, c, p;
        cin >> a >> b >> c >> p;
        graf[a][c].push_back({b, i});
        graf[b][c].push_back({a, i});
        dist[b][c] = INF;
        dist[a][c] = INF;
        cost[a][c] += p;
        cost[b][c] += p;
        edg[i] = p;
        clr[i] = c;
    }
    dist[1][0] = 0;
    using T = pair <ll, pair <int, int>>;
    priority_queue <T, vector <T>, greater <T>> q;
    q.push({0, {1, 0}});
    while(!q.empty()){
        T prr = q.top();
        q.pop();
        ll dst = prr.first;
        int v = prr.second.first;
        int vcol = prr.second.second;
        if(dist[v][vcol] != dst) continue;
        if(vcol == 0){
            for(auto &colors : graf[v]){
                for(auto pr : colors.second){
                    int c = pr.first;
                    int koja = pr.second;
                    ll p = edg[koja];
                    int col = clr[koja];
                    ll sve = cost[v][col] - p;
                    ll nd;
                    /// 1 - nije bitno da li menjam granu
                    nd = dst + min(sve, p);
                    if(dist[c][0] > nd){
                        dist[c][0] = nd;
                        q.push({dist[c][0], {c, 0}});
                    }
                    /// 2 - menjam granu (i sve iste boje kod c), bitna je
                    nd = dst;
                    if(dist[c][col] > nd){
                        dist[c][col] = nd;
                        q.push({nd, {c, col}});
                    }
                }
            }
        }
        else{
            for(auto pr : graf[v][vcol]){
                int c = pr.first;
                int koja = pr.second;
                int p = edg[koja];
                ll nd = dst + cost[v][vcol] - p;
                if(dist[c][0] > nd){
                    dist[c][0] = nd;
                    q.push({nd, {c, 0}});
                }
            }
        }
    }
    ll mn = dist[n][0];
    if(mn == INF) cout << "-1\n";
    else cout << mn << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 10 ms 14324 KB Output is correct
3 Correct 9 ms 14420 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14424 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 10 ms 14540 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 13 ms 15052 KB Output is correct
10 Correct 12 ms 14988 KB Output is correct
11 Correct 11 ms 14796 KB Output is correct
12 Correct 11 ms 14668 KB Output is correct
13 Correct 11 ms 14868 KB Output is correct
14 Correct 11 ms 14836 KB Output is correct
15 Correct 10 ms 14668 KB Output is correct
16 Correct 11 ms 14788 KB Output is correct
17 Correct 11 ms 14796 KB Output is correct
18 Correct 9 ms 14596 KB Output is correct
19 Correct 11 ms 14620 KB Output is correct
20 Correct 10 ms 14736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 264 ms 34360 KB Output is correct
2 Correct 105 ms 24784 KB Output is correct
3 Correct 258 ms 29388 KB Output is correct
4 Correct 159 ms 28080 KB Output is correct
5 Correct 1275 ms 83884 KB Output is correct
6 Correct 992 ms 72708 KB Output is correct
7 Correct 454 ms 52308 KB Output is correct
8 Correct 514 ms 50580 KB Output is correct
9 Correct 531 ms 50356 KB Output is correct
10 Correct 429 ms 50232 KB Output is correct
11 Correct 203 ms 39364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14412 KB Output is correct
2 Correct 10 ms 14324 KB Output is correct
3 Correct 9 ms 14420 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14424 KB Output is correct
6 Correct 9 ms 14412 KB Output is correct
7 Correct 10 ms 14540 KB Output is correct
8 Correct 9 ms 14412 KB Output is correct
9 Correct 13 ms 15052 KB Output is correct
10 Correct 12 ms 14988 KB Output is correct
11 Correct 11 ms 14796 KB Output is correct
12 Correct 11 ms 14668 KB Output is correct
13 Correct 11 ms 14868 KB Output is correct
14 Correct 11 ms 14836 KB Output is correct
15 Correct 10 ms 14668 KB Output is correct
16 Correct 11 ms 14788 KB Output is correct
17 Correct 11 ms 14796 KB Output is correct
18 Correct 9 ms 14596 KB Output is correct
19 Correct 11 ms 14620 KB Output is correct
20 Correct 10 ms 14736 KB Output is correct
21 Correct 264 ms 34360 KB Output is correct
22 Correct 105 ms 24784 KB Output is correct
23 Correct 258 ms 29388 KB Output is correct
24 Correct 159 ms 28080 KB Output is correct
25 Correct 1275 ms 83884 KB Output is correct
26 Correct 992 ms 72708 KB Output is correct
27 Correct 454 ms 52308 KB Output is correct
28 Correct 514 ms 50580 KB Output is correct
29 Correct 531 ms 50356 KB Output is correct
30 Correct 429 ms 50232 KB Output is correct
31 Correct 203 ms 39364 KB Output is correct
32 Correct 159 ms 23272 KB Output is correct
33 Correct 212 ms 27108 KB Output is correct
34 Correct 537 ms 48788 KB Output is correct
35 Correct 403 ms 40632 KB Output is correct
36 Correct 398 ms 47292 KB Output is correct
37 Correct 454 ms 50464 KB Output is correct
38 Correct 483 ms 56344 KB Output is correct
39 Correct 185 ms 25528 KB Output is correct
40 Correct 523 ms 50464 KB Output is correct
41 Correct 566 ms 50540 KB Output is correct
42 Correct 665 ms 62912 KB Output is correct
43 Correct 296 ms 37192 KB Output is correct
44 Correct 583 ms 37228 KB Output is correct
45 Correct 439 ms 47924 KB Output is correct
46 Correct 374 ms 48116 KB Output is correct
47 Correct 407 ms 48884 KB Output is correct
48 Correct 1377 ms 83880 KB Output is correct