Submission #527639

# Submission time Handle Problem Language Result Execution time Memory
527639 2022-02-17T21:13:32 Z Lobo Olympic Bus (JOI20_ho_t4) C++17
100 / 100
313 ms 6112 KB
#include<bits/stdc++.h>
using namespace std;
 
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
 
#define maxn 210
#define maxm 50010
 
int n, m, d[maxn][maxn], dx1[maxn][maxn], dxn[maxn][maxn], dxi1[maxn][maxn], dxin[maxn][maxn];
pair<int,int> mn1[maxn], mnn[maxn];
int u[maxm], v[maxm], c[maxm], up[maxm], mark[maxn];
vector<int> g[maxn],gi[maxn];
 
void shpx1(int x) {
    for(int i = 1; i <= n; i++) {
        dx1[x][i] = inf;
        mark[i] = 0;
    }
 
    dx1[x][1] = 0;
        
    if(x == 1) return;
    int cnt = n;
    while(cnt--) {
        int a;
        int dist = inf;
        for(int i = 1; i <= n; i++) {
            if(!mark[i] && dx1[x][i] < dist) {
                dist = dx1[x][i];
                a = i;
            }
        }

        if(dist == inf) return;
        mark[a] = 1;

        for(auto i : g[a]) {
            int b = v[i];
            int w = c[i];
            if(b == x) continue;
 
            if(dx1[x][b] > dx1[x][a] + w) {
                dx1[x][b] = dx1[x][a] + w;
            }
        }
    }
}

void shpxi1(int x) {
    for(int i = 1; i <= n; i++) {
        dxi1[x][i] = inf;
        mark[i] = 0;
    }
 
    dxi1[x][1] = 0;
        
    if(x == 1) return;
    int cnt = n;
    while(cnt--) {
        int a;
        int dist = inf;
        for(int i = 1; i <= n; i++) {
            if(!mark[i] && dxi1[x][i] < dist) {
                dist = dxi1[x][i];
                a = i;
            }
        }

        if(dist == inf) return;
        mark[a] = 1;

        for(auto i : gi[a]) {
            int b = u[i];
            int w = c[i];
            if(b == x) continue;
 
            if(dxi1[x][b] > dxi1[x][a] + w) {
                dxi1[x][b] = dxi1[x][a] + w;
            }
        }
    }
}

void shpxin(int x) {
    for(int i = 1; i <= n; i++) {
        dxin[x][i] = inf;
        mark[i] = 0;
    }
 
    dxin[x][n] = 0;
        
    if(x == n) return;
    int cnt = n;
    while(cnt--) {
        int a;
        int dist = inf;
        for(int i = 1; i <= n; i++) {
            if(!mark[i] && dxin[x][i] < dist) {
                dist = dxin[x][i];
                a = i;
            }
        }

        if(dist == inf) return;
        mark[a] = 1;
        for(auto i : gi[a]) {
            int b = u[i];
            int w = c[i];
            if(b == x) continue;
 
            if(dxin[x][b] > dxin[x][a] + w) {
                dxin[x][b] = dxin[x][a] + w;
            }
        }
    }
}
 
 
void shpxn(int x) {
    for(int i = 1; i <= n; i++) {
        dxn[x][i] = inf;
        mark[i] = 0;
    }
 
    dxn[x][n] = 0;
        
    if(x == n) return;
    int cnt = n;
    while(cnt--) {
        int a;
        int dist = inf;
        for(int i = 1; i <= n; i++) {
            if(!mark[i] && dxn[x][i] < dist) {
                dist = dxn[x][i];
                a = i;
            }
        }

        if(dist == inf) return;
        mark[a] = 1;
        for(auto i : g[a]) {
            int b = v[i];
            int w = c[i];
            if(b == x) continue;
 
            if(dxn[x][b] > dxn[x][a] + w) {
                dxn[x][b] = dxn[x][a] + w;
            }
        }
    }
}
 
