61052023-10-30 08:03:06GervidNagysebességű vasútcpp17Time limit exceeded 0/1002.099s1046908 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>> connections(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;

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

        cin >> connections[temp1][temp2];
        connections[temp2][temp1] = connections[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 < n; j++)
            {
                if (connections[steps.top().second][j] != -1 && distance[i][j] > steps.top().first + connections[j][steps.top().second])
                {
                    distance[i][j] = (steps.top().first + connections[steps.top().second][j]);
                    steps.push({steps.top().first + connections[steps.top().second][j], j});
    
                    last[i][j] = 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 (distance[0][i] + distance[1][i] != opt && min > distance[0][i] + distance[1][i] && !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 < connections[temp1][last[i][temp1]] - 1)
                {
                    maxreduction = connections[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
2Accepted3ms2220 KiB
subtask20/35
3Time limit exceeded2.099s5964 KiB
4Time limit exceeded2.069s6140 KiB
5Time limit exceeded2.04s2564 KiB
6Wrong answer23ms11724 KiB
7Wrong answer10ms6556 KiB
8Wrong answer13ms12060 KiB
subtask30/30
9Runtime error423ms1046908 KiB
10Runtime error354ms1046660 KiB
11Runtime error340ms1046596 KiB
12Runtime error342ms1046620 KiB
13Runtime error423ms1046592 KiB
14Runtime error340ms1046580 KiB
subtask40/35
15Runtime error414ms1046332 KiB
16Runtime error340ms1046316 KiB
17Runtime error423ms1046084 KiB
18Runtime error340ms1046060 KiB
19Runtime error425ms1045916 KiB
20Runtime error423ms1045908 KiB
21Runtime error386ms1045676 KiB
22Runtime error340ms1045668 KiB
23Runtime error342ms1045572 KiB
24Runtime error342ms1045568 KiB
25Runtime error340ms1045340 KiB
26Runtime error421ms1045344 KiB
27Runtime error419ms1045152 KiB