Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city.

In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with n nodes and m edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node n. The total weight that travels across a particular edge must not exceed the weight capacity of that edge.

Niwel has exactly x bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like.

Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight.


-----Input-----

The first line contains three integers n, m and x (2 ≤ n ≤ 50, 1 ≤ m ≤ 500, 1 ≤ x ≤ 100 000) — the number of nodes, the number of directed edges and the number of bears, respectively.

Each of the following m lines contains three integers a_{i}, b_{i} and c_{i} (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}, 1 ≤ c_{i} ≤ 1 000 000). This represents a directed edge from node a_{i} to b_{i} with weight capacity c_{i}. There are no self loops and no multiple edges from one city to the other city. More formally, for each i and j that i ≠ j it's guaranteed that a_{i} ≠ a_{j} or b_{i} ≠ b_{j}. It is also guaranteed that there is at least one path from node 1 to node n.


-----Output-----

Print one real value on a single line — the maximum amount of weight Niwel can deliver if he uses exactly x bears. Your answer will be considered correct if its absolute or relative error does not exceed 10^{ - 6}.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if $\frac{|a - b|}{\operatorname{max}(1, b)} \leq 10^{-6}$.


-----Examples-----
Input
4 4 3
1 2 2
2 4 1
1 3 1
3 4 2

Output
1.5000000000

Input
5 11 23
1 2 3
2 3 4
3 4 5
4 5 6
1 3 4
2 4 5
3 5 6
1 4 2
2 5 3
1 5 2
3 2 30

Output
10.2222222222



-----Note-----

In the first sample, Niwel has three bears. Two bears can choose the path $1 \rightarrow 3 \rightarrow 4$, while one bear can choose the path $1 \rightarrow 2 \rightarrow 4$. Even though the bear that goes on the path $1 \rightarrow 2 \rightarrow 4$ can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.

Source: codeforces | Difficulty: HARD
Tags: Sorting

Graph algorithms

Flows and cuts


Solutions:
from collections import deque

class Dinic:

	def __init__(self, listEdge, s, t):
		self.s = s
		self.t = t
		self.graph = {}
		self.maxCap = 1000000
		for e in listEdge:
			if e[0] not in self.graph:
				self.graph[e[0]] = []
			if e[1] not in self.graph:
				self.graph[e[1]] = []
			self.graph[e[0]].append([e[1], e[2], len(self.graph[e[1]])])
			self.graph[e[1]].append([e[0], 0, len(self.graph[e[0]]) - 1])
		self.N = len(self.graph.keys())

	def bfs(self):
		self.dist = {}
		self.dist[self.s] = 0
		self.curIter = {node: [] for node in self.graph}
		Q = deque([self.s])
		while len(Q) > 0:
			cur = Q.popleft()
			for (index, e) in enumerate(self.graph[cur]):
				if e[1] > 0 and e[0] not in self.dist:
					self.dist[e[0]] = self.dist[cur] + 1
					self.curIter[cur].append(index)
					Q.append(e[0])

	def findPath(self, cur, f):
		if cur == self.t:
			return f
		while len(self.curIter[cur]) > 0:
			indexEdge = self.curIter[cur][-1]
			nextNode = self.graph[cur][indexEdge][0]
			remainCap = self.graph[cur][indexEdge][1]
			indexPreEdge = self.graph[cur][indexEdge][2]
			if remainCap > 0 and self.dist[nextNode] > self.dist[cur]:
				flow = self.findPath(nextNode, min(f, remainCap))
				if flow > 0:
					self.path.append(cur)
					self.graph[cur][indexEdge][1] -= flow
					self.graph[nextNode][indexPreEdge][1] += flow
					return flow
			self.curIter[cur].pop()
		return 0

	def maxFlow(self):
		maxflow = 0
		flow = []
		while True:
			self.bfs()
			if self.t not in self.dist:
				break
			while True:
				self.path = []
				f = self.findPath(self.s, self.maxCap)
				if f == 0:
					break
				flow.append(f)
				maxflow += f
		return maxflow

	def residualBfs(self):
		Q = deque([self.s])
		side = {self.s: 's'}
		while len(Q) > 0:
			cur = Q.popleft()
			for (index, e) in enumerate(self.graph[cur]):
				if e[1] > 0 and e[0] not in side:
					Q.append(e[0])
					side[e[0]] = 's'
		S = []
		T = []
		for x in self.graph:
			if x in side:
				S.append(x)
			else:
				T.append(x)
		return (set(S), set(T))
