제출 #1347385

#제출 시각아이디문제언어결과실행 시간메모리
1347385truongthaiduonglaptrinhEvacuation plan (IZhO18_plan)C++20
100 / 100
937 ms32344 KiB
// duonglaptrinh

# include <bits/stdc++.h>

# define pa pair<int, int>
# define fi first
# define se second

using namespace std;

const int N = 2e5+5;

int n, m;
vector<int> way[N], ll;
vector<pa> wway[N];

int t;
pa qu[N];

pa a[N];
int id[N];
priority_queue<pa, vector<pa>, greater<>> q;

void build() {
    for(int i = 1; i <= n; i++) {
        a[i].fi = -1, a[i].se = i;
    }

    for(int x : ll) {
        a[x].fi = 0;
        q.push({0, x});
    }

    while(!q.empty()) {
        pa x = q.top();
        q.pop();
        int u = x.se, w = x.fi;

        if(a[u].fi != w) {
            continue;
        }

        for(pa x : wway[u]) {
            int v = x.fi, ww = x.se + w;
            if(a[v].fi == -1 || a[v].fi > ww) {
                a[v].fi = ww;
                q.push({ww, v});
            }
        }
    }

    sort(a + 1, a + 1 + n, greater<>());

    for(int i = 1; i <= n; i++) {
        id[a[i].se] = i;
    }
}

int par[N];

void build_par() {
    for(int i = 1; i <= n; i++) {
        par[i] = i;
    }
}

int find_par(int u) {
    if(par[u] == u) {
        return par[u];
    }
    par[u] = find_par(par[u]);
    return par[u];
}

bool kethop(int u, int v, bool yes) {
    u = find_par(u), v = find_par(v);
    if(u == v) {
        return 1;
    }
    if(yes) {
        par[v] = u;
    }
    return 0;
}

int L[N], R[N], ANS[N];
queue<pa> qq;

void solve() {
    for(int i = 1; i <= t; i++) {
        q.push({(L[i] + R[i]) / 2, i});
    }

    while(!q.empty()) {
        while(!q.empty()) {
            qq.push(q.top());
            q.pop();
        }
        build_par();
        int d = 1;
        while(!qq.empty()) {
            pa x = qq.front();
            qq.pop();
            int i = x.se, vt = x.fi;
            for(; d <= vt; d++) {
                int u = a[d].se;
                for(int v : way[u]) {
                    if(id[v] <= vt) {
                        kethop(u, v, 1);
                    }
                }
            }
            int u = qu[i].fi, v = qu[i].se;
            if(kethop(u, v, 0)) {
                ANS[i] = a[vt].fi;
                R[i] = vt - 1;
            }
            else {
                L[i] = vt + 1;
            }

            if(L[i] <= R[i]) {
                q.push({(L[i] + R[i]) / 2, i});
            }
        }
    }

    // build_par();

    // int mid = (l + r) / 2;

    // for(int i = 1; i <= mid; i++) {
    //     int u = a[i].se;
    //     for(int v : way[u]) {
    //         if(id[v] <= mid) {
    //             kethop(u, v, 1);
    //         }
    //     }
    // }

    // for(int i : Mid[mid]) {
    //     int u = qu[i].fi, v = qu[i].se;

    //     if(kethop(u, v, 0)) {
    //         ANS[i] = a[mid].fi;
    //         R[i] = mid - 1;
    //     }
    //     else {
    //         L[i] = mid + 1;
    //     }

    //     if(L[i] <= R[i]) {
    //         Mid[(R[i] + L[i]) / 2].push_back(i);
    //     }
    // }

    // if(l <= mid - 1) {
    //     solve(l, mid - 1);
    // }
    // if(mid + 1 <= r) {
    //     solve(mid + 1, r);
    // }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if(fopen("nhap.inp", "r")) {
        freopen("nhap.inp", "r", stdin);
        freopen("nhap.out", "w", stdout);
    }

    cin>>n>>m;

    for(int i = 1; i <= m; i++) {
        int u, v, w;
        cin>>u>>v>>w;

        way[u].push_back(v);
        way[v].push_back(u);

        wway[u].push_back({v, w});
        wway[v].push_back({u, w});
    }

    int k;
    cin>>k;
    for(int i = 1; i <= k; i++) {
        int x;
        cin>>x;
        ll.push_back(x);
    }

    build();

    cin>>t;
    for(int i = 1; i <= t; i++) {
        int u, v;
        cin>>u>>v;
        qu[i] = {u, v};

        L[i] = 1, R[i] = n;
    }

    solve();

    for(int i = 1; i <= t; i++) {
        cout<<ANS[i]<<"\n";
    }

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

plan.cpp: In function 'int main()':
plan.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen("nhap.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |         freopen("nhap.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...