제출 #736093

#제출 시각아이디문제언어결과실행 시간메모리
736093nguyentunglamPassport (JOI23_passport)C++17
48 / 100
34 ms22920 KiB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int f[N], g[N], h[N];
vector<int> adj[N];
void bfs(int start, int d[]) {
    queue<int> q;
    d[start] = 1;
    q.push(start);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for(int &v : adj[u]) if (!d[v]) d[v] = d[u] + 1, q.push(v);
    }
}
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }

    int n; cin >> n;
    for(int i = 1; i <= n; i++) {
        int l, r; cin >> l >> r;
        for(int j = l; j <= r; j++) adj[j].push_back(i);
    }
    bfs(1, f);
    bfs(n, g);
    priority_queue<ii, vector<ii>, greater<ii> > Q;

    for(int i = 1; i <= n; i++) {
        if (f[i] > 0 && g[i] > 0) {
            if (i == 1 || i == n) h[i] = f[i] + g[i] - 2;
            else h[i] = f[i] + g[i] - 3;
        }
        else h[i] = 1e9;
        Q.push({h[i], i});
    }
    while (!Q.empty()) {
        int cost, u; tie(cost, u) = Q.top(); Q.pop();
        if (cost != h[u]) continue;
        for(int &v : adj[u]) if (h[v] > h[u] + 1) {
            h[v] = h[u] + 1;
            Q.push({h[v], v});
        }
    }

    for(int i = 1; i <= n; i++) if (h[i] == 1e9) h[i] = -1;

    int q; cin >> q;
    while (q--) {
        int x; cin >> x;
        cout << h[x] << endl;
    }
}

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

passport.cpp: In function 'int main()':
passport.cpp:23:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:24:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:27:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:28:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen (task".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...