Buggy python submission (verdict: Time Limit Exceeded):
V, E = map(int, raw_input().split())
g = [[] for i in xrange(V)]
for i in xrange(E):
    s, t, d = map(int, raw_input().split())
    g[s].append((t, d))
INF = 10**9
dp = [[-1]*V for i in xrange(2**V)]
tmp = {}
def dfs(state, v, s):
    if dp[state][v] != -1:
        return dp[state][v]
    if state == 2**V-1:
        for t, d in g[v]:
            if t==s:
                dp[state][v] = d
                return d
        return INF
    res = INF
    idx = -1
    for t, d in g[v]:
        if state & (1 << t):
            continue
        r = dfs(state | (1 << t), t, s) + d
        if r < res:
            idx = t
        res = min(res, r)

    tmp[state, v] = (idx, res)

    return res
def solve(v):
    global dp
    dp = [[-1]*V for i in xrange(2**V)]
    return dfs(1<<v, v, v)
res = min(solve(v) for v in xrange(V))
print res if res<INF else -1

Fixed submission:
import sys
readline = sys.stdin.readline
V, E = map(int, readline().split())
g = [[] for i in xrange(V)]
cost = {}
to = [0]*V
for i in xrange(E):
    s, t, d = map(int, readline().split())
    g[s].append((t, d))
    cost[s,t] = d
    to[s] |= 1 << t
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = {}
start = 0
def dfs(state, v):
    key = (state, v)
    if key in dp: return dp[key]
    if state == ALL:
        dp[key] = res = get((v, start), INF)
        return res
    dp[key] = res = min((dfs(state | (1 << t), t) + d for t, d in g[v] if not state & (1 << t))) if state & to[v] != to[v] else INF
    return res
res = dfs(1, 0)
print res if res<INF else -1

Buggy python submission (verdict: Time Limit Exceeded):
V, E = map(int, raw_input().split())
g = [[] for i in xrange(V)]
cost = {}
for i in xrange(E):
    s, t, d = map(int, raw_input().split())
    g[s].append((t, d))
    cost[s,t] = d
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = [[-1]*V for i in xrange(2**V)]
def dfs(state, v, s):
    if dp[state][v] != -1:
        return dp[state][v]
    if state == ALL:
        res = get((v, s), INF)
        dp[state][v] = res
        return res
    res = INF
    for t, d in g[v]:
        if not state & (1 << t):
            res = min(res, dfs(state | (1 << t), t, s) + d)
    dp[state][v] = res
    return res
def solve(v):
    global dp
    dp = [[-1]*V for i in xrange(2**V)]
    return dfs(1<<v, v, v)
res = min(solve(v) for v in xrange(V))
print res if res<INF else -1

Fixed submission:
import sys
readline = sys.stdin.readline
V, E = map(int, readline().split())
g = [[] for i in xrange(V)]
cost = {}
to = [0]*V
for i in xrange(E):
    s, t, d = map(int, readline().split())
    g[s].append((t, d))
    cost[s,t] = d
    to[s] |= 1 << t
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = {}
start = 0
def dfs(state, v):
    key = (state, v)
    if key in dp: return dp[key]
    if state == ALL:
        dp[key] = res = get((v, start), INF)
        return res
    dp[key] = res = min((dfs(state | (1 << t), t) + d for t, d in g[v] if not state & (1 << t))) if state & to[v] != to[v] else INF
    return res
res = dfs(1, 0)
print res if res<INF else -1

Buggy python submission (verdict: Time Limit Exceeded):
V, E = map(int, raw_input().split())
g = [[] for i in xrange(V)]
cost = {}
for i in xrange(E):
    s, t, d = map(int, raw_input().split())
    g[s].append((t, d))
    cost[s,t] = d
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = {}
def dfs(state, v, s):
    key = (state, v)
    if key in dp:
        return dp[key]
    if state == ALL:
        res = get((v, s), INF)
        dp[key] = res
        return res
    res = INF
    for t, d in g[v]:
        if not state & (1 << t):
            res = min(res, dfs(state | (1 << t), t, s) + d)
    dp[key] = res
    return res
def solve(v):
    global dp
    dp = {}
    return dfs(1<<v, v, v)
res = min(solve(v) for v in xrange(V))
print res if res<INF else -1

Fixed submission:
import sys
readline = sys.stdin.readline
V, E = map(int, readline().split())
g = [[] for i in xrange(V)]
cost = {}
to = [0]*V
for i in xrange(E):
    s, t, d = map(int, readline().split())
    g[s].append((t, d))
    cost[s,t] = d
    to[s] |= 1 << t
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = {}
start = 0
def dfs(state, v):
    key = (state, v)
    if key in dp: return dp[key]
    if state == ALL:
        dp[key] = res = get((v, start), INF)
        return res
    dp[key] = res = min((dfs(state | (1 << t), t) + d for t, d in g[v] if not state & (1 << t))) if state & to[v] != to[v] else INF
    return res
res = dfs(1, 0)
print res if res<INF else -1

Buggy python submission (verdict: Time Limit Exceeded):
import sys
readline = sys.stdin.readline
V, E = map(int, readline().split())
g = [[] for i in xrange(V)]
cost = {}
to = [0]*V
for i in xrange(E):
    s, t, d = map(int, readline().split())
    g[s].append((t, d))
    cost[s,t] = d
    to[s] |= 1 << t
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = {}
start = -1
def dfs(state, v):
    key = (state, v)
    if key in dp: return dp[key]
    if state == ALL:
        res = get((v, start), INF)
        dp[key] = res
        return res
    if state & to[v] != to[v]:
        res = min((dfs(state | (1 << t), t) + d for t, d in g[v] if not state & (1 << t)))
    else:
        res = INF
    dp[key] = res
    return res
def solve(v):
    global dp, start
    dp = {}
    start = v
    return dfs(1<<v, v)
res = min(solve(v) for v in xrange(V))
print res if res<INF else -1

Fixed submission:
import sys
readline = sys.stdin.readline
V, E = map(int, readline().split())
g = [[] for i in xrange(V)]
cost = {}
to = [0]*V
for i in xrange(E):
    s, t, d = map(int, readline().split())
    g[s].append((t, d))
    cost[s,t] = d
    to[s] |= 1 << t
get = cost.get
INF = 10**9
ALL = 2**V-1
dp = {}
start = 0
def dfs(state, v):
    key = (state, v)
    if key in dp: return dp[key]
    if state == ALL:
        dp[key] = res = get((v, start), INF)
        return res
    dp[key] = res = min((dfs(state | (1 << t), t) + d for t, d in g[v] if not state & (1 << t))) if state & to[v] != to[v] else INF
    return res
res = dfs(1, 0)
print res if res<INF else -1