Submission #628070

# Submission time Handle Problem Language Result Execution time Memory
628070 2022-08-13T04:35:14 Z nttt Toll (BOI17_toll) C++14
0 / 100
3000 ms 20460 KB
#include<bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<ll, int>
#define all(x) (x).begin(), (x).end()
#define task "name"

const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 5e5 + 3;
const int M = 1e5 + 9;
const int BASE = 10;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}

struct query
{
    int u, v, id;
    bool operator < (const query &a) const
    {
        return u < a.u;
    }
} que[M];
int k, n, m, q;
vector<pii> adj[N];
ll d[N];
ll ans[M];
void dijkstra(int s)
{
    memset(d, 0x3f, sizeof d);
    priority_queue<pii, vector<pii>, greater<pii>> kew;
    kew.push(mp(0, s));
    d[s] = 0;

    while(!kew.empty())
    {
        int u = kew.top().se;
        ll du = kew.top().fi;
        kew.pop();
        for(pii i : adj[u])
        {
            int v = i.se;
            ll uv = i.fi;
            if(minimize(d[v], du + uv))
            {
                kew.push(mp(d[v], v));
            }
        }
    }
}
void Solve()
{
    cin >> k >> n >> m >> q;
    for(int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].pb(mp(w, v));
        adj[v].pb(mp(w, u));
    }
    for(int i = 1; i <= q; i++)
    {
        int u, v;
        cin >> u >> v;
        if(u > v) swap(u, v);
        que[i] = {u, v, i};
    }
    sort(que + 1, que + 1 + q);
    int s = -1;
    for(int i = 1; i <= q; i++)
    {
        if(que[i].u != s)
        {
            s = que[i].u;
            dijkstra(s);
        }
        ans[que[i].id] = d[que[i].v];
    }
    for(int i = 1; i <= q; i++)
    {
        if(ans[i] < loo) cout << ans[i] << '\n';
        else cout << -1 << '\n';
    }
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen(task".inp", "r", stdin);
//    freopen(task".out", "w", stdout);
    int T = 1;
    //cin >> T;
    while(T--)
    {
        Solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 3101 ms 18764 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 46 ms 20460 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15956 KB Output is correct
2 Incorrect 10 ms 15956 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15956 KB Output is correct
2 Incorrect 10 ms 15956 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3101 ms 18764 KB Time limit exceeded
2 Halted 0 ms 0 KB -