Tenzing and His Animal Friends  (Codeforces 1842D)
Tags: Greedy, graphs

Tell a story about me and my animal friends.Tenzing has $$$n$$$ animal friends. He numbers them from $$$1$$$ to $$$n$$$.One day Tenzing wants to play with his animal friends. To do so, Tenzing will host several games.In one game, he will choose a set $$$S$$$ which is a subset of $$$\{1,2,3,...,n\}$$$ and choose an integer $$$t$$$. Then, he will play the game with the animals in $$$S$$$ for $$$t$$$ minutes.But there are some restrictions:   Tenzing loves friend $$$1$$$ very much, so $$$1$$$ must be an element of $$$S$$$.  Tenzing doesn't like friend $$$n$$$, so $$$n$$$ must not be an element of $$$S$$$.  There are m additional restrictions. The $$$i$$$-th special restriction is described by integers $$$u_i$$$, $$$v_i$$$ and $$$y_i$$$, suppose $$$x$$$ is the total time that exactly one of $$$u_i$$$ and $$$v_i$$$ is playing with Tenzing. Tenzing must ensure that $$$x$$$ is less or equal to $$$y_i$$$. Otherwise, there will be unhappiness. Tenzing wants to know the maximum total time that he can play with his animal friends. Please find out the maximum total time that Tenzing can play with his animal friends and a way to organize the games that achieves this maximum total time, or report that he can play with his animal friends for an infinite amount of time. Also, Tenzing does not want to host so many games, so he will host at most $$$n^2$$$ games.

Input: The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 100$$$, $$$0 \leq m \leq \frac{n(n-1)}{2}$$$) — the number of animal friends and the number of special restrictions.The $$$i$$$-th of the following $$$m$$$ lines of input contains three integers $$$u_i$$$, $$$v_i$$$ and $$$y_i$$$ ($$$1\leq u_i<v_i\leq n$$$, $$$0\leq y_i\leq 10^9$$$) — describing the $$$i$$$-th special restriction. It is guaranteed that for $$$1 \leq i < j \leq m$$$, $$$(u_i,v_i) \neq (u_j,v_j)$$$.

Output: If Tenzing can play with his animal friends for an infinite amount of time, output "inf". (Output without quotes.)Otherwise, in the first line, output the total time $$$T$$$ ($$$0 \leq t \leq 10^{18}$$$) and the number of games $$$k$$$ ($$$0 \leq k \leq n^2$$$).In the following $$$k$$$ lines of output, output a binary string $$$s$$$ of length $$$n$$$ and an integer $$$t$$$ ($$$0 \leq t \leq 10^{18}$$$) — representing the set $$$S$$$ and the number of minutes this game will be played. If $$$s_i=\texttt{1}$$$, then $$$i \in S$$$, otherwise if $$$s_i=\texttt{0}$$$, then $$$i \notin S$$$.Under the constraints of this problem, it can be proven that if Tenzing can only play with his friends for a finite amount of time, then he can only play with them for at most $$$10^{18}$$$ minutes.

Note: In the first test case:   Tenzing will host a game with friend $$$\{1\}$$$ for $$$1$$$ minute.  Tenzing will host a game with friends $$$\{1,4\}$$$ for $$$1$$$ minute.  Tenzing will host a game with friends $$$\{1,3\}$$$ for $$$1$$$ minute.  Tenzing will host a game with friends $$$\{1,2,3,4\}$$$ for $$$1$$$ minute. If after that, Tenzing host another game with friends $$$\{1,2\}$$$ for $$$1$$$ minute. Then the time of exactly one of friends $$$2$$$ or $$$3$$$ with Tenzing will becomes $$$2$$$ minutes which will not satisfy the $$$3$$$-rd special restriction.In the second test case, there is no special restrictions. So Tenzing can host a game with friend $$$\{1\}$$$ for an infinite amount of time.

Sample Inputs:
5 4
1 3 2
1 4 2
2 3 1
2 5 1

3 0

Sample Outputs:
4 4
10000 1
10010 1
10100 1
11110 1

inf

Solution (Python):
import sys
input = sys.stdin.readline
import heapq

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, m) = readInts()
    graph = [[] for _ in range(n + 1)]
    for _ in range(m):
        (u, v, y) = readInts()
        graph[u].append([v, y])
        graph[v].append([u, y])
    val = [float('inf')] * (n + 1)
    val[n] = 0
    qu = [(0, n)]
    while qu:
        (d, node) = heapq.heappop(qu)
        for (nei, w) in graph[node]:
            if val[node] + w < val[nei]:
                val[nei] = val[node] + w
                heapq.heappush(qu, (val[nei], nei))
    if val[1] == float('inf'):
        print('inf')
        return
    val = sorted([(val[i], i) for i in range(1, n)])
    ans = []
    off = 0
    r = ['1'] * n
    r[-1] = '0'
    for (v, i) in val:
        x = v - off
        ans.append(''.join(r) + f' {x}')
        r[i - 1] = '0'
        off = v
        if i == 1:
            break
    print(off, len(ans))
    print('\n'.join(ans))
    return
solve()

Solution (Python):
import operator

def solve():
    (n, m) = list(map(int, input().split()))
    e = [list() for i in range(n)]
    for i in range(m):
        (u, v, y) = list(map(int, input().split()))
        (u, v) = (u - 1, v - 1)
        e[u].append((v, y))
        e[v].append((u, y))
    b = set(range(n))
    q = n - 1
    (time, games) = (0, 0)
    pr = []
    ex = []
    for _ in range(n):
        if q == 0:
            break
        b.remove(q)
        ex += [(i, y) for (i, y) in e[q] if i in b]
        if not ex:
            print('inf')
            return
        (q, t) = min(ex, key=operator.itemgetter(1))
        ex = [(i, y - t) for (i, y) in ex if i != q]
        if t == 0:
            continue
        (time, games) = (time + t, games + 1)
        pr += [''.join(['1' if i in b else '0' for i in range(n)]) + ' ' + str(t)]
    pr = ['%d %d' % (time, games)] + pr
    print('\n'.join(pr))
T = 1
for I in range(T):
    solve()

Solution (Python):
bruh = [[] for _ in range(101)]
vis = [False] * 101

def dfs(x):
    if vis[x]:
        return
    vis[x] = True
    for child in bruh[x]:
        dfs(child[0])
(n, m) = map(int, input().split())
for _ in range(m):
    (x, y, z) = map(int, input().split())
    bruh[x].append((y, z))
    bruh[y].append((x, z))
dfs(1)
if not vis[n]:
    print('inf')
else:
    vis = [False] * (n + 1)
    val = [float('inf')] * (n + 1)
    s = [(0, 1)]
    val[1] = 0
    m = {}
    while s:
        p = s[0]
        x = p[1]
        vl = p[0]
        s = s[1:]
        if vl > val[x]:
            continue
        if val[x] in m:
            m[val[x]].append(x)
        else:
            m[val[x]] = [x]
        for i in bruh[x]:
            if val[i[0]] > val[x] + i[1]:
                val[i[0]] = val[x] + i[1]
                s.append((val[i[0]], i[0]))
    x = '0' * n
    ct = 0
    ans = []
    for key in sorted(m.keys()):
        ans.append((x, key - ct))
        for i in m[key]:
            x = x[:i - 1] + '1' + x[i:]
        ct = key
        if x[n - 1] == '1':
            break
    print(ct, len(ans) - 1)
    for i in range(1, len(ans)):
        print(ans[i][0], ans[i][1])