138482025-01-08 21:38:54BucsMateHálózati átvitelcpp17Accepted 50/50160ms5892 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Node
{
    int value;
    int dist;
    int index;
};

class hasonlit
{
public:
    bool operator()(Node a, Node b)
    {
        if(a.value == b.value){
            return (a.dist < b.dist);
        }
        else {
            return (a.value > b.value);
        }
    }

};

void dijkstra(vector<vector<pair<int, int>>> &adj, int N, int K, int H, vector<vector<int>> &dp)
{
    queue<Node> pq;
    pq.push({0, 0, K});
    while(!pq.empty()){
        Node currNode = pq.front();
        pq.pop();
        if(dp[currNode.index][currNode.dist] > currNode.value || currNode.dist >= H){
            continue;
        }
        for(int i = 0; i < adj[currNode.index].size(); i++){
            int newValue;
            if(currNode.value == 0){
                newValue = adj[currNode.index][i].second;
            }
            else {
                newValue = min(currNode.value, adj[currNode.index][i].second);
            }
            Node newNode = {newValue, currNode.dist+1, adj[currNode.index][i].first};
            if(newNode.value > dp[newNode.index][newNode.dist]){
                dp[newNode.index][newNode.dist] = newNode.value;
                pq.push(newNode);
            }
        }
    }
}

int main()
{
    int N, M, K, H;
    cin >> N >> M >> K >> H;
    vector<vector<pair<int, int>>> adj(N+1);
    for(int i = 0; i < M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }
    vector<vector<int>> dp(N+1, vector<int>(H+1, 0));
    dijkstra(adj, N, K, H, dp);

    for(int i = 1; i <= N; i++){
        if(i == K){
            cout << 0 << endl;
            continue;
        }
        int maxim = 0;
        for(int j = 0; j <= H; j++){
            maxim = max(maxim, dp[i][j]);
        }
        if(maxim != 0){
            cout << maxim << endl;
        }
        else{
            cout << -1 << endl;
        }
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/02ms500 KiB
3Accepted1/11ms316 KiB
4Accepted1/11ms316 KiB
5Accepted2/22ms500 KiB
6Accepted2/22ms316 KiB
7Accepted2/22ms444 KiB
8Accepted2/22ms508 KiB
9Accepted1/14ms564 KiB
10Accepted1/16ms564 KiB
11Accepted1/18ms720 KiB
12Accepted1/18ms564 KiB
13Accepted2/28ms724 KiB
14Accepted2/29ms752 KiB
15Accepted2/219ms956 KiB
16Accepted2/216ms824 KiB
17Accepted2/217ms924 KiB
18Accepted2/223ms820 KiB
19Accepted2/219ms716 KiB
20Accepted2/217ms820 KiB
21Accepted1/164ms4736 KiB
22Accepted1/190ms4852 KiB
23Accepted1/1101ms4744 KiB
24Accepted1/1123ms4968 KiB
25Accepted2/2151ms5684 KiB
26Accepted2/2152ms5684 KiB
27Accepted2/2160ms5696 KiB
28Accepted2/268ms5172 KiB
29Accepted2/275ms5724 KiB
30Accepted2/275ms5848 KiB
31Accepted2/271ms5684 KiB
32Accepted2/275ms5892 KiB