Paths and Trees (Codeforces 545E)
Tags: Greedy, shortest paths

Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1 are the same. You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.

Input: The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.

Output: In the first line print the minimum total weight of the edges of the tree.In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.If there are multiple answers, print any of them.

Note: In the first sample there are two possible shortest path trees:  with edges 1 – 3 and 2 – 3 (the total weight is 3);  with edges 1 – 2 and 2 – 3 (the total weight is 2); And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.

Sample Inputs:
3 3
1 2 1
2 3 1
1 3 2
3

4 4
1 2 1
2 3 1
3 4 1
4 1 2
4

Sample Outputs:
2
1 2

4
2 3 4

Solution (Python):
import heapq
from collections import defaultdict
from sys import stdin, stdout
input = stdin.readline
print = stdout.write

def SPT(adjacency_list, src, v):
    heap = [(0, 0, src, 0)]
    visited = [False] * (n + 1)
    spt = []
    total = 0
    while heap:
        (first_dist, weight, vertex, edge_num) = heapq.heappop(heap)
        if not visited[vertex]:
            spt.append(edge_num)
            total += weight
            visited[vertex] = True
            for (_vertex, _weight, _edgenum) in adjacency_list[vertex]:
                if not visited[_vertex]:
                    heapq.heappush(heap, (first_dist + _weight, _weight, _vertex, _edgenum))
    return (spt, total)
(n, m) = map(int, input().split())
d = defaultdict(list)
for i in range(1, m + 1):
    (a, b, w) = map(int, input().split())
    d[a].append((b, w, i))
    d[b].append((a, w, i))
src = int(input())
x = SPT(d, src, n)
print(str(x[1]) + '\n')
x = map(str, x[0][1:])
print(' '.join(x))

Solution (Python):
import heapq
(n, m) = map(int, input().split())
g = [[] for _ in range(n + 1)]
for i in range(1, m + 1):
    (u, v, w) = map(int, input().split())
    g[u].append((i, v, w))
    g[v].append((i, u, w))
src = int(input())
pq = [(0, 0, src, -1)]
mk = [0] * (n + 1)
t = []
s = 0
while pq:
    (d, w, u, e) = heapq.heappop(pq)
    if mk[u]:
        continue
    mk[u] = 1
    s += w
    t.append(e)
    for (e, v, w) in g[u]:
        if not mk[v]:
            heapq.heappush(pq, (d + w, w, v, e))
print(s)
print(*t[1:])

Solution (Python):
import heapq
(n, m) = map(int, input().split())
g = [[] for _ in range(n + 1)]
for i in range(1, m + 1):
    (u, v, w) = map(int, input().split())
    g[u].append((i, v, w))
    g[v].append((i, u, w))
src = int(input())
pq = [(0, 0, src, -1)]
mk = [0] * (n + 1)
t = []
s = 0
while pq:
    (d, w, u, e) = heapq.heappop(pq)
    if mk[u]:
        continue
    mk[u] = 1
    s += w
    t.append(e)
    for (e, v, w) in g[u]:
        if not mk[v]:
            heapq.heappush(pq, (d + w, w, v, e))
print(s)
print(*t[1:])