(n, m, X) = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(m)]
(l, r) = (0, 1000000)
while r - l > 1e-09:
	md = (r + l) / 2
	edge_ = [[u, v, w // md] for (u, v, w) in edge]
	g = Dinic(edge_, 1, n)
	maxflow = g.maxFlow()
	if maxflow >= X:
		l = md
	else:
		r = md
print(X * r)


from queue import Queue

def addEdge(s, t, flow):
	E[s].append((len(E[t]), t, flow))
	E[t].append((len(E[s]) - 1, s, 0))

def mkLevel():
	nonlocal src, des, E, lvl
	for i in range(n):
		lvl[i] = -1
	lvl[src] = 0
	q = Queue()
	q.put(src)
	while not q.empty():
		cur = q.get()
		for j in range(len(E[cur])):
			to = E[cur][j][1]
			if lvl[to] < 0 and E[cur][j][2] > 0:
				lvl[to] = lvl[cur] + 1
				q.put(to)
				if to == des:
					return True
	return False

def extend(cur, lim):
	nonlocal des, E
	if lim == 0 or cur == des:
		return lim
	flow = 0
	for j in range(len(E[cur])):
		if flow >= lim:
			break
		to = E[cur][j][1]
		lim0 = min(lim - flow, E[cur][j][2])
		if E[cur][j][2] > 0 and lvl[to] == lvl[cur] + 1:
			newf = extend(to, lim0)
			if newf > 0:
				E[cur][j] = (E[cur][j][0], E[cur][j][1], E[cur][j][2] - newf)
				jj = E[cur][j][0]
				E[to][jj] = (E[to][jj][0], E[to][jj][1], E[to][jj][2] + newf)
				flow += newf
	if flow == 0:
		lvl[cur] = -1
	return flow

def Dinic():
	flow = 0
	newf = 0
	while mkLevel():
		newf = extend(src, INF)
		while newf > 0:
			flow += newf
			newf = extend(src, INF)
	return flow

def check(mid):
	nonlocal E
	E = [[] for i in range(n)]
	for i in range(m):
		if w[i] - bears * mid > 0:
			addEdge(u[i], v[i], bears)
		else:
			addEdge(u[i], v[i], int(w[i] / mid))
	return Dinic() >= bears
(n, m, bears) = list(map(int, input().split()))
INF = 1061109567
src = 0
des = n - 1
lo = 0.0
hi = 0.0
u = [0 for i in range(m)]
v = [0 for i in range(m)]
w = [0 for i in range(m)]
for i in range(m):
	(u[i], v[i], w[i]) = list(map(int, input().split()))
	u[i] -= 1
	v[i] -= 1
	hi = max(hi, w[i])
E = [[] for i in range(n)]
lvl = [0 for i in range(n)]
for i in range(100):
	mid = (lo + hi) / 2
	if check(mid):
		lo = mid
	else:
		hi = mid
print('{:.10f}'.format(mid * bears))


from collections import defaultdict, deque
adj = defaultdict(lambda : defaultdict(lambda : 0))

def bfs(graph, inicio, destino, parent):
	parent.clear()
	queue = deque()
	queue.append([inicio, float('Inf')])
	parent[inicio] = -2
	while len(queue):
		(current, flow) = queue.popleft()
		for i in adj[current]:
			if parent[i] == -1 and graph[current][i] > 0:
				parent[i] = current
				flow = min(flow, graph[current][i])
				if i == destino:
					return flow
				queue.append((i, flow))
	return 0

def maxflow(graph, inicio, destino):
	flow = 0
	parent = defaultdict(lambda : -1)
	while True:
		t = bfs(graph, inicio, destino, parent)
		if t:
			flow += t
			current = destino
			while current != inicio:
				prev = parent[current]
				graph[prev][current] -= t
				graph[current][prev] += t
				current = prev
		else:
			break
	return flow
(n, m, x) = [int(i) for i in input().split()]
for _ in range(m):
	t = [int(i) for i in input().split()]
	adj[t[0]][t[1]] = t[2]

def check(k):
	meh = defaultdict(lambda : defaultdict(lambda : 0))
	for i in adj:
		for j in adj[i]:
			ww = adj[i][j] // k
			meh[i][j] = ww
	flow = maxflow(meh, 1, n)
	return flow
lo = 1 / x
hi = check(1)
for _ in range(70):
	mid = (hi + lo) / 2
	if hi - lo < 1e-10:
		break
	if check(mid) >= x:
		lo = mid
	else:
		hi = mid
print(format(lo * x, '.9f'))