void solve() {
    cin >> n >> m;
 
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            d[i][j] = inf;
        }
    }

    for(int i = 1; i <= n; i++) {
        d[i][i] = 0;
    }
 
    for(int i = 1; i <= m; i++) {
        cin >> u[i] >> v[i] >> c[i] >> up[i];
        g[u[i]].pb(i);
        gi[v[i]].pb(i);
 
        d[u[i]][v[i]] = min(d[u[i]][v[i]],c[i]);
    }
 
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
 
    //tenho que pegar dx1 e dxn
    for(int i = 1; i <= n; i++) {
        shpx1(i);
        shpxn(i);
        shpxi1(i);
        shpxin(i);
    }

    //pegar o menor/segundo menor de cada cara
 
    for(int a = 1; a <= n; a++) {
        mn1[a] = mp(inf,inf);
        if(a == n) mn1[a] = mp(0,0);
 
        for(auto i : g[a]) {
            int b = v[i];
 
            mn1[a].sc = min(mn1[a].sc, dx1[b][a] + c[i] + dxin[a][b]);
 
            if(mn1[a].sc < mn1[a].fr) swap(mn1[a].fr,mn1[a].sc);
        }
 
    }
    
    for(int a = 1; a <= n; a++) {
        mnn[a] = mp(inf,inf);
        if(a == 1) mnn[a] = mp(0,0);
 
        for(auto i : g[a]) {
            int b = v[i];
 
            mnn[a].sc = min(mnn[a].sc, dxn[b][a] + c[i] + dxi1[a][b]);
 
            if(mnn[a].sc < mnn[a].fr) swap(mnn[a].fr,mnn[a].sc);
        }
    }
 
    int ans = inf;
    ans = min(ans, d[1][n]+d[n][1]);
 
    //usa no 1->n
    for(int i = 1; i <= m; i++) {
        int ans1 = 0;
 
        if(d[n][1] == d[n][u[i]] + c[i] + d[v[i]][1]) {
            ans1 = min(dxn[u[i]][1], mnn[u[i]].sc);
        }
        else {
            ans1 = d[n][1];
        }
        
        ans1 = min(ans1,dxn[u[i]][v[i]]+c[i]+dxi1[v[i]][u[i]]);
        //d(n,v[i]) sem passar pro u[i]
        //d(u[i],1) sem passar por v[i]
        ans1+= dx1[u[i]][v[i]] + c[i] + up[i] + dxin[v[i]][u[i]];
        ans = min(ans,ans1);
    }
 
    //usa no n->1
    for(int i = 1; i <= m; i++) {
        int ans1 = 0;
 
        if(d[1][n] == d[1][u[i]] + c[i] + d[v[i]][n]) {
            ans1 = min(dx1[u[i]][n], mn1[u[i]].sc);
        }
        else {
            ans1 = d[1][n];
        }
        
        ans1 = min(ans1,dx1[u[i]][v[i]]+c[i]+dxin[v[i]][u[i]]);
        ans1+= dxn[u[i]][v[i]] + c[i] + up[i] + dxi1[v[i]][u[i]];
        ans = min(ans,ans1);
    }
 
    if(ans == inf) cout << -1 << endl;
    else cout << ans << endl;
 
}
 
