Submission #1294930

#TimeUsernameProblemLanguageResultExecution timeMemory
1294930beheshtRobot (JOI21_ho_t4)C++20
0 / 100
555 ms22568 KiB
#include <bits/stdc++.h>

#define d1(x)                cout << #x << " : " << x << endl << flush
#define d2(x, y)             cout << #x << " : " << x << "   " << #y << " : " << y << endl << flush
#define d3(x, y, z)          cout << #x << " : " << x << "   " << #y << " : " << y << "   " << #z << " : " << z << endl << flush
#define d4(x, y, z, a)       cout << #x << " : " << x << "   " << #y << " : " << y << "   " << #z << " : " << z << "    "<< #a << " : " << a << endl << flush
#define arr(x)               array <ll, x>
#define ld                   long double
#define ll                   long long
#define int                  long long
#define pb                   push_back
#define endl                 '\n'
#define lc                   v << 1
#define rc                   v << 1 | 1

using namespace std;

const int INF = 1e14 + (35 / 10); // 35 ---> 36
const int MAXN = 2e5 + (35 / 10); // 35 ---> 36

vector <arr(3)> adj[MAXN];
map <arr(2), int> dp;
map <arr(2), int> mp;

signed main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, m;
    cin >> n >> m;

    for(int i = 1; i < n; i++)
        dp[{i, 0}] = INF;

    for(int i = 1; i <= m; i++){
        int u, v, c, p;
        cin >> u >> v >> c >> p;

        u--;
        v--;

        adj[u].pb({v, c, p});
        adj[v].pb({u, c, p});

        mp[{u, c}] += p;
        mp[{v, c}] += p;

        dp[{u, c}] = dp[{v, c}] = INF;
    }
    
    dp[{0, 0}] = 0;

    set <arr(6)> s;
    s.insert({0, 0, 0, -1, -INF, 0});

    while(!s.empty()){
        auto [w, u, cool, eli, pp, E] = *s.begin();
        s.erase(s.begin());

        // d4(w, u, cool, C[id]);

        for(int i = 0; i < adj[u].size(); i++){

            auto [v, c, p] = adj[u][i];
            
            if(dp[{v, 0}] > w + p){
                dp[{v, 0}] = w + p;
                s.insert({dp[{v, 0}], v, 0, c, p, u});
            }

            if(cool != c){
                int cost = mp[{u, c}] - p;

                if(cool == 0 && eli == c && E != v)
                    cost -= pp;

                // d1(cost);
                if(dp[{v, c}] > w + cost){
                    dp[{v, c}] = w + cost;
                    s.insert({dp[{v, c}], v, c, -1, -1, u});
                }
            }
            // d3(i, u, v);
            // d1(2);
        }

        // d3(w, u, eli);
        // cout << endl;
    }

    // cout << mp[{1, 3}] << " " <<  mp[{3, 3}] << endl;
    int ans = INF;

    for(auto [v, c, p] : adj[n - 1])
        ans = min(ans, dp[{n - 1, c}]);

    if(ans == INF)
        ans = -1;
    cout << ans << endl;
}

// Ey To Bahane! :_)))

// -------------<3------------- //
/*
Magasan dor shirini: 

1. MAXN
2. Input style
3. index or value? Masale In Ast!	
4. MOD 

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...