Submission #1161712

#TimeUsernameProblemLanguageResultExecution timeMemory
1161712tw20000807Robot (JOI21_ho_t4)C++20
0 / 100
105 ms30232 KiB
#include<bits/stdc++.h>
#define int long long
#pragma GCC optimize("O3")
#define all(v) v.begin(), v.end()
#define SZ(x) (int)x.size()
#define pii pair<int, int>
#define X first
#define Y second

using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;//    998244353;
const int llmx = 1e18;


void sol(){
    int n, m;
    cin >> n >> m;
    vector< vector< array<int, 3> > > g(n + 1);
    vector< vector< pii > > cnt(n + 1);

    vector< int > dis(2 * m + n + 1, llmx);
    vector< vector< pii > > num(n + 1);
    vector< int > base(n + 1);
    while(m--){
        int a, b, c, p;
        cin >> a >> b >> c >> p;
        cnt[a].push_back({c, 0});
        cnt[b].push_back({c, 0});

        num[a].push_back({p, c});
        num[b].push_back({p, c});

        g[a].push_back({b, c, p});
        g[b].push_back({a, c, p});
    }
    for(int i = 1; i <= n; ++i){
        sort(all(cnt[i]));
        cnt[i].resize(unique(all(cnt[i])) - cnt[i].begin());
        num[i].push_back({0, 0});
        sort(all(num[i]));
        num[i].resize(unique(all(num[i])) - num[i].begin());
        base[i] = base[i - 1] + SZ(num[i - 1]);
        for(auto &[nxt, c, p] : g[i]){
            auto it = lower_bound(all(cnt[i]), pii(c, 0));
            it->Y += p;
        }
    }
    auto get = [&](int id, int p, int c) -> int {
        return base[id] + lower_bound(all(num[id]), pii(p, c)) - num[id].begin();
    };

    deque< array<int, 5> > pq;

    dis[get(1, 0, 0)] = 0;
    pq.push_back({0, 1, 0, 0, get(1, 0, 0)});
    while(!pq.empty()){
        auto [D, cur, cost, col, nid] = pq.front(); pq.pop_front();
        if(dis[nid] != D) continue;
        if(cur == n){
            cout << D << "\n";
            return;
        }
        for(auto &[nxt, c, p] : g[cur]){
            int id1 = get(nxt, p, c);
            int id2 = get(nxt, 0, 0);
            int al = lower_bound(all(cnt[cur]), pii(c, 0))->Y;

            int wei = max(0LL, al - p - (c == col ? cost : 0));
            if(dis[id2] > D + wei){
                dis[id2] = D + wei;
                if(wei == 0) pq.push_front({dis[id2], nxt, 0, 0, id2});
                else pq.push_back({dis[id2], nxt, 0, 0, id2});
            }
            if(dis[id1] > D + p){
                dis[id1] = D + p;
                pq.push_back({dis[id1], nxt, p, c, id1});
            }
        }
    }
    cout << "-1\n";
}
/*
4 6
1 4 4 4
3 4 1 3
1 3 4 4
2 4 3 1
2 3 3 2
1 2 4 2

// 3

5 2
1 4 1 2
3 5 1 4
// -1


5 7
2 3 7 1
1 4 5 1
4 5 3 1
3 4 7 1
2 4 3 1
3 5 6 1
1 2 5 1
// 1

13 21
7 10 4 4
3 6 4 7
8 10 4 5
3 9 2 5
1 4 4 5
2 6 4 2
3 11 2 2
3 8 16 2
8 11 16 1
6 10 4 14
6 8 16 6
9 12 16 5
5 13 4 6
1 12 4 7
2 4 4 18
2 9 4 10
2 12 4 6
10 13 4 28
5 7 2 5
5 11 2 16
7 13 4 20

// 7


*/
signed main(){
    ios::sync_with_stdio(0), cin.tie(0), cerr.tie(0);
    int t = 1; //cin >> t;
    while(t--) sol();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...