def read_graph(lines, base=True):
    """Given the lines of a digraph file, return the graph data.

    :param lines:   a sequence of lines
    :type lines:    [str, ...]
    :param base:    Shall I consider base edges?
    :type base:     bool
    :return:        front edges, background edges, total weight and root vertex
    :rtype:         ([(int, int, float), ...], [(int, int, float), ...],
                     float, int)
    """
    fedges = []
    bedges = []
    root = None
    total_weight = None
    first = True

    for lineno, line in enumerate(lines):
        if line.startswith('c ') or line.startswith('#') or not line.strip():
            continue
        elif first:
            first = False
            vals = line.split()
            assert len(vals) in [3, 4], "invalid header line"
            root = int(vals[2])
            if len(vals) == 4:
                total_weight = float(vals[3])
        elif line.startswith('b ') and not base:
            continue
        elif line.startswith('b '):
            vals = line.split()
            assert len(vals) == 4, "invalid base edge line {}".format(lineno)
            bedges.append((int(vals[1]), int(vals[2]), float(vals[3])))
        else:
            vals = line.split()
            assert len(vals) == 3, "invalid edge line {}".format(lineno)
            fedges.append((int(vals[0]), int(vals[1]), float(vals[2])))

    return (fedges, bedges, total_weight, root)