Submission #1246241

#TimeUsernameProblemLanguageResultExecution timeMemory
1246241GeforgsOlympic Bus (JOI20_ho_t4)C++20
0 / 100
1092 ms5192 KiB
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bit>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;

void Dijkstra(ll start, vector<vector<pair<pair<ll, ll>, ll>>>& a, vector<ll>& dis, vector<ll>& prev){
    set<pair<ll, ll>> s;
    dis[start] = 0;
    s.insert({0, start});
    while(!s.empty()){
        auto [val, x] = *s.begin();
        s.erase(s.begin());
        for(auto [el, id]: a[x]){
            auto [y, w] = el;
            if(dis[y] > val + w){
                s.erase({dis[y], y});
                dis[y] = val + w;
                prev[y] = id;
                s.insert({dis[y], y});
            }
        }
    }
}

void Dijkstra(ll start, vector<vector<pair<ll, ll>>>& a, vector<ll>& dis){
    set<pair<ll, ll>> s;
    dis[start] = 0;
    s.insert({0, start});
    while(!s.empty()){
        auto [val, x] = *s.begin();
        s.erase(s.begin());
        for(auto [y, w]: a[x]){
            if(dis[y] > val + w){
                s.erase({dis[y], y});
                dis[y] = val + w;
                s.insert({dis[y], y});
            }
        }
    }
}

ll Dijkstra(ll n, ll start, ll finish, ll id, vector<pair<pair<ll, ll>, pair<ll, ll>>>& c){
    vector<vector<ll>> a(n, vector<ll>(n, inf));
    for(int i=0;i<c.size();++i){
        if(i == id) continue;
        a[c[i].first.first][c[i].first.second] = min(a[c[i].first.first][c[i].first.second], c[i].second.first);
    }
    vector<ll> dis(n, inf);
    vector<bool> visited(n, false);
    dis[start] = 0;
    for(int i=0;i<n;++i){
        ll x=-1, val=inf;
        for(int j=0;j<n;++j){
            if(dis[j] < val and !visited[j]){
                x = j;
                val = dis[j];
            }
        }
        if(x == -1) break;
        visited[x] = true;
        for(int j=0;j<n;++j){
            if(dis[j] > a[x][j] + val){
                dis[j] = a[x][j] + val;
            }
        }
    }
    return dis[finish];
}

void solve(){
    ll n, m, i, x, y, w, v, res=inf;
    cin>>n>>m;
    vector<vector<pair<pair<ll, ll>, ll>>> a(n);
    vector<vector<pair<ll, ll>>> b(n);
    vector<pair<pair<ll, ll>, pair<ll, ll>>> c(m);
    vector<ll> dis(n, inf);
    vector<ll> prev(n, -1);
    vector<ll> dis2(n, inf);
    vector<ll> prev2(n, -1);
    vector<ll> dis3(n, inf);
    vector<ll> dis4(n, inf);
    for(i=0;i<m;++i){
        cin>>x>>y>>w>>v;
        --x;
        --y;
        c[i] = {{x, y}, {w, v}};
        a[x].pb({{y, w}, i});
        b[y].pb({x, w});
    }
    Dijkstra(0, a, dis, prev);
    Dijkstra(n-1, a, dis2, prev2);
    Dijkstra(0, b, dis3);
    Dijkstra(n-1, b, dis4);
    res = min(res, dis[n-1] + dis2[0]);
    for(i=0;i<m;++i){
        x = c[i].first.first;
        y = c[i].first.second;
        res = min(res, dis[y] + dis4[x] + Dijkstra(n, n-1, 0, i, c) + c[i].second.second + c[i].second.first);
        res = min(res, dis2[y] + dis3[x] + Dijkstra(n, 0, n-1, i, c) + c[i].second.second + c[i].second.first);
    }
    if(res == inf){
        cout<<-1<<endl;
    }else{
        cout<<res<<endl;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    srand(time(nullptr));
    ll t=1;
//    cin>>t;
    for(;t>0;--t){
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...