'''
Bellman-Ford算法,解带负权边的有向图和不带负权边的无向图的单源
最短路问题
'''
class BellmanFord:
# start_node 为起始点,edges是边的三元组(节点1, 节点2, 边权重) is_directed表示是否是有向图
def __init__(self, start_node, edges, is_directed, K):
self.edges = edges[::]
self.start_node = start_node
self.is_directed = is_directed
self.K = K
# 获取最短路径长度的列表[(节点1,长度1), (节点2, 长度2) .....]
def getMinPathLen(self):
return self.getMinInfo(False)
# 获取所有最短路径列表[(节点1, 长度1, 路径节点列表1), (节点2, 长度2, 路径节点列表2)]
def getMinPath(self):
return self.getMinInfo(True)
def getMinInfo(self, get_path):
link = {}
pre = {} # pre[x]表示以节点x结尾的最短路径的前驱节点
dis, dis_tmp = {}, {} # 起点到终点的最短距离
for a, b, w in self.edges:
if a not in dis:
dis[a] = 0 if a == self.start_node else 0x7fffffff
if b not in dis:
dis[b] = 0 if b == self.start_node else 0x7fffffff
if not self.is_directed:
# 无向图检查是否有负边
if w < 0:
return None
if a not in link:
link[a] = []
if b not in link:
link[b] = []
link[a].append((b, w))
link[b].append((a, w))
else:
if a not in link:
link[a] = []
link[a].append((b, w))
# 进行V次迭代,第V次检查是否是无解情况
iter_times = 0
while iter_times < self.K:
# 所有边进行松弛操作
update_flag = False
for node in dis:
dis_tmp[node] = dis[node]
for a in link:
for b, w in link[a]:
new_dis = 0x7fffffff if dis[a] == 0x7fffffff else dis[a] + w
if new_dis < dis_tmp[b]:
dis_tmp[b] = new_dis
pre[b] = a
update_flag = True
dis, dis_tmp = dis_tmp, dis
iter_times += 1
if not update_flag:
break
# 生成路径函数
def __getPath(end_node):
stack = []
node = end_node
while True:
stack.append(node)
if node not in pre:
break
node = pre[node]
return stack[::-1]
return [[node, dis[node], __getPath(node)] for node in dis] if get_path else [[node, dis[node]] for node in dis]
n, m, k = map(int, input().split())
edges = {}
for _ in range(m):
a, b, w = map(int, input().split())
if (a, b) not in edges:
edges[(a, b)] = w
else:
edges[(a, b)] = min(edges[(a, b)], w)
edges = [(a, b, w) for (a, b), w in edges.items()]
algo = BellmanFord(1, edges, is_directed=True, K=k)
ans = algo.getMinPathLen()
for k, v in ans:
if k == n:
print(v if v != 0x7fffffff else 'impossible')
break