61062023-10-30 10:30:15GervidNagysebességű vasútcpp17Runtime error 0/100425ms1047588 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <limits.h>

using namespace std;

int main()
{
    int n, m, i, j, temp1, temp2;
    cin >> n >> m;

    vector<vector<int>> edgematrix(n, vector<int>(n, -1));
    vector<vector<bool>> inoptpath(n, vector<bool>(n, 0));
    vector<vector<int>> last(2, vector<int>(n));
    last[0][0] = -1, last[1][n-1] = -1;

    vector<vector<pair<int, int>>> connections(n);

    for (i = 0; i < m; i++)
    {
        cin >> temp1 >> temp2;

        cin >> edgematrix[temp1][temp2];
        edgematrix[temp2][temp1] = edgematrix[temp1][temp2];

        connections[temp1].push_back({ temp2, edgematrix[temp1][temp2] });
        connections[temp2].push_back({ temp1, edgematrix[temp1][temp2] });
    }

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> steps;

    vector<vector<int>> distance(2, vector<int>(n, INT_MAX));
    distance[0][0] = 0, distance[1][n-1] = 0;

    int starts[2] = { 0, n - 1 };

    for (i = 0; i < 2; i++)
    {
        steps.push({ 0, starts[i] });

        while (steps.size() > 0)
        {
            for (j = 0; j < connections[steps.top().second].size(); j++)
            {
                if (distance[i][connections[steps.top().second][j].first] > steps.top().first + connections[steps.top().second][j].second)
                {
                    distance[i][connections[steps.top().second][j].first] = (steps.top().first + connections[steps.top().second][j].second);
                    steps.push({steps.top().first + connections[steps.top().second][j].second, connections[steps.top().second][j].first});
    
                    last[i][connections[steps.top().second][j].first] = steps.top().second;
                }
            }
            steps.pop();
        }
    }

    temp1 = n - 1;

    while (last[0][temp1] != -1)
    {
        inoptpath[temp1][last[0][temp1]] = true, inoptpath[last[0][temp1]][temp1] = true;
        temp1 = last[0][temp1];
    }

    int opt = distance[0][n - 1];
    int min, mini;
    set<int> nope;

    while (true)
    {
        min = INT_MAX;

        for (i = 1; i < n-1; i++)
        {
            if (min > distance[0][i] + distance[1][i] && distance[0][i] + distance[1][i] != opt && !nope.count(i))
            {
                min = distance[0][i] + distance[1][i];
                mini = i;
            }
        }

        if (min == INT_MAX)
        {
            cout << -1;
            return 0;
        }
        
        int maxreduction = 0;

        for (i = 0; i < 2; i++)
        {
            temp1 = mini;

            while (last[i][temp1] != -1)
            {
                if (inoptpath[temp1][last[i][temp1]])
                {
                    temp1 = last[i][temp1];
                    continue;
                }

                if (maxreduction < edgematrix[temp1][last[i][temp1]] - 1)
                {
                    maxreduction = edgematrix[temp1][last[i][temp1]] - 1;
                    if (maxreduction > distance[0][mini] + distance[1][mini] - opt)
                    {
                        cout << distance[0][mini] + distance[1][mini] - opt + 1;
                        return 0;
                    }
                }

                temp1 = last[i][temp1];
            }
        }

        nope.insert(mini);
    }
}
//10 13
//0 1 60
//0 2 20
//1 2 5
//1 3 6
//1 4 8
//2 5 20
//3 6 36
//4 7 12
//5 7 20
//5 8 20
//6 9 40
//7 9 20
//8 9 35
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2088 KiB
2Accepted3ms2056 KiB
subtask20/35
3Runtime error8ms10664 KiB
4Runtime error9ms10968 KiB
5Runtime error4ms3340 KiB
6Wrong answer23ms11200 KiB
7Wrong answer9ms6108 KiB
8Wrong answer8ms11400 KiB
subtask30/30
9Runtime error342ms1047588 KiB
10Runtime error423ms1047568 KiB
11Runtime error342ms1047332 KiB
12Runtime error342ms1047300 KiB
13Runtime error337ms1047068 KiB
14Runtime error340ms1047072 KiB
subtask40/35
15Runtime error425ms1047056 KiB
16Runtime error419ms1046832 KiB
17Runtime error425ms1046828 KiB
18Runtime error423ms1046600 KiB
19Runtime error423ms1046612 KiB
20Runtime error421ms1046600 KiB
21Runtime error423ms1046376 KiB
22Runtime error423ms1046344 KiB
23Runtime error340ms1046180 KiB
24Runtime error340ms1046032 KiB
25Runtime error340ms1045980 KiB
26Runtime error338ms1045984 KiB
27Runtime error340ms1045920 KiB