int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);
 
    // freopen("in.in", "r", stdin);
    //freopen("out.out", "w", stdout);
    
    int tt = 1;
    // cin >> tt;
    while(tt--) solve();
 
}
# Verdict Execution time Memory Grader output
1 Correct 57 ms 1992 KB Output is correct
2 Correct 14 ms 2000 KB Output is correct
3 Correct 86 ms 2000 KB Output is correct
4 Correct 88 ms 1996 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 24 ms 2012 KB Output is correct
7 Correct 1 ms 360 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 113 ms 2016 KB Output is correct
11 Correct 90 ms 2076 KB Output is correct
12 Correct 89 ms 2044 KB Output is correct
13 Correct 43 ms 2032 KB Output is correct
14 Correct 72 ms 2060 KB Output is correct
15 Correct 72 ms 2012 KB Output is correct
16 Correct 70 ms 2044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 184 ms 5796 KB Output is correct
2 Correct 225 ms 5844 KB Output is correct
3 Correct 240 ms 6088 KB Output is correct
4 Correct 88 ms 2120 KB Output is correct
5 Correct 85 ms 1992 KB Output is correct
6 Correct 31 ms 1996 KB Output is correct
7 Correct 17 ms 2000 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 122 ms 6112 KB Output is correct
10 Correct 92 ms 5952 KB Output is correct
11 Correct 145 ms 6036 KB Output is correct
12 Correct 150 ms 6012 KB Output is correct
13 Correct 158 ms 6032 KB Output is correct
14 Correct 172 ms 6108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 1968 KB Output is correct
2 Correct 34 ms 1904 KB Output is correct
3 Correct 117 ms 4812 KB Output is correct
4 Correct 24 ms 1996 KB Output is correct
5 Correct 145 ms 5756 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 86 ms 5700 KB Output is correct
9 Correct 87 ms 5676 KB Output is correct
10 Correct 118 ms 5704 KB Output is correct
11 Correct 115 ms 5696 KB Output is correct
12 Correct 160 ms 5724 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 117 ms 5828 KB Output is correct
20 Correct 159 ms 5828 KB Output is correct
21 Correct 114 ms 5696 KB Output is correct
22 Correct 116 ms 5712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 1992 KB Output is correct
2 Correct 14 ms 2000 KB Output is correct
3 Correct 86 ms 2000 KB Output is correct
4 Correct 88 ms 1996 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 24 ms 2012 KB Output is correct
7 Correct 1 ms 360 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 113 ms 2016 KB Output is correct
11 Correct 90 ms 2076 KB Output is correct
12 Correct 89 ms 2044 KB Output is correct
13 Correct 43 ms 2032 KB Output is correct
14 Correct 72 ms 2060 KB Output is correct
15 Correct 72 ms 2012 KB Output is correct
16 Correct 70 ms 2044 KB Output is correct
17 Correct 184 ms 5796 KB Output is correct
18 Correct 225 ms 5844 KB Output is correct
19 Correct 240 ms 6088 KB Output is correct
20 Correct 88 ms 2120 KB Output is correct
21 Correct 85 ms 1992 KB Output is correct
22 Correct 31 ms 1996 KB Output is correct
23 Correct 17 ms 2000 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 122 ms 6112 KB Output is correct
26 Correct 92 ms 5952 KB Output is correct
27 Correct 145 ms 6036 KB Output is correct
28 Correct 150 ms 6012 KB Output is correct
29 Correct 158 ms 6032 KB Output is correct
30 Correct 172 ms 6108 KB Output is correct
31 Correct 60 ms 1968 KB Output is correct
32 Correct 34 ms 1904 KB Output is correct
33 Correct 117 ms 4812 KB Output is correct
34 Correct 24 ms 1996 KB Output is correct
35 Correct 145 ms 5756 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 0 ms 332 KB Output is correct
38 Correct 86 ms 5700 KB Output is correct
39 Correct 87 ms 5676 KB Output is correct
40 Correct 118 ms 5704 KB Output is correct
41 Correct 115 ms 5696 KB Output is correct
42 Correct 160 ms 5724 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 0 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 1 ms 328 KB Output is correct
49 Correct 117 ms 5828 KB Output is correct
50 Correct 159 ms 5828 KB Output is correct
51 Correct 114 ms 5696 KB Output is correct
52 Correct 116 ms 5712 KB Output is correct
53 Correct 234 ms 5772 KB Output is correct
54 Correct 195 ms 5796 KB Output is correct
55 Correct 214 ms 5772 KB Output is correct
56 Correct 89 ms 1984 KB Output is correct
57 Correct 95 ms 2128 KB Output is correct
58 Correct 166 ms 4992 KB Output is correct
59 Correct 180 ms 4936 KB Output is correct
60 Correct 172 ms 4936 KB Output is correct
61 Correct 173 ms 4928 KB Output is correct
62 Correct 186 ms 4980 KB Output is correct
63 Correct 176 ms 4908 KB Output is correct
64 Correct 313 ms 5268 KB Output is correct
65 Correct 294 ms 5104 KB Output is correct
66 Correct 247 ms 5144 KB Output is correct
67 Correct 13 ms 3256 KB Output is correct
68 Correct 96 ms 5964 KB Output is correct
69 Correct 106 ms 5932 KB Output is correct
70 Correct 157 ms 5976 KB Output is correct
71 Correct 182 ms 6112 KB Output is correct
72 Correct 147 ms 6004 KB Output is correct
73 Correct 154 ms 5976 KB Output is correct
74 Correct 154 ms 5972 KB Output is correct