Problem:
Problem I: Mobile Network

The trafic on the Internet is increasing these days due to smartphones. The wireless carriers
have to enhance their network infrastructure.

The network of a wireless carrier consists of a number of base stations and lines. Each line connects two base stations bi-directionally. The bandwidth of a line increases every year and is given by a polynomial f(x) of the year x.

Your task is, given the network structure, to write a program to calculate the maximal bandwidth between the 1-st and N-th base stations as a polynomial of x.

Input

The input consists of multiple datasets. Each dataset has the following format:

N M

u1 v1 p1

...

uM vM pM

The first line of each dataset contains two integers N (2 ≤ N ≤ 50) and M (0 ≤ M ≤ 500), which indicates the number of base stations and lines respectively. The following M lines describe the network structure. The i-th of them corresponds to the i-th network line and contains two integers ui and vi and a polynomial pi. ui and vi indicate the indices of base stations (1 ≤ ui, vi ≤ N); pi indicates the network bandwidth.

Each polynomial has the form of:

aLxL + aL-1xL-1 + ... + a2x2 + a1x + a0

where L (0 ≤ L ≤ 50) is the degree and ai's (0 ≤ i ≤ L, 0 ≤ ai ≤ 100) are the coefficients. In the input,

each term aixi (for i ≥ 2) is represented as <ai>x^<i>

the linear term (a1x) is represented as <a1>x;

the constant (a0) is represented just by digits;

these terms are given in the strictly decreasing order of the degrees and connected by a
plus sign ("+");

just like the standard notations, the <ai> is omitted if ai = 1 for non-constant terms;

similarly, the entire term is omitted if ai = 0 for any terms; and

the polynomial representations contain no space or characters other than digits, "x", "^", and "+".

For example, 2x2 + 3x + 5 is represented as 2x^2+3x+5; 2x3 + x is represented as 2x^3+x, not 2x^3+0x^2+1x+0 or the like. No polynomial is a constant zero, i.e. the one with all the coefficients being zero.

The end of input is indicated by a line with two zeros. This line is not part of any dataset.

Output

For each dataset, print the maximal bandwidth as a polynomial of x. The polynomial should be represented in the same way as the input format except that a constant zero is possible and should be represented by "0" (without quotes).

Sample Input

3 3
1 2 x+2
2 3 2x+1
3 1 x+1
2 0
3 2
1 2 x
2 3 2
4 3
1 2 x^3+2x^2+3x+4
2 3 x^2+2x+3
3 4 x+2
0 0

Output for the Sample Input

2x+3
0
2
x+2

Solution in C++ (judge verdict: Wrong Answer)

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>

using namespace std;

typedef pair<int, int> P;

struct edge{ int to; vector<int> cap; int rev; };

const int MAX_V = 55;
const int INF = 1 << 28;
const int L = 53;

vector<edge> G[MAX_V];
bool used[MAX_V];
vector<int> vec_zero(L, 0);

void add_edge(int from, int to, vector<int> cap){
  G[from].push_back((edge){to, cap, G[to].size()});
  G[to].push_back((edge){from, vec_zero, G[from].size() - 1});
}

vector<int> dfs(int v, int t, vector<int> f){
  if(v == t) return f;
  used[v] = true;
  for(int i=0;i<G[v].size();i++){
    edge &e = G[v][i];
    if(!used[e.to] && e.cap > vec_zero){
      vector<int> d = dfs(e.to, t, min(f, e.cap));
      if(d > vec_zero){
				int zero = -1;
        for(int j=L-1;j>=0;j--){
					if(zero == -1 && e.cap[j] < d[j]) zero = 1;
					if(zero == -1 && e.cap[j] > d[j]) zero = 0;
					if(zero) e.cap[j] = 0;
					else e.cap[j] -= d[j];
        }
        for(int j=0;j<L;j++) G[e.to][e.rev].cap[j] += d[j];
        return d;
      }
    }
  }
  return vec_zero;
}

vector<int> max_flow(int s, int t){
  vector<int> vec_INF = vec_zero;
  vec_INF[0] = 1;
  vector<int> flow = vec_zero;
  for(;;){
    memset(used, 0, sizeof(used));
    vector<int> f = dfs(s, t, vec_INF);
    if(f == vec_zero) return flow;
    for(int j=0;j<L;j++) flow[j] += f[j];
  }
}

void init(){
  for(int i=0;i<MAX_V;i++) G[i].clear();
}

P calc2(string s){
  int x = -1;
  for(int i=0;i<s.size();i++) if(s[i] == 'x') x = i;
  if(x == -1) return make_pair(0, atoi(s.c_str()));
  P res;
	string tmp = s.substr(0, x);
	if(tmp.size() == 0) res.second = 1;
	else res.second = atoi(tmp.c_str());
  int h = -1;
  for(int i=0;i<s.size();i++) if(s[i] == '^') res.first = atoi(s.substr(i+1).c_str());
	if(h == -1) res.first = 1;
  return res;
}

vector<int> calc(string s){
  vector<int> res(L, 0);
  int pre = 0;
  for(int i=0;;i++){
    if(i == s.size() || s[i] == '+'){
      P tmp = calc2(s.substr(pre, pre + (i - pre)));
      res[tmp.first] += tmp.second;
      if(i == s.size()) break;
			pre = i + 1;
    }
  }
  return res;
}

main(){
	int V, E;
  while(cin >> V >> E && (V|E)){
    for(int i=0;i<E;i++){
      int a, b;
      string s;
      cin >> a >> b >> s;
      a--; b--;
      vector<int> tmp = calc(s);
			reverse(tmp.begin(), tmp.end());
      add_edge(a, b, tmp);
      add_edge(b, a, tmp);
    }
    vector<int> ans = max_flow(0, V-1);
		reverse(ans.begin(), ans.end());
    bool first = true;
    for(int i=L-1;i>=0;i--){
      if(ans[i] == 0) continue;
      if(first) first = false;
      else cout << "+";
      if(i == 0) cout << ans[i];
			else{
				if(ans[i] != 1) cout << ans[i];
				if(i == 1) cout << "x";
				else cout << "x^" << i;
      }
    }
		if(first) cout << 0;
		cout << endl;
  }
}