<p>There is a country of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> where <strong>all the cities are connected</strong> by bi-directional roads. The roads are represented as a 2D integer array <code>edges</code> where <code>edges[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> denotes a road between cities <code>x<sub>i</sub></code> and <code>y<sub>i</sub></code> that takes <code>time<sub>i</sub></code> minutes to travel. There may be multiple roads of differing travel times connecting the same two cities, but no road connects a city to itself.</p>

<p>Each time you pass through a city, you must pay a passing fee. This is represented as a <strong>0-indexed</strong> integer array <code>passingFees</code> of length <code>n</code> where <code>passingFees[j]</code> is the amount of dollars you must pay when you pass through city <code>j</code>.</p>

<p>In the beginning, you are at city <code>0</code> and want to reach city <code>n - 1</code> in <code>maxTime</code><strong> minutes or less</strong>. The <strong>cost</strong> of your journey is the <strong>summation of passing fees</strong> for each city that you passed through at some moment of your journey (<strong>including</strong> the source and destination cities).</p>

<p>Given <code>maxTime</code>, <code>edges</code>, and <code>passingFees</code>, return <em>the <strong>minimum cost</strong> to complete your journey, or </em><code>-1</code><em> if you cannot complete it within </em><code>maxTime</code><em> minutes</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/leetgraph1-1.png" style="width: 371px; height: 171px;" /></p>

<pre>
<strong>Input:</strong> maxTime = 30, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
<strong>Output:</strong> 11
<strong>Explanation:</strong> The path to take is 0 -&gt; 1 -&gt; 2 -&gt; 5, which takes 30 minutes and has $11 worth of passing fees.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2021/06/04/copy-of-leetgraph1-1.png" style="width: 371px; height: 171px;" /></strong></p>

<pre>
<strong>Input:</strong> maxTime = 29, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
<strong>Output:</strong> 48
<strong>Explanation:</strong> The path to take is 0 -&gt; 3 -&gt; 4 -&gt; 5, which takes 26 minutes and has $48 worth of passing fees.
You cannot take path 0 -&gt; 1 -&gt; 2 -&gt; 5 since it would take too long.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> maxTime = 25, edges = [[0,1,10],[1,2,10],[2,5,10],[0,3,1],[3,4,10],[4,5,15]], passingFees = [5,1,2,20,20,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no way to reach city 5 from city 0 within 25 minutes.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= maxTime &lt;= 1000</code></li>
	<li><code>n == passingFees.length</code></li>
	<li><code>2 &lt;= n &lt;= 1000</code></li>
	<li><code>n - 1 &lt;= edges.length &lt;= 1000</code></li>
	<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n - 1</code></li>
	<li><code>1 &lt;= time<sub>i</sub> &lt;= 1000</code></li>
	<li><code>1 &lt;= passingFees[j] &lt;= 1000</code>&nbsp;</li>
	<li>The graph may contain multiple edges between two nodes.</li>
	<li>The graph does not contain self loops.</li>
</ul>

def improve(acum_time, acum_cost, paths):
    P = len(paths)
    new_info = True
    p=0
    while p < P:
        seen_time, seen_cost = paths.popleft()
        if seen_time <= acum_time and seen_cost <= acum_cost:
            paths.append((seen_time, seen_cost))
            new_info = False
            break
        if seen_time < acum_time or seen_cost < acum_cost:
            paths.append((seen_time, seen_cost))
        p += 1
    if new_info:
        paths.append((acum_time, acum_cost))
    return new_info

def propagate(passingFees, edges_dict, edges_time, N, maxTime):
    paths = defaultdict(deque)
    new_messages = [(passingFees[0], 0, 0)]
    messages = 0
    best_cost = float('inf')
    while new_messages:
        messages += 1
        acum_cost, acum_time, node = heapq.heappop(new_messages)
        if acum_cost > best_cost: break
        if node == N:
            best_cost = min(best_cost, acum_cost)
            continue
        for neighbor in edges_dict[node]:
            if neighbor == 0: continue
            new_acum_time = acum_time + edges_time[(node, neighbor)]
            if new_acum_time > maxTime: continue
            new_acum_cost = acum_cost + passingFees[neighbor]
            if new_acum_cost > best_cost:
                continue
            new_info = improve(new_acum_time, new_acum_cost, paths[neighbor])
            if new_info:
                heapq.heappush(new_messages, (new_acum_cost, new_acum_time, neighbor))
    return paths
        

# message to propagate:
# node, acum_time, acum_cost

class Solution:
    def minCost(self, maxTime: int, edges: List[List[int]], passingFees: List[int]) -> int:
        N = 0
        edges_dict = defaultdict(set)
        edges_time = {}
        for a,b,t in edges:
            N = max(a,b,N)
            if (a,b) not in edges_time or t < edges_time[(a,b)]:
                edges_dict[a].add(b)
                edges_dict[b].add(a)

                edges_time[(a,b)] = t
                edges_time[(b,a)] = t

                
        if N == 0:
            return passingFees[0]
        paths = propagate(passingFees, edges_dict, edges_time, N, maxTime)
        print(paths)
        best_time = float('inf')
        for time, cost in paths[N]:
            best_time = min(best_time, cost)
        if best_time == float('inf'):
            return -1
        return best_time
35100