Submission #1081416

# Submission time Handle Problem Language Result Execution time Memory
1081416 2024-08-30T04:23:41 Z Icelast Robot (JOI21_ho_t4) C++17
100 / 100
748 ms 158956 KB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
vector<ll> dijkstra(int s, vector<vector<pair<ll, ll>>> &adj){
    int n = adj.size();
    vector<ll> dist(n+1, INF);
    dist[s] = 0;
    vector<bool> vis(n+1, false);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push({0, s});
    while(!pq.empty()){
        ll d_u = pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if(vis[u]){
            continue;
        }
        vis[u] = true;
        for(auto it : adj[u]){
            int v = it.first;
            ll w = it.second;
            if(vis[v]) continue;
            if(dist[v] > d_u+w){
                dist[v] = d_u+w;
                pq.push({dist[v], v});
            }
        }
    }
    return dist;
}
bool spfa(int s, vector<ll>& d, vector<vector<pair<ll, ll>>> adj) {
    int n = adj.size();
    d.assign(n+1, INF);
    vector<int> cnt(n+1, 0);
    vector<bool> inqueue(n+1, false);
    queue<int> q;

    d[s] = 0;
    q.push(s);
    inqueue[s] = true;
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        inqueue[v] = false;

        for (auto edge : adj[v]) {
            int to = edge.first;
            ll len = edge.second;

            if (d[v] + len < d[to]) {
                d[to] = d[v] + len;
                if (!inqueue[to]) {
                    q.push(to);
                    inqueue[to] = true;
                    cnt[to]++;
                    if (cnt[to] > n)
                        return false;  // negative cycle
                }
            }
        }
    }
    return true;
}
struct road{
    ll v, c, p;
};
void solve(){
    int n, m;
    cin >> n >> m;
    vector<vector<road>> adj(n+1);
    for(int i = 1; i <= m; i++){
        int u, v, c, p;
        cin >> u >> v >> c >> p;
        adj[u].push_back({v, c, p});
        adj[v].push_back({u, c, p});
    }
    vector<bool> vis(n+1, false);
    auto dfs = [&](auto dfs, int u) -> void{
        vis[u] = true;
        for(auto it : adj[u]){
            int v = it.v;
            if(vis[v]) continue;
            dfs(dfs, v);
        }
    };
    dfs(dfs, 1);
    if(!vis[n]){
        cout << -1;
        return;
    }
    vector<map<int, int>> mp(n+1), nn(n+1);
    vector<map<int, vector<int>>> to(n+1);
    vector<map<ll, ll>> sum(n+1);
    int N = n;
    for(int i = 1; i <= n; i++){
        for(auto it : adj[i]){
            mp[i][it.c]++;
            to[i][it.c].push_back(it.v);
            sum[i][it.c]+=it.p;
            if(nn[i][it.c] == 0){
                N++;
                nn[i][it.c] = N;
            }
        }
    }
    vector<vector<pair<ll, ll>>> adj2(N+1);
    for(int i = 1; i <= n; i++){
        for(auto it : adj[i]){
            adj2[i].push_back({it.v, it.p});
            adj2[i].push_back({nn[i][it.c], 0});
            adj2[nn[i][it.c]].push_back({it.v, sum[i][it.c]-it.p});
            adj2[i].push_back({nn[it.v][it.c], 0});
        }
    }
    vector<ll> dist(N+1, INF);
    dist = dijkstra(1, adj2);
    cout << dist[n];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 4 ms 1952 KB Output is correct
10 Correct 3 ms 1628 KB Output is correct
11 Correct 2 ms 1368 KB Output is correct
12 Correct 2 ms 1368 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1372 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 49156 KB Output is correct
2 Correct 55 ms 25296 KB Output is correct
3 Correct 121 ms 50632 KB Output is correct
4 Correct 91 ms 35012 KB Output is correct
5 Correct 748 ms 153652 KB Output is correct
6 Correct 557 ms 136032 KB Output is correct
7 Correct 235 ms 106912 KB Output is correct
8 Correct 315 ms 109328 KB Output is correct
9 Correct 359 ms 109136 KB Output is correct
10 Correct 46 ms 11088 KB Output is correct
11 Correct 33 ms 9036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 4 ms 1952 KB Output is correct
10 Correct 3 ms 1628 KB Output is correct
11 Correct 2 ms 1368 KB Output is correct
12 Correct 2 ms 1368 KB Output is correct
13 Correct 2 ms 1372 KB Output is correct
14 Correct 2 ms 1372 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 137 ms 49156 KB Output is correct
22 Correct 55 ms 25296 KB Output is correct
23 Correct 121 ms 50632 KB Output is correct
24 Correct 91 ms 35012 KB Output is correct
25 Correct 748 ms 153652 KB Output is correct
26 Correct 557 ms 136032 KB Output is correct
27 Correct 235 ms 106912 KB Output is correct
28 Correct 315 ms 109328 KB Output is correct
29 Correct 359 ms 109136 KB Output is correct
30 Correct 46 ms 11088 KB Output is correct
31 Correct 33 ms 9036 KB Output is correct
32 Correct 102 ms 41932 KB Output is correct
33 Correct 104 ms 44232 KB Output is correct
34 Correct 278 ms 83660 KB Output is correct
35 Correct 201 ms 67532 KB Output is correct
36 Correct 203 ms 88764 KB Output is correct
37 Correct 234 ms 99324 KB Output is correct
38 Correct 244 ms 111776 KB Output is correct
39 Correct 114 ms 54476 KB Output is correct
40 Correct 351 ms 109216 KB Output is correct
41 Correct 364 ms 114680 KB Output is correct
42 Correct 76 ms 21208 KB Output is correct
43 Correct 35 ms 10840 KB Output is correct
44 Correct 226 ms 79308 KB Output is correct
45 Correct 277 ms 100088 KB Output is correct
46 Correct 241 ms 100436 KB Output is correct
47 Correct 231 ms 97208 KB Output is correct
48 Correct 710 ms 158956 KB Output is correct