Road Repairs
A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 is the capital of the country. Some pairs of cities have monodirectional roads built between them. However, not all of them are in good condition. For each road we know whether it needs repairing or not. If a road needs repairing, then it is forbidden to use it. However, the Berland government can repair the road so that it can be used.Right now Berland is being threatened by the war with the neighbouring state. So the capital officials decided to send a military squad to each city. The squads can move only along the existing roads, as there's no time or money to build new roads. However, some roads will probably have to be repaired in order to get to some cities.Of course the country needs much resources to defeat the enemy, so you want to be careful with what you're going to throw the forces on. That's why the Berland government wants to repair the minimum number of roads that is enough for the military troops to get to any city from the capital, driving along good or repaired roads. Your task is to help the Berland government and to find out, which roads need to be repaired.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of roads in Berland.Next m lines contain three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 0 ≤ ci ≤ 1), describing the road from city ai to city bi. If ci equals 0, than the given road is in a good condition. If ci equals 1, then it needs to be repaired.It is guaranteed that there is not more than one road between the cities in each direction.
If even after all roads are repaired, it is still impossible to get to some city from the capital, print  - 1. Otherwise, on the first line print the minimum number of roads that need to be repaired, and on the second line print the numbers of these roads, separated by single spaces.The roads are numbered starting from 1 in the order, in which they are given in the input.If there are multiple sets, consisting of the minimum number of roads to repair to make travelling to any city from the capital possible, print any of them.If it is possible to reach any city, driving along the roads that already are in a good condition, print 0 in the only output line.
class edge:

    def __init__(self, u, v, w, id):
        self.u = u
        self.v = v
        self.w = w
        self.id = id
        self.u1 = u
        self.v1 = v
        self.w1 = w
edges = []
with open('input.txt', 'r') as f:
    (n, m) = map(int, f.readline().split())
    for j in range(m):
        (u, v, w) = map(int, f.readline().split())
        edges.append(edge(u - 1, v - 1, w, j))

def ZhuLiu(root, n, edges):
    m = len(edges)
    used = [0] * m
    dele = [-1] * m
    add = [-1] * m
    m1 = m
    sum = 0
    while 1:
        incost = [-1] * n
        id = [-1] * n
        vis = [-1] * n
        pre = [-1] * n
        in_edge_id = [-1] * n
        for j in range(m):
            (u, v) = (edges[j].u, edges[j].v)
            if u != v and v != root and (incost[v] == -1 or incost[v] > edges[j].w):
                incost[v] = edges[j].w
                pre[v] = u
                in_edge_id[v] = edges[j].id
        incost[root] = 0
        for i in range(n):
            if i != root:
                if incost[i] == -1:
                    return (-1, used)
                used[in_edge_id[i]] = 1
        tn = -1
        for i in range(n):
            sum += incost[i]
            v = i
            while vis[v] != i and v != root and (id[v] == -1):
                vis[v] = i
                v = pre[v]
            if v != root and id[v] == -1:
                tn += 1
                u = v
                while 1 > 0:
                    id[u] = tn
                    u = pre[u]
                    if u == v:
                        break
        if tn == -1:
            break
        for i in range(n):
            if id[i] == -1:
                tn += 1
                id[i] = tn
        for j in range(m):
            (u, v) = (edges[j].u, edges[j].v)
            (edges[j].u, edges[j].v) = (id[u], id[v])
            if id[u] != id[v]:
                edges[j].w = edges[j].w - incost[v]
                used.append(0)
                dele.append(in_edge_id[v])
                add.append(edges[j].id)
                edges[j].id = m1
                m1 += 1
        n = tn + 1
        root = id[root]
    for j in range(m1 - 1, m, -1):
        if used[j]:
            used[dele[j]] = 0
            used[add[j]] = 1
    return (sum, used[:m])
(sum, used) = ZhuLiu(0, n, edges)
with open('output.txt', 'w') as f:
    f.write(str(sum) + '\n')
    if sum > 0:
        out = []
        for j in range(m):
            if used[j] and edges[j].w1:
                out.append(j + 1)
        f.write(' '.join(map(str, out)) + '\n')
class edge:

    def __init__(self, u, v, w, id):
        self.u = u
        self.v = v
        self.w = w
        self.val = w
        self.id = id
edges = []
with open('input.txt', 'r') as f:
    (n, m) = map(int, f.readline().split())
    for j in range(m):
        (u, v, w) = map(int, f.readline().split())
        edges.append(edge(u - 1, v - 1, w, j))

