제출 #1202606

#제출 시각아이디문제언어결과실행 시간메모리
120260629ChuManhTich날다람쥐 (JOI14_ho_t4)C++20
0 / 100
116 ms16068 KiB
#include<bits/stdc++.h>
using namespace std;

#define NAME "ROBOT"
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int, int>
#define fi first
#define se second
#define fastIO ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                                \
    cout.tie(NULL);
#define BIT(x, i) ((x >> i) & 1)
#define ALL(x) x.begin(), x.end()

const int maxn = 2e5 + 11;
const int LOGN = 20;
const int INF = 1e9 + 7;

int d[maxn]; /// thời gian ít nhất để di chuyển từ cây 1 đến cây i
int h[maxn], n, m, x;
vector<ii> adj[maxn];
int trace[maxn];
vector<int> path;

signed main() {
    if(fopen(NAME".INP", "r")) {
        freopen(NAME".INP", "r", stdin);
        freopen(NAME".OUT", "w", stdout);
    }
    fastIO;

    cin >> n >> m >> x;

    FOR(i, 1, n) {
        cin >> h[i];
        d[i] = INF;
    }

    FOR(i, 1, m) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].push_back({w, v});
        adj[v].push_back({w, u});
    }

    d[1] = 0;

    priority_queue<ii, vector<ii>, greater<ii>> q;
    q.push({0, 1});
    trace[1] = -1;
    while(q.size()){
        auto top = q.top();q.pop();
        int u = top.second;
        int du = top.first;

        if(du > d[u]) continue;

        for(auto i : adj[u]){
            int t = i.fi;
            int v = i.se;

            if(d[v] > du + t){
                d[v] = du + t;
                trace[v] = u;
                q.push({d[v], v});
            }
        }
    }

//    cout << d[n];

    int u = n;
    ll res = 0;

    while(trace[u] != -1) {
        int pu = trace[u];
        path.push_back(u);
        u = pu;
    }
    path.push_back(1);
    reverse(ALL(path));

    for(int i = 1; i < path.size(); i++) {
        int t = d[path[i]] - d[path[i - 1]];
//        cout << t << '\n';

        res += t;
        if(x < t) {
            res += abs(x - t);
            x = t;
        }
        if(x > h[path[i - 1]]) {
            res = -1;
            break;
        }

        if(x - t > h[path[i]]) {
            res += abs(x - t - h[path[i]]);
            x = h[path[i]];
            continue;
        }


        x -= t;
    }

    if(res != -1 && x != h[n]) {
        res += abs(x - h[n]);
        x = h[n];
    }

    cout << res;
    return 0;
}
/*
5 5 0
50
100
25
30
10
1 2 10
2 5 50
2 4 20
4 3 1
5 4 20

2 1 0
1
1
1 2 100
*/

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

2014_ho_t4.cpp: In function 'int main()':
2014_ho_t4.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen(NAME".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t4.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         freopen(NAME".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...