답안 #1114789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114789 2024-11-19T15:45:01 Z FucKanh Toll (BOI17_toll) C++14
100 / 100
271 ms 34628 KB
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define pii pair<int,int>
#define p3i pair<int,pii>

using namespace std;

const int inf = 1e15;
const int maxn = 5e4 + 2;
const int maxq = 1e4 + 2;

int k,n,m,q;
int ans[maxq];
int d[maxn][7][7];
vector<pii> a[maxn],b[maxn];

int cv(int tk, int lvl) {
    return (lvl-1) * k + tk;
}

pii cv(int u) {
    return make_pair(u % k, u / k + 1);
}

void query(int l, int r, vector<p3i> qr) {
    if (qr.size()==0) return;
    if (l==r) {
        for (p3i val : qr) {
            int id = val.first;
            ans[id] = (val.second.first == val.second.second) ? 0 : -1;
        }
        return;
    }

    for (int i = 1; i <= n; i++) {
        for (int u = 0; u < k; u++) {
            for (int v = 0; v < k; v++) {
                d[i][u][v] = inf;
            }
        }
    }

    int mid = l + r >> 1;
    for (int i = 0; i < k; i++) d[mid][i][i] = 0;

    for (int i = mid - 1; i >= l; i--) {
        for (int u = 0; u < k; u++) {
            int f = cv(u,i);
            for(pii val : a[f]) {
                int g,t; tie(g,t) =  val;
                int um = cv(g).first;
                for (int v = 0; v < k; v++) {
                    int h = cv(v,mid);
                    if (cv(v,mid) >= n) continue;
                    if (d[i+1][um][v] < inf) {
                        d[i][u][v] = min(d[i][u][v], d[i+1][um][v] + t);
                    }
                }
            }
        }
    }

    for (int i = mid + 1; i <= r; i++) {
        for (int u = 0; u < k; u++) {
            int f = cv(u,i);
            if (f >= n) continue;
            for(pii val : b[f]) {
                int g,t; tie(g,t) =  val;
                int um = cv(g).first;
                for (int v = 0; v < k; v++) {
                    int h = cv(v,mid);
                    if (d[i-1][v][um] < inf) {
                        d[i][v][u] = min(d[i][v][u], d[i-1][v][um] + t);
                    }
                }
            }
        }
    }
    vector<p3i> qrl,qrr;
    for (p3i val : qr) {
        int l,r; tie(l,r) = val.second;
        int u,lvlu; tie(u,lvlu) = cv(l);
        int v,lvlv; tie(v,lvlv) = cv(r);
        int id = val.first;
        if (lvlv <= mid) qrl.push_back(val);
        else if (lvlu >mid) qrr.push_back(val);
        else {
            ans[id] = inf;
            for (int f = 0; f < k; f++) {
                ans[id] = min(ans[id], d[lvlu][u][f] + d[lvlv][f][v]);
            }
            if (ans[id] >= inf) ans[id] = -1;
        }
    }

    query(l,mid,qrl);
    query(mid+1,r,qrr);
}

void solve() {
    cin >> k >> n >> m >> q;
    int lvl = n / k + (n%k != 0);
    for (int i = 1; i <= m; i++) {
        int x,y,t; cin >> x >> y >> t;
        a[x].push_back({y,t});
        b[y].push_back({x,t});
    }

    vector<p3i> qr;
    for (int i = 1; i <= q; i++){
        int x,y; cin >> x >> y;
        qr.push_back({i,{x,y}});
    }
    query(1,lvl,qr);
    for (int i = 1; i <= q; i++) {
        cout << ans[i] << '\n';
    }
}
void file(string name) {
    if (fopen((name + ".inp").c_str(), "r")) {
        freopen((name + ".inp").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
signed main() {
    file("toll");
    cin.tie(0) -> sync_with_stdio(0);
    solve();
    return 0;
}

Compilation message

toll.cpp: In function 'void query(long long int, long long int, std::vector<std::pair<long long int, std::pair<long long int, long long int> > >)':
toll.cpp:44:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |     int mid = l + r >> 1;
      |               ~~^~~
toll.cpp:54:25: warning: unused variable 'h' [-Wunused-variable]
   54 |                     int h = cv(v,mid);
      |                         ^
toll.cpp:72:25: warning: unused variable 'h' [-Wunused-variable]
   72 |                     int h = cv(v,mid);
      |                         ^
toll.cpp: In function 'void file(std::string)':
toll.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen((name + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 25936 KB Output is correct
2 Correct 1 ms 4688 KB Output is correct
3 Correct 2 ms 4688 KB Output is correct
4 Correct 2 ms 4688 KB Output is correct
5 Correct 2 ms 4688 KB Output is correct
6 Correct 2 ms 4688 KB Output is correct
7 Correct 2 ms 4688 KB Output is correct
8 Correct 41 ms 25932 KB Output is correct
9 Correct 39 ms 26448 KB Output is correct
10 Correct 22 ms 22864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 27868 KB Output is correct
2 Correct 2 ms 4688 KB Output is correct
3 Correct 1 ms 4688 KB Output is correct
4 Correct 1 ms 4688 KB Output is correct
5 Correct 1 ms 4688 KB Output is correct
6 Correct 2 ms 4688 KB Output is correct
7 Correct 4 ms 5712 KB Output is correct
8 Correct 4 ms 5968 KB Output is correct
9 Correct 22 ms 26956 KB Output is correct
10 Correct 60 ms 32864 KB Output is correct
11 Correct 45 ms 29940 KB Output is correct
12 Correct 36 ms 28228 KB Output is correct
13 Correct 64 ms 27020 KB Output is correct
14 Correct 35 ms 21832 KB Output is correct
15 Correct 30 ms 20524 KB Output is correct
16 Correct 33 ms 20536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4688 KB Output is correct
2 Correct 2 ms 4688 KB Output is correct
3 Correct 2 ms 4688 KB Output is correct
4 Correct 2 ms 4688 KB Output is correct
5 Correct 2 ms 4688 KB Output is correct
6 Correct 2 ms 4856 KB Output is correct
7 Correct 2 ms 4756 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 3 ms 5112 KB Output is correct
10 Correct 25 ms 25680 KB Output is correct
11 Correct 49 ms 28488 KB Output is correct
12 Correct 71 ms 31816 KB Output is correct
13 Correct 72 ms 32864 KB Output is correct
14 Correct 69 ms 30024 KB Output is correct
15 Correct 53 ms 20100 KB Output is correct
16 Correct 42 ms 20048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4688 KB Output is correct
2 Correct 2 ms 4688 KB Output is correct
3 Correct 2 ms 4688 KB Output is correct
4 Correct 2 ms 4688 KB Output is correct
5 Correct 2 ms 4688 KB Output is correct
6 Correct 2 ms 4856 KB Output is correct
7 Correct 2 ms 4756 KB Output is correct
8 Correct 3 ms 4944 KB Output is correct
9 Correct 3 ms 5112 KB Output is correct
10 Correct 25 ms 25680 KB Output is correct
11 Correct 49 ms 28488 KB Output is correct
12 Correct 71 ms 31816 KB Output is correct
13 Correct 72 ms 32864 KB Output is correct
14 Correct 69 ms 30024 KB Output is correct
15 Correct 53 ms 20100 KB Output is correct
16 Correct 42 ms 20048 KB Output is correct
17 Correct 81 ms 28764 KB Output is correct
18 Correct 2 ms 4688 KB Output is correct
19 Correct 2 ms 4688 KB Output is correct
20 Correct 2 ms 4688 KB Output is correct
21 Correct 1 ms 4688 KB Output is correct
22 Correct 2 ms 4532 KB Output is correct
23 Correct 3 ms 5116 KB Output is correct
24 Correct 3 ms 4944 KB Output is correct
25 Correct 5 ms 5200 KB Output is correct
26 Correct 5 ms 5200 KB Output is correct
27 Correct 41 ms 26120 KB Output is correct
28 Correct 129 ms 32072 KB Output is correct
29 Correct 149 ms 33352 KB Output is correct
30 Correct 104 ms 30280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 25936 KB Output is correct
2 Correct 1 ms 4688 KB Output is correct
3 Correct 2 ms 4688 KB Output is correct
4 Correct 2 ms 4688 KB Output is correct
5 Correct 2 ms 4688 KB Output is correct
6 Correct 2 ms 4688 KB Output is correct
7 Correct 2 ms 4688 KB Output is correct
8 Correct 41 ms 25932 KB Output is correct
9 Correct 39 ms 26448 KB Output is correct
10 Correct 22 ms 22864 KB Output is correct
11 Correct 41 ms 27868 KB Output is correct
12 Correct 2 ms 4688 KB Output is correct
13 Correct 1 ms 4688 KB Output is correct
14 Correct 1 ms 4688 KB Output is correct
15 Correct 1 ms 4688 KB Output is correct
16 Correct 2 ms 4688 KB Output is correct
17 Correct 4 ms 5712 KB Output is correct
18 Correct 4 ms 5968 KB Output is correct
19 Correct 22 ms 26956 KB Output is correct
20 Correct 60 ms 32864 KB Output is correct
21 Correct 45 ms 29940 KB Output is correct
22 Correct 36 ms 28228 KB Output is correct
23 Correct 64 ms 27020 KB Output is correct
24 Correct 35 ms 21832 KB Output is correct
25 Correct 30 ms 20524 KB Output is correct
26 Correct 33 ms 20536 KB Output is correct
27 Correct 1 ms 4688 KB Output is correct
28 Correct 2 ms 4688 KB Output is correct
29 Correct 2 ms 4688 KB Output is correct
30 Correct 2 ms 4688 KB Output is correct
31 Correct 2 ms 4688 KB Output is correct
32 Correct 2 ms 4856 KB Output is correct
33 Correct 2 ms 4756 KB Output is correct
34 Correct 3 ms 4944 KB Output is correct
35 Correct 3 ms 5112 KB Output is correct
36 Correct 25 ms 25680 KB Output is correct
37 Correct 49 ms 28488 KB Output is correct
38 Correct 71 ms 31816 KB Output is correct
39 Correct 72 ms 32864 KB Output is correct
40 Correct 69 ms 30024 KB Output is correct
41 Correct 53 ms 20100 KB Output is correct
42 Correct 42 ms 20048 KB Output is correct
43 Correct 81 ms 28764 KB Output is correct
44 Correct 2 ms 4688 KB Output is correct
45 Correct 2 ms 4688 KB Output is correct
46 Correct 2 ms 4688 KB Output is correct
47 Correct 1 ms 4688 KB Output is correct
48 Correct 2 ms 4532 KB Output is correct
49 Correct 3 ms 5116 KB Output is correct
50 Correct 3 ms 4944 KB Output is correct
51 Correct 5 ms 5200 KB Output is correct
52 Correct 5 ms 5200 KB Output is correct
53 Correct 41 ms 26120 KB Output is correct
54 Correct 129 ms 32072 KB Output is correct
55 Correct 149 ms 33352 KB Output is correct
56 Correct 104 ms 30280 KB Output is correct
57 Correct 271 ms 34628 KB Output is correct
58 Correct 54 ms 26700 KB Output is correct
59 Correct 111 ms 29780 KB Output is correct