def ZhuLiu(root, n, edges):
    m = len(edges)
    used = [0] * m
    dele = [-1] * m
    add = [-1] * m
    m1 = m
    sum = 0
    while 1:
        incost = [-1] * n
        id = [-1] * n
        vis = [-1] * n
        pre = [-1] * n
        in_edge_id = [-1] * n
        for j in range(m):
            (u, v) = (edges[j].u, edges[j].v)
            if u != v and v != root and (incost[v] == -1 or incost[v] > edges[j].val):
                incost[v] = edges[j].val
                pre[v] = u
                in_edge_id[v] = edges[j].id
        incost[root] = 0
        for i in range(n):
            if i != root:
                if incost[i] == -1:
                    return (-1, used)
                used[in_edge_id[i]] = 1
        tn = -1
        for i in range(n):
            sum += incost[i]
            v = i
            while vis[v] != i and v != root and (id[v] == -1):
                vis[v] = i
                v = pre[v]
            if v != root and id[v] == -1:
                tn += 1
                u = v
                while 1 > 0:
                    id[u] = tn
                    u = pre[u]
                    if u == v:
                        break
        if tn == -1:
            break
        for i in range(n):
            if id[i] == -1:
                tn += 1
                id[i] = tn
        for j in range(m):
            (u, v) = (edges[j].u, edges[j].v)
            (edges[j].u, edges[j].v) = (id[u], id[v])
            if id[u] != id[v]:
                edges[j].val = edges[j].val - incost[v]
                used.append(0)
                dele.append(in_edge_id[v])
                add.append(edges[j].id)
                edges[j].id = m1
                m1 += 1
        n = tn + 1
        root = id[root]
    for j in range(m1 - 1, m, -1):
        if used[j]:
            used[dele[j]] = 0
            used[add[j]] = 1
    return (sum, used[:m])
(sum, used) = ZhuLiu(0, n, edges)
with open('output.txt', 'w') as f:
    f.write(str(sum) + '\n')
    if sum > 0:
        out = []
        for j in range(m):
            if used[j] and edges[j].w:
                out.append(j + 1)
        f.write(' '.join(map(str, out)) + '\n')
class edge:

    def __init__(self, u, v, w, id):
        self.u = u
        self.v = v
        self.w = w
        self.val = w
        self.id = id
edges = []
with open('input.txt', 'r') as f:
    (n, m) = map(int, f.readline().split())
    for j in range(m):
        (u, v, w) = map(int, f.readline().split())
        edges.append(edge(u - 1, v - 1, w, j))

def ZhuLiu(root, n, edges):
    m = len(edges)
    used = [0] * m
    dele = [-1] * m
    add = [-1] * m
    m1 = m
    sum = 0
    while 1:
        incost = [10000000000.0] * n
        id = [-1] * n
        vis = [-1] * n
        pre = [-1] * n
        in_edge_id = [-1] * n
        for j in range(m):
            (u, v) = (edges[j].u, edges[j].v)
            if u != v and v != root and (edges[j].val < incost[v]):
                incost[v] = edges[j].val
                pre[v] = u
                in_edge_id[v] = edges[j].id
        incost[root] = 0
        for i in range(n):
            if i != root:
                if incost[i] == 10000000000.0:
                    return (-1, used)
                used[in_edge_id[i]] = 1
        tn = -1
        for i in range(n):
            sum += incost[i]
            v = i
            while vis[v] != i and v != root and (id[v] == -1):
                vis[v] = i
                v = pre[v]
            if v != root and id[v] == -1:
                tn += 1
                u = v
                while 1 > 0:
                    id[u] = tn
                    u = pre[u]
                    if u == v:
                        break
        if tn == -1:
            break
        for i in range(n):
            if id[i] == -1:
                tn += 1
                id[i] = tn
        for j in range(m):
            (u, v) = (edges[j].u, edges[j].v)
            (edges[j].u, edges[j].v) = (id[u], id[v])
            if id[u] != id[v]:
                edges[j].val = edges[j].val - incost[v]
                used.append(0)
                dele.append(in_edge_id[v])
                add.append(edges[j].id)
                edges[j].id = m1
                m1 += 1
        n = tn + 1
        root = id[root]
    for j in range(m1 - 1, m, -1):
        if used[j]:
            used[dele[j]] = 0
            used[add[j]] = 1
    return (sum, used[:m])
(sum, used) = ZhuLiu(0, n, edges)
with open('output.txt', 'w') as f:
    f.write(str(sum) + '\n')
    if sum > 0:
        out = []
        for j in range(m):
            if used[j] and edges[j].w:
                out.append(j + 1)
        f.write(' '.join(map(str, out)) + '\n')