178392025-09-19 12:33:06DávidNagysebességű vasútcpp17Wrong answer 30/100597ms23136 KiB
#include <bits/stdc++.h>
#include <functional>
#include <queue>
#include <vector>
using namespace std;
#define pii pair<int, int>
#define ll long long
#define pli pair<ll, int>

const ll INF = 1e12;

void dijkstra(int s, vector<vector<pii>> &graf, vector<ll> &tav, vector<int> &honnan) {
    priority_queue<pli, vector<pli>, greater<pli>> q;
    q.push({0, s});
    tav[s] = 0;
    while(!q.empty()) {
        int index = q.top().second;
        ll d = q.top().first;
        q.pop();
        if(d != tav[index]) continue;
        for(pii el : graf[index]) {
            int cel = el.first;
            int w = el.second;
            if(tav[index] + w < tav[cel]) {
                tav[cel] = tav[index] + w;
                q.push({tav[cel], cel});
                honnan[cel] = index;
            }
        }
    }
}

int main() {
	int n, m;
    cin >> n >> m;
    vector<vector<pii>> graf(n);
    vector<ll> a(n, INF), b(n, INF);
    vector<int> honnan(n, -1);
    for(int i = 0; i < m; i++) {
        int x, y, w;
        cin >> x >> y >> w;
        graf[x].push_back({y, w});
        graf[y].push_back({x, w});
    }
    dijkstra(0, graf, a, honnan);
    dijkstra(n - 1, graf, b, honnan);
    vector<bool> ut(n);
    int most = 0;
    while(most != n - 1) {
        ut[most] = true;
        most = honnan[most];
    }
    ut[n - 1] = true;
    //for(int i = 0; i < n; i++) if(ut[i]) cout << i << " ";
    //cout << endl;
    //cout << b[1] << " " << a[2] << endl;
    unsigned int eredmeny = -1;
    for(int i = 0; i < n; i++) {
        for(pii el : graf[i]) {
            int index = el.first;
            int w = el.second;
            int kell = b[0] - min(a[i] + b[index], b[i] + a[index]) - 1;
            if((!ut[i] || !ut[index]) && w - kell < w) eredmeny = min(eredmeny, (unsigned int)(w - kell));
            //cout << i << " " << index << " " << kell << endl;
        }
    }
    cout << (int)eredmeny << endl;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted1ms508 KiB
subtask20/35
3Wrong answer4ms564 KiB
4Wrong answer4ms564 KiB
5Accepted3ms316 KiB
6Accepted4ms564 KiB
7Accepted4ms316 KiB
8Accepted3ms316 KiB
subtask330/30
9Accepted167ms9148 KiB
10Accepted150ms9124 KiB
11Accepted148ms9176 KiB
12Accepted156ms9268 KiB
13Accepted144ms9268 KiB
14Accepted155ms9268 KiB
subtask40/35
15Wrong answer552ms22916 KiB
16Wrong answer597ms22916 KiB
17Wrong answer597ms22920 KiB
18Wrong answer549ms23136 KiB
19Accepted550ms23088 KiB
20Accepted550ms23096 KiB
21Accepted596ms23092 KiB
22Accepted597ms22936 KiB
23Accepted532ms23088 KiB
24Accepted273ms12596 KiB
25Accepted163ms9268 KiB
26Accepted163ms9268 KiB
27Accepted144ms9268 KiB