제출 #696947

#제출 시각아이디문제언어결과실행 시간메모리
696947browntoadRobot (JOI21_ho_t4)C++14
100 / 100
1080 ms99328 KiB
#include <bits/stdc++.h>
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 pii pair<int, int>
#define ppi pair<int, pii>
#define f first
#define s second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())

// #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 maxn = 1e5+5;
const ll maxm = 2e5+5;
 
map<pii, bool> occ; // the special dp[i][j] state (if it is alr determined or not)
vector<int> dpv(maxn);
priority_queue<ppi, vector<ppi>, greater<ppi> > pq;
map<pii, int> vord;
map<pii, int> colcnt; 
 
struct edge{
    int to, c, p;
    int idc; // for that node, the rank of the color
    bool operator <(edge b){
        return c<b.c;
    }
};
vector<edge> e[maxn];
vector<vector<int> > ec;
int n, m;
 
 
signed main(){
    IOS();
    cin>>n>>m;
    REP(i, m){
        int u, v, c, p; cin>>u>>v>>c>>p;
        e[u].pb({v, c, p, 0});
        e[v].pb({u, c, p, 0});
    }
    REP1(i, n){
        sort(ALL(e[i]));
        vector<int> tmp;
        REP(j, SZ(e[i])){
            tmp.pb(j);
            if (j==SZ(e[i])-1 ||e[i][j+1].c!=e[i][j].c){
                vord[{i, e[i][j].c}]=SZ(ec);
                ec.pb(tmp);
                tmp.clear();
            }
            colcnt[{i, e[i][j].c}]+=e[i][j].p;
        }
    }
    // compute dp
    pq.push({0, {1, 0}});
    int ans = -1;
    while(pq.size()){
        ppi tmp = pq.top(); pq.pop();
        if (occ.count(tmp.s)) continue;
        // cout<<tmp.f<<' '<<tmp.s.f<<' '<<tmp.s.s<<endl;
        occ[tmp.s]=1;
        if (tmp.s.f == n && tmp.s.s == 0){
            ans=tmp.f;
            break;
        }
        if (tmp.s.s == 0){
            REP(i, SZ(e[tmp.s.f])){
                edge tx = e[tmp.s.f][i];
                if (!occ.count({tx.to, tx.c})){
                    pq.push({tmp.f, {tx.to, tx.c}});
                }
                if (!occ.count({tx.to, 0})){
                    // cout<<tmp.s.f<<' '<<tx.c<<' '<<colcnt[{tmp.s.f, tx.c}]<<endl;
                    pq.push({min(tx.p+tmp.f, colcnt[{tmp.s.f, tx.c}]-tx.p+tmp.f), {tx.to, 0}});
                }
 
            }
        }
        else{
            int poop = vord[tmp.s];
            REP(i, SZ(ec[poop])){
                int tid = ec[poop][i];
                edge tx = e[tmp.s.f][tid];
                if (!occ.count({tx.to, 0})){
                    pq.push({colcnt[{tmp.s.f, tx.c}]-tx.p+tmp.f, {tx.to, 0}});
                }
            }
        }
    }
    cout<<ans<<endl;
 
 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...