Submission #495114

#TimeUsernameProblemLanguageResultExecution timeMemory
495114kinglineEvacuation plan (IZhO18_plan)C++17
12 / 100
1049 ms54136 KiB
/*#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <bits/stdc++.h>
#define file(data) freopen(data".in", "r", stdin); freopen(data".out", "w", stdout);
#define pb push_back
#define all(data) data.begin(), data.end()
#define endl '\n'
#define ll long long
//#define int long long

using namespace std;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;

int n, m, q, k, s[N], t[N], d[N], dang[N];
bool used[N];
vector < pair < int, int > > g[N];

void djk() {
    for(int i = 1; i <= n; i++) d[i] = mod;
    set < pair < int, int > > st;
    for(int i = 1; i <= k; i++) {
        d[dang[i]] = 0;
        st.insert({0, dang[i]});
    }
    while(st.size()) {
        int now = (*st.begin()).second;
        st.erase(*st.begin());
        for(int i = 0; i < g[now].size(); i++) {
            int to = g[now][i].first, len = g[now][i].second;
            if(d[to] > d[now] + len) {
                st.erase({d[to], to});
                d[to] = d[now] + len;
                st.insert({d[to], to});
            }
        }
    }
}

int slow(int v, int fn) {
    set < pair < int, int > > st;
    st.insert({d[v], v});
    int res = d[v];
    while(st.size()) {
        int now = (*st.rbegin()).second;
        used[now] = 1;
        res = min(res, d[now]);
        if(now == fn) return res;
        st.erase(*st.rbegin());
        for(int i = 0; i < g[now].size(); i++) {
            int to = g[now][i].first;
            if(!used[to]) {
                st.insert({d[to], to});
            }
        }
    }
}

main() {
    //file("snowcow");
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    set < int > st[n + 1];
    for(int i = 1; i <= m; i++) {
        int a, b, len; cin >> a >> b >> len;
        g[a].pb({b, len});
        g[b].pb({a, len});
        if(a > b) swap(a, b);
        st[a].insert(b);
    }
    cin >> k;
    for(int i = 1; i <= k; i++) {
        cin >> dang[i];
    }
    djk();
    cin >> q;
    bool ok = 1;
    for(int i = 1; i <= q; i++) {
        cin >> s[i] >> t[i];
        if(s[i] > t[i]) swap(s[i], t[i]);
        if(!st[s[i]].count(t[i])) ok = 0;
    }
    if(ok) {
        for(int i = 1; i <= q; i++) {
            cout << min(s[i], t[i]) << endl;
        }
        return 0;
    }
    for(int i = 1; i <= q; i++) {
        for(int j = 0; j <= n; j++) used[j] = 0;
        cout << slow(s[i], t[i]) << endl;
    }
}






Compilation message (stderr)

plan.cpp: In function 'void djk()':
plan.cpp:33:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         for(int i = 0; i < g[now].size(); i++) {
      |                        ~~^~~~~~~~~~~~~~~
plan.cpp: In function 'int slow(int, int)':
plan.cpp:54:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         for(int i = 0; i < g[now].size(); i++) {
      |                        ~~^~~~~~~~~~~~~~~
plan.cpp: At global scope:
plan.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   63 | main() {
      | ^~~~
plan.cpp: In function 'int slow(int, int)':
plan.cpp:45:31: warning: control reaches end of non-void function [-Wreturn-type]
   45 |     set < pair < int, int > > st;
      |                               ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...