Given a graph whose edge capacities are polynomials in x, output the max-flow polynomial from node 1 to node N for large x.
Hard9GraphGreedyMathImplementationNo attempts yetTime limit8sMemory limit512 MBInternet traffic keeps growing because of smartphones, so wireless carriers have to expand their networks.
The network of a carrier consists of base stations and lines. Each line connects two base stations in both directions. The bandwidth of a line grows every year and is given as a polynomial f(x) of the year x.
Given the structure of the network, compute the maximum bandwidth between base station 1 and base station N as a polynomial of x.
The traffic between the two stations can be split over several routes. The traffic on a line cannot exceed the bandwidth of that line, so the maximum bandwidth equals the maximum flow from station 1 to station N.
Which line is the bottleneck depends on x. Exactly one polynomial equals the maximum bandwidth for every sufficiently large x. Print that polynomial.
The input consists of several datasets. Each dataset has the following format.
N M
u1 v1 p1
...
uM vM pM
The first line contains the number of base stations N (2≤N≤50) and the number of lines M (0≤M≤500). The next M lines describe the network. The i-th of them contains the station indices ui and vi (1≤ui,vi≤N) and the polynomial pi that gives the bandwidth of the line. Several lines may connect the same pair of stations, and a line may have both of its ends at the same station.
A polynomial has the form
aLxL+aL−1xL−1+⋯+a1x+a0
where L (0≤L≤50) is the degree and ai (0≤i≤L, 0≤ai≤100) are the coefficients. The input writes a polynomial by these rules.
<a_i>x^<i>.<a_1>x.+.x, ^, and +.So 2x2+3x+5 is written as 2x^2+3x+5, and 2x3+x is written as 2x^3+x, never as 2x^3+0x^2+1x+0. No polynomial in the input has all coefficients equal to 0.
The end of the input is a line with two zeros. That line is not a dataset.
For each dataset, print the maximum bandwidth as a polynomial of x on one line. Use the same notation as the input, except that the answer may be the constant 0, which is printed as 0.