Submission #703770

# Submission time Handle Problem Language Result Execution time Memory
703770 2023-02-28T10:05:20 Z browntoad Olympic Bus (JOI20_ho_t4) C++14
100 / 100
173 ms 4524 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
// #define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pip pair<int, pii>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif

//const ll inf = 1ll<<60;
const int inf=2147483647;
const ll mod = 1e9+7;
const ll maxn=205;
const ll maxm=5e4+5;
const double PI=acos(-1);

ll pw(ll x, ll p, ll m=mod){
    ll ret=1;
    while (p>0){
        if (p&1){
            ret*=x;
            ret%=m;
        }
        x*=x;
        x%=m;
        p>>=1;
    }
    return ret;
}

ll inv(ll a, ll m=mod){
    return pw(a,m-2,m);
}
struct edge{
    int to, cap, rev;
    int id;
};
struct inedge{
    int u, v, c, d;
    int id;
};
bool operator <(pip a, pip b){
    return a.f<b.f;
}
int n, m;
vector<edge> graph[maxn];
int dis[maxn][4], preev[maxn][4];
int dd[maxn][maxn];
bool occ[maxm][4];
vector<inedge> e(maxm);
vector<int> cores;
void bck(bool typ){
    int cur = cores[typ^1];
    if (dis[cur][typ] == inf) return;
    while(cur != cores[typ]){
        occ[preev[cur][typ]][typ]=1;
        cur=e[preev[cur][typ]].u;
    }
}
void dij(int typ){
    priority_queue<pip, vector<pip>, greater<pip> > pq;
    if (typ%2 == 0) pq.push({0, {1, -1}});
    else pq.push({0, {n, -1}});
    while(pq.size()){
        pip x=pq.top(); pq.pop();
        if (dis[x.s.f][typ] != inf) continue;
        preev[x.s.f][typ] = x.s.s;
        dis[x.s.f][typ] = x.f;
        REP(i, SZ(graph[x.s.f])){
            if (dis[graph[x.s.f][i].to][typ] == inf){
                pq.push({x.f+graph[x.s.f][i].cap, {graph[x.s.f][i].to, graph[x.s.f][i].id}});
            }
        }
    }
    bck(typ);
}
void dij2(int typ, int bn){ // bn is banned index
    dis[cores[typ%2]][typ] = 0;
    REP1(i, n){
        pii cmn = {inf, -1};
        REP1(j, n){
            if (dis[j][typ] < cmn.f && !occ[j][typ]){
                cmn.f=dis[j][typ];
                cmn.s=j;
            }
        }
        if (cmn.s == -1) break;
        occ[cmn.s][typ]=1;
        if (cmn.s == e[bn].v) dis[e[bn].u][typ]=min(dis[e[bn].u][typ], dis[cmn.s][typ]+e[bn].c);
        REP(j, SZ(graph[cmn.s])){
            if (graph[cmn.s][j].id != bn && occ[graph[cmn.s][j].to][typ] == 0){
                dis[graph[cmn.s][j].to][typ] = min(dis[graph[cmn.s][j].to][typ], dis[cmn.s][typ]+graph[cmn.s][j].cap);
            }
        }
    }
}
void floy(){
    REP1(i, n) REP1(j, n) dd[i][j] = ((i==j)?0:inf);
    REP(i, m){
        dd[e[i].u][e[i].v]=min(dd[e[i].u][e[i].v], e[i].c);
    }
    REP1(k, n){
        REP1(i, n){
            REP1(j, n){
                if (dd[i][k] == inf || dd[k][j] == inf) continue;
                dd[i][j] = min(dd[i][j], dd[i][k]+dd[k][j]);
            }
        }
    }
}
int ret;
void solve(){
    cores={1, n};
    floy();
    dij(0);
    dij(1);
    if (dd[1][n] == inf || dd[n][1] == inf) ret=inf;
    else ret = dd[1][n]+dd[n][1];
    REP(i, m){
        int m0, m1;
        if (!occ[i][0] && !occ[i][1]){
            m0 = dd[1][n];
            if (dd[1][e[i].v] != inf && dd[e[i].u][n] != inf) m0=min(m0, dd[1][e[i].v]+e[i].c+dd[e[i].u][n]);
            m1 = dd[n][1];
            if (dd[n][e[i].v] != inf && dd[e[i].u][1] != inf) m1=min(m1, dd[n][e[i].v]+e[i].c+dd[e[i].u][1]);
            if (m0!=inf && m1!=inf) ret = min(ret, m0+m1+e[i].d);
            continue;
        }
        REP1(j, n){
            dis[j][2]=dis[j][3]=inf;
            occ[j][2] = occ[j][3] = 0;
        }
        dij2(2, i);
        dij2(3, i);
        if (dis[n][2]!=inf && dis[1][3]!=inf) ret = min(ret, e[i].d+dis[n][2]+dis[1][3]);
    }
    if (ret>=inf) ret=-1;
}
signed main (){
    IOS();
    cin>>n>>m;
    REP1(i, n){
        dis[i][0]=dis[i][1]=inf;
    }
    REP(i, m){
        cin>>e[i].u>>e[i].v>>e[i].c>>e[i].d;
        e[i].id=i;
        graph[e[i].u].pb({e[i].v, e[i].c, e[i].d, i});
    }
    solve();
    cout<<ret<<endl;
}
/*
test 1
output: 26
4 7
1 2 100 1000
2 3 21 2
3 4 100 1000
1 3 1 1000
2 4 1 1000
1 4 101 1000
4 1 1 1000

test 2
output: 16
4 6
1 4 10 1000
2 3 1 1
1 2 2 1000
3 4 2 1000
4 3 2 1000
2 1 2 1000
*/
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 8 ms 1468 KB Output is correct
4 Correct 9 ms 1492 KB Output is correct
5 Correct 2 ms 1364 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1364 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 31 ms 1492 KB Output is correct
11 Correct 37 ms 1492 KB Output is correct
12 Correct 35 ms 1492 KB Output is correct
13 Correct 4 ms 1488 KB Output is correct
14 Correct 5 ms 1492 KB Output is correct
15 Correct 5 ms 1472 KB Output is correct
16 Correct 5 ms 1468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3720 KB Output is correct
2 Correct 37 ms 3712 KB Output is correct
3 Correct 38 ms 3796 KB Output is correct
4 Correct 8 ms 1620 KB Output is correct
5 Correct 6 ms 1480 KB Output is correct
6 Correct 2 ms 1472 KB Output is correct
7 Correct 1 ms 1460 KB Output is correct
8 Correct 1 ms 1236 KB Output is correct
9 Correct 28 ms 3320 KB Output is correct
10 Correct 26 ms 3160 KB Output is correct
11 Correct 34 ms 3556 KB Output is correct
12 Correct 31 ms 3612 KB Output is correct
13 Correct 31 ms 3612 KB Output is correct
14 Correct 29 ms 3700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1492 KB Output is correct
2 Correct 2 ms 1492 KB Output is correct
3 Correct 33 ms 2972 KB Output is correct
4 Correct 2 ms 1492 KB Output is correct
5 Correct 38 ms 3368 KB Output is correct
6 Correct 1 ms 1236 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 26 ms 3168 KB Output is correct
9 Correct 25 ms 3152 KB Output is correct
10 Correct 37 ms 3304 KB Output is correct
11 Correct 32 ms 3324 KB Output is correct
12 Correct 30 ms 3396 KB Output is correct
13 Correct 1 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
15 Correct 1 ms 1328 KB Output is correct
16 Correct 1 ms 1236 KB Output is correct
17 Correct 1 ms 1236 KB Output is correct
18 Correct 1 ms 1236 KB Output is correct
19 Correct 35 ms 3404 KB Output is correct
20 Correct 36 ms 3348 KB Output is correct
21 Correct 30 ms 3296 KB Output is correct
22 Correct 31 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 8 ms 1468 KB Output is correct
4 Correct 9 ms 1492 KB Output is correct
5 Correct 2 ms 1364 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 1 ms 1364 KB Output is correct
9 Correct 2 ms 1364 KB Output is correct
10 Correct 31 ms 1492 KB Output is correct
11 Correct 37 ms 1492 KB Output is correct
12 Correct 35 ms 1492 KB Output is correct
13 Correct 4 ms 1488 KB Output is correct
14 Correct 5 ms 1492 KB Output is correct
15 Correct 5 ms 1472 KB Output is correct
16 Correct 5 ms 1468 KB Output is correct
17 Correct 42 ms 3720 KB Output is correct
18 Correct 37 ms 3712 KB Output is correct
19 Correct 38 ms 3796 KB Output is correct
20 Correct 8 ms 1620 KB Output is correct
21 Correct 6 ms 1480 KB Output is correct
22 Correct 2 ms 1472 KB Output is correct
23 Correct 1 ms 1460 KB Output is correct
24 Correct 1 ms 1236 KB Output is correct
25 Correct 28 ms 3320 KB Output is correct
26 Correct 26 ms 3160 KB Output is correct
27 Correct 34 ms 3556 KB Output is correct
28 Correct 31 ms 3612 KB Output is correct
29 Correct 31 ms 3612 KB Output is correct
30 Correct 29 ms 3700 KB Output is correct
31 Correct 9 ms 1492 KB Output is correct
32 Correct 2 ms 1492 KB Output is correct
33 Correct 33 ms 2972 KB Output is correct
34 Correct 2 ms 1492 KB Output is correct
35 Correct 38 ms 3368 KB Output is correct
36 Correct 1 ms 1236 KB Output is correct
37 Correct 1 ms 1236 KB Output is correct
38 Correct 26 ms 3168 KB Output is correct
39 Correct 25 ms 3152 KB Output is correct
40 Correct 37 ms 3304 KB Output is correct
41 Correct 32 ms 3324 KB Output is correct
42 Correct 30 ms 3396 KB Output is correct
43 Correct 1 ms 1236 KB Output is correct
44 Correct 1 ms 1236 KB Output is correct
45 Correct 1 ms 1328 KB Output is correct
46 Correct 1 ms 1236 KB Output is correct
47 Correct 1 ms 1236 KB Output is correct
48 Correct 1 ms 1236 KB Output is correct
49 Correct 35 ms 3404 KB Output is correct
50 Correct 36 ms 3348 KB Output is correct
51 Correct 30 ms 3296 KB Output is correct
52 Correct 31 ms 3284 KB Output is correct
53 Correct 37 ms 3656 KB Output is correct
54 Correct 41 ms 3632 KB Output is correct
55 Correct 38 ms 3656 KB Output is correct
56 Correct 8 ms 1468 KB Output is correct
57 Correct 8 ms 1492 KB Output is correct
58 Correct 121 ms 3144 KB Output is correct
59 Correct 134 ms 3888 KB Output is correct
60 Correct 173 ms 3756 KB Output is correct
61 Correct 118 ms 3668 KB Output is correct
62 Correct 130 ms 3776 KB Output is correct
63 Correct 171 ms 3728 KB Output is correct
64 Correct 91 ms 3820 KB Output is correct
65 Correct 84 ms 3868 KB Output is correct
66 Correct 100 ms 3924 KB Output is correct
67 Correct 19 ms 3920 KB Output is correct
68 Correct 29 ms 4168 KB Output is correct
69 Correct 26 ms 4048 KB Output is correct
70 Correct 33 ms 4284 KB Output is correct
71 Correct 30 ms 4416 KB Output is correct
72 Correct 30 ms 4468 KB Output is correct
73 Correct 31 ms 4524 KB Output is correct
74 Correct 31 ms 4320 KB Output is correct