Find an s-t flow minimizing squared cost plus squared shortfall from max flow in a capacitated graph with edge costs, and print the minimum as a fraction.
Hard8GraphShortest pathMathNo attempts yetTime limit2sMemory limit256 MBYayoi is an expert at saving money. She never picks an item just because it is cheap, and her motto is "cost performance". That is one reason she cooks so well with bean sprouts. Word of her thrift has spread, so people keep asking her to cut costs of all kinds. This time the job is optimizing a network flow.
Consider a directed graph G=(V,E), where V={1,2,…,∣V∣} is the vertex set and E⊂V×V is the edge set. Every edge e has a capacity u(e) and a cost c(e). For two vertices s and t, a function fs,t:E→R is an s-t flow when both of the following hold.
The value and the cost of fs,t are defined as follows.
F(fs,t)=∑e=(s,v)∈Efs,t(e)−∑e=(v,s)∈Efs,t(e),C(fs,t)=∑e∈Efs,t(e)c(e)
Optimizing a network flow usually means minimizing the cost among the maximum flows. Yayoi judges by cost performance instead. Let Fmax=maxfF(f) be the maximum value over all s-t flows f, and define the balance function of an s-t flow fs,t as follows.
B(fs,t)=C(fs,t)2+(Fmax−F(fs,t))2
Yayoi regards the flow that minimizes B as the one with the best cost performance. Compute the minimum of B(fs,t) over all s-t flows.
The input is a single test case. The first line has two integers separated by one space: the number of vertices N (2≤N≤100) and the number of edges M (1≤M≤1000). The second line has two vertices s and t (1≤s,t≤N, s=t). Each of the following M lines describes one edge with four integers ai, bi, ui, ci. The i-th edge goes from ai to bi (1≤ai,bi≤N), its capacity is ui (1≤ui≤100), and its cost is ci (1≤ci≤100). For every i, ai=bi, and (ai,bi)=(aj,bj) whenever i=j.
Print the minimum of B on one line as an irreducible fraction. Precisely, print "u/d", where u is the numerator and d is the denominator. Both u and d must be non-negative integers whose greatest common divisor is 1. The answer is always a rational number. If the minimum is 0, print "0/1".