Submission #409483

#TimeUsernameProblemLanguageResultExecution timeMemory
409483534351Robot (JOI21_ho_t4)C++17
0 / 100
161 ms23524 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

const long long LLINF = 3e18;
const int MAXN = 100013;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M;
map<int, vpi> edge[MAXN];
//you can only save it if its color is unique or its color is twice and you just used one of them & paid.
ll dist[MAXN];
map<int, int> special[MAXN];
priority_queue<array<ll, 3>, vector<array<ll, 3> >, greater<array<ll, 3> > > pq;
ll ans;
//distance, vertex, color

vpi conv(vpi v)
{
    ll sum = 0;
    for (pii p : v)
    {
        sum += p.se;
    }
    for (pii &p : v)
    {
        ckmin(p.se, sum - p.se);
    }
    return v;
}

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N >> M;
    FOR(i, 0, M)
    {
        int u, v, c, d;
        cin >> u >> v >> c >> d; u--; v--;
        edge[u][c].PB({v, d});
        edge[v][c].PB({u, d});
        // edge[u].PB({v, c, d});
        // edge[v].PB({u, c, d});
    }
    FOR(u, 0, N)
    {
        for (auto &p : edge[u])
        {
            p.se = conv(p.se);
        }
    }
    // FOR(u, 0, N)
    // {
    //     for (auto p : edge[u])
    //     {
    //         for (auto e : p.se)
    //         {
    //             cerr << "EDGE " << u << ' ' << e.fi << ' ' << " color " << p.fi << " dis " << e.se << endl;
    //         }
    //     }
    // }
    fill(dist, dist + N, LLINF);
    dist[0] = 0;
    pq.push({0, 0, 0});
    while(!pq.empty())
    {
        ll d = pq.top()[0]; int u = pq.top()[1], c = pq.top()[2];
        pq.pop();
        if (c != 0)
        {
            if (d != special[u][c]) continue;
            if (SZ(edge[u][c]) == 2)
            {
                for (auto p : edge[u][c])
                {
                    int v = p.fi;
                    if (dist[v] > d)
                    {
                        dist[v] = d;
                        pq.push({d, v, 0});
                    }
                }
            }
        }
        else
        {
            if (d != dist[u]) continue;
            for (auto p : edge[u])
            {
                int cl = p.fi;
                for (auto e : p.se)
                {
                    int v = e.fi, dis = e.se;
                    //no guarantees -> you pay iff this color is unique.
                    //first set the general distance
                    ll nd = d + dis;
                    // ll nd = d + dis;
                    if (dist[v] > nd)
                    {
                        dist[v] = nd;
                        pq.push({nd, v, 0});
                    }
                    //but also set the special distance
                    nd = d + dis;
                    if (special[v].find(cl) == special[v].end() || special[v][cl] > nd)
                    {
                        special[v][cl] = nd;
                        pq.push({nd, v, cl});
                    }
                }
            }
        }
    }
    ans = dist[N - 1];
    if (ans >= LLINF) ans = -1;
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...