답안 #784733

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784733 2023-07-16T13:04:28 Z chanhchuong123 Toll (BOI17_toll) C++14
0 / 100
37 ms 9172 KB
#include <bits/stdc++.h>
using namespace std;
#define task ""
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)

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;
}

const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};

const int INF = 1e9 + 7;
const int MAX = 50000;
int k, n, m, q;
int ans[MAX];
int L[MAX], R[MAX];
int A[MAX], B[MAX];
vector<int> blocks[MAX];
vector<pair<int, int>> adj[2][MAX];

void divi(int l, int r, vector<int> &queries) {
    if (queries.empty()) return;
    if (l == r) {
        for (int &id: queries) {
            ans[id] = A[id] == B[id] ? 0 : INF;
        }
        return;
    }
    int mid = l + r >> 1;
    for (int i = l; i <= mid - 1; ++i) for (int &u: blocks[i]) L[u] = INF;
    for (int i = mid + 1; i <= r; ++i) for (int &u: blocks[i]) R[u] = INF;
    for (int &u: blocks[mid]) L[u] = 0;
    for (int i = mid; i > l; --i) {
        for (int &u: blocks[i]) {
            for (pair<int, int> &x: adj[0][u]) {
                int v = x.first, w = x.second;
                minimize(L[v], L[u] + w);
            }
        }
    }
    for (int &u: blocks[mid]) {
        for (pair<int, int> &x: adj[1][u]) {
            int v = x.first, w = x.second;
            minimize(R[v], R[u] + w);
        }
    }
    for (int i = mid + 1; i < r; ++i) {
        for (int &u: blocks[i]) {
            for (pair<int, int> &x: adj[1][u]) {
                int v = x.first, w = x.second;
                minimize(R[v], R[u] + w);
            }
        }
    }
    vector<int> todo[2];
    for (int &id: queries) {
        if (A[id] / k <= mid && mid < B[id] / k) {
            ans[id] = min(ans[id], L[A[id]] + R[B[id]]);
        } else {
            todo[A[id] / k > mid].push_back(id);
        }
    }
    divi(l, mid, todo[0]); divi(mid + 1, r, todo[1]);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

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

	cin >> k >> n >> m >> q;
	for (int i = 1; i <= m; ++i) {
        int a, b, t; cin >> a >> b >> t;
        adj[1][a].push_back({b, t});
        adj[0][b].push_back({a, t});
	}
	for (int i = 0; i < n; ++i) {
        blocks[i / k].push_back(i);
	}
	vector<int> queries;
	for (int i = 0; i < q; ++i) {
        ans[i] = INF;
        cin >> A[i] >> B[i];
        queries.push_back(i);
	}
	divi(0, (n + k - 1) / k - 1, queries);
	for (int i = 0; i < q; ++i) {
        if (ans[i] == INF) ans[i] = -1;
        cout << ans[i] << '\n';
	}

	cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
	return 0;
}

Compilation message

toll.cpp: In function 'void divi(int, int, std::vector<int>&)':
toll.cpp:34:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   34 |     int mid = l + r >> 1;
      |               ~~^~~
toll.cpp: In function 'int main()':
toll.cpp:75:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:76:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 9172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 8260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 3796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 3796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 9172 KB Output isn't correct
2 Halted 0 ms 0 KB -