p103.in1
======================
1 4
4 5
B 2 16 99
P 6 32 13
P 2 87 4
P 38 96 49
1 2 4
1 3 40
2 3 75
2 4 76
3 4 77

=================
p103.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, m, source, destination;
vector<tuple<bool, int, int, int>> junctions;
vector<vector<pair<int, int>>> adj;

void read() {
    cin >> source >> destination;
    source--, destination--;
    cin >> n >> m;

    junctions.resize(n);
    for(int i = 0; i < n; ++i) {
        char Ci;
        int riC, tiB, tiP;
        cin >> Ci >> riC >> tiB >> tiP;
        junctions[i] = make_tuple(Ci == 'B', riC, tiB, tiP);
    }

    adj.assign(n, {});
    for(int i = 0; i < m; ++i) {
        int from, to, length;
        cin >> from >> to >> length;
        from--, to--;
        adj[from].push_back({to, length});
        adj[to].push_back({from, length});
    }
}

/*
AAAAAAAAABBBBBBBBBBBBBBBAAAAAAAAAA
BBBBBBBBBAAAAAAAAAAAAAAABBBBBBBBBB
*/

void solve() {
    function<int(int, int)> get_color = [&](int u, int t) -> int {
        auto [isBlue, r, tB, tP] = junctions[u];
        if(t < r) {
            return isBlue;
        }

        if(isBlue && t < r + tP) {
            return 0;
        } else if(isBlue) {
            r += tP;
        }

        int cycle = tB + tP;
        int tU = (t - r) % cycle;
        return (int)(tU < tB);
    };

    function<int(int, int)> time_to_next_color = [&](int u, int t) {
        auto [isBlue, r, tB, tP] = junctions[u];
        if(t < r) {
            return r - t;
        }

        int cycle = tB + tP;
        if(isBlue && t < r + tP) {
            return r + tP - t;
        } else if(isBlue) {
            r += tP;
        }

        int tU = (t - r) % cycle;
        return tU < tB ? (tB - tU) : (cycle - tU);
    };

    function<int(int, int, int)> first_time_same_color = [&](int u, int v,
                                                             int t) {
        for(int attempt = 0; attempt < 3; attempt++) {
            if(get_color(v, t) == get_color(u, t)) {
                return t;
            }

            int dtu = time_to_next_color(u, t);
            int dtv = time_to_next_color(v, t);
            t += min(dtu, dtv);
        }

        return -1;
    };

    vector<int> dist(n, INT_MAX);
    vector<int> parent(n, -1);
    priority_queue<
        pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
        pq;

    pq.push({0, source});
    dist[source] = 0;
    while(!pq.empty()) {
        auto [t, u] = pq.top();
        pq.pop();

        for(auto [v, w]: adj[u]) {
            int first_time = first_time_same_color(u, v, t);
            if(first_time == -1) {
                continue;
            }

            int new_time = first_time + w;
            if(new_time < dist[v]) {
                parent[v] = u;
                dist[v] = new_time;
                pq.push({new_time, v});
            }
        }
    }

    if(dist[destination] == INT_MAX) {
        cout << "0\n";
        return;
    }

    vector<int> path;
    int u = destination;
    while(u != -1) {
        path.push_back(u + 1);
        u = parent[u];
    }

    reverse(path.begin(), path.end());
    cout << dist[destination] << '\n';
    cout << path << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
103. Traffic Lights
Time limit per test: 0.25 second(s)
Memory limit: 4096 kilobytes
input: standard
output: standard



In the city of Dingilville the traffic is arranged in an unusual way. There are junctions and roads connecting the junctions. There is at most one road between any two different junctions. There is no road connecting a junction to itself. Travel time for a road is the same for both directions. At every junction there is a single traffic light that is either blue or purple at any moment. The color of each light alternates periodically: blue for certain duration and then purple for another duration. Traffic is permitted to travel down the road between any two junctions, if and only if the lights at both junctions are the same color at the moment of departing from one junction for the other. If a vehicle arrives at a junction just at the moment the lights switch it must consider the new colors of lights. Vehicles are allowed to wait at the junctions. You are given the city map which shows:
the travel times for all roads (integers)
the durations of the two colors at each junction (integers)
and the initial color of the light and the remaining time (integer) for this color to change at each junction.

Your task is to find a path which takes the minimum time from a given source junction to a given destination junction for a vehicle when the traffic starts. In case more than one such path exists you are required to report only one of them.

Input
The first line contains two numbers: The id-number of the source junction and the id-number of the destination junction. The second line contains two numbers: N, M. The following N lines contain information on N junctions. The (i+2)'th line of the input file holds information about the junction i : Ci, riC, tiB, tiP where Ci is either B for blue or P for purple, indicating the initial color of the light at the junction i. Finally, the next M lines contain information on M roads. Each line is of the form: i, j, lij where i and j are the id-numbers of the junctions which are connected by this road. 2 ≤ N ≤ 300 where N is the number of junctions. The junctions are identified by integers 1 through N. These numbers are called id-numbers. 1 ≤ M ≤ 14000 where M is the number of roads. 1 ≤ lij ≤ 100 where lij is the time required to move from junction i to j using the road that connects i and j. 1 ≤ tiC ≤ 100 where tiC is the duration of the color c for the light at the junction i. The index c is either 'B' for blue or 'P' for purple. 1 ≤ riC ≤ tiC where riC is the remaining time for the initial color c at junction i.

Output
If a path exists:
The first line will contain the time taken by a minimum-time path from the source junction to the destination junction.
Second line will contain the list of junctions that construct the minimum-time path you have found. You have to write the junctions to the output file in the order of travelling. Therefore the first integer in this line must be the id-number of the source junction and the last one the id-number of the destination junction.

If a path does not exist:
A single line containing only the integer 0.

Example(s)
sample input
sample output
1 4
4 5
B 2 16 99
P 6 32 13
P 2 87 4
P 38 96 49
1 2 4
1 3 40
2 3 75
2 4 76
3 4 77
127
1 2 4

=================
p103.ans1
======================
127
1 2 4

=================
