제출 #703770

#제출 시각아이디문제언어결과실행 시간메모리
703770browntoadOlympic Bus (JOI20_ho_t4)C++14
100 / 100
173 ms4524 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...