Submission #1081407

# Submission time Handle Problem Language Result Execution time Memory
1081407 2024-08-30T04:13:14 Z Icelast Robot (JOI21_ho_t4) C++17
58 / 100
3000 ms 188908 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], sum[i][it.c]});
            adj2[nn[i][it.c]].push_back({it.v, -it.p});
            adj2[i].push_back({nn[it.v][it.c], sum[it.v][it.c]});
        }
    }
    vector<ll> dist(N+1, INF);
    spfa(1, dist, 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 388 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 5 ms 2140 KB Output is correct
10 Correct 3 ms 2140 KB Output is correct
11 Correct 2 ms 1752 KB Output is correct
12 Correct 2 ms 1628 KB Output is correct
13 Correct 2 ms 1628 KB Output is correct
14 Correct 2 ms 1628 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 4 ms 1884 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 62824 KB Output is correct
2 Correct 60 ms 31568 KB Output is correct
3 Correct 135 ms 70780 KB Output is correct
4 Correct 112 ms 44112 KB Output is correct
5 Correct 732 ms 188908 KB Output is correct
6 Correct 696 ms 172612 KB Output is correct
7 Correct 273 ms 136432 KB Output is correct
8 Correct 446 ms 146204 KB Output is correct
9 Correct 383 ms 146256 KB Output is correct
10 Correct 53 ms 13460 KB Output is correct
11 Correct 47 ms 10752 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 388 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 5 ms 2140 KB Output is correct
10 Correct 3 ms 2140 KB Output is correct
11 Correct 2 ms 1752 KB Output is correct
12 Correct 2 ms 1628 KB Output is correct
13 Correct 2 ms 1628 KB Output is correct
14 Correct 2 ms 1628 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 4 ms 1884 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1628 KB Output is correct
21 Correct 143 ms 62824 KB Output is correct
22 Correct 60 ms 31568 KB Output is correct
23 Correct 135 ms 70780 KB Output is correct
24 Correct 112 ms 44112 KB Output is correct
25 Correct 732 ms 188908 KB Output is correct
26 Correct 696 ms 172612 KB Output is correct
27 Correct 273 ms 136432 KB Output is correct
28 Correct 446 ms 146204 KB Output is correct
29 Correct 383 ms 146256 KB Output is correct
30 Correct 53 ms 13460 KB Output is correct
31 Correct 47 ms 10752 KB Output is correct
32 Correct 116 ms 63020 KB Output is correct
33 Correct 141 ms 60932 KB Output is correct
34 Correct 452 ms 108684 KB Output is correct
35 Correct 274 ms 87564 KB Output is correct
36 Correct 229 ms 111800 KB Output is correct
37 Correct 314 ms 126236 KB Output is correct
38 Correct 259 ms 136348 KB Output is correct
39 Correct 781 ms 80468 KB Output is correct
40 Execution timed out 3052 ms 147420 KB Time limit exceeded
41 Halted 0 ms 0 KB -