Submission #1327961

#TimeUsernameProblemLanguageResultExecution timeMemory
1327961dex111222333444555Robot (JOI21_ho_t4)C++20
100 / 100
666 ms89812 KiB
#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) (int)(v).size()
#define int long long

using namespace std;

template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}

const int MAXN = 1e5 + 5, MAXM = 2e5 + 5, mod = 1e9 + 7;
const long long inf = 1e18 + 134;

struct E{
    int u, v, c, p;

    E(int _u = 0, int _v = 0, int _c = 0, int _p = 0):
        u(_u), v(_v), c(_c), p(_p) {};

    int other(int x){
        return u ^ v ^ x;
    }
};

struct S{
    long long len;
    int u, w;

    S(long long _len = 0, int _u = 0, int _w = 0):
        len(_len), u(_u), w(_w) {};

    bool operator<(const S &other) const{
        return len > other.len;
    }
};

int numNode, numEdge;
map<int, vector<int> > adj[MAXN];
map<int, int> sum[MAXN];
map<int, long long > dist[MAXN];
E edge[MAXM];

void input(){
    cin >> numNode >> numEdge;
    for(int i = 1; i <= numEdge; i++){
        int u, v, c, p; cin >> u >> v >> c >> p;
        adj[u][c].push_back(i);
        adj[v][c].push_back(i);
        sum[u][c] += p;
        sum[v][c] += p;
        edge[i] = E(u, v, c, p);
    }
}

void dijsktra(){
    priority_queue<S> q;
    q.emplace(dist[1][0] = 0, 1, 0);

    while(siz(q)){
        long long len = q.top().len;
        int u = q.top().u, w = q.top().w;
        q.pop();

        if (len > dist[u][w]) continue;

        if (w == 0){
            for(const pair<int, vector<int> > tmp: adj[u]){
                int nw = tmp.fi;
                for(int id: tmp.se){
                    int v = edge[id].other(u);

                    if (!dist[v].count(0)) dist[v][0] = inf;

                    // change color this edge
                    if (minimize(dist[v][0], len + edge[id].p))
                        q.emplace(dist[v][0], v, 0);


                    // change color of all the edge adjacent to this edge that have the same color
                    if (minimize(dist[v][0], len + sum[u][nw] - edge[id].p))
                        q.emplace(dist[v][0], v, 0);

                    if (!dist[v].count(nw)) dist[v][nw] = inf;

                    // change state for edge case
                    if (minimize(dist[v][nw], len))
                        q.emplace(dist[v][nw], v, nw);
                }
            }
        }else{
            // edge case
            for(int id: adj[u][w]){
                int v = edge[id].other(u);

                if (!dist[v].count(0)) dist[v][0] = inf;

                if (minimize(dist[v][0], len + sum[u][w] - edge[id].p))
                    q.emplace(dist[v][0], v, 0);
            }
        }
    }
}

void solve(){
    dijsktra();
    if (!dist[numNode].count(0)) cout << -1 << '\n';
    else cout << dist[numNode][0] << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int t = 1;
//    cin >> t;
    while(t--){
        input();
        solve();
    }
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...