Language: C++

#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
#include <memory>
#include <time.h>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXIST2(s,e) (find(ALL(s),(e))!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

struct poly{
	vi a;
	int n;
	string to_s()const{
		string ret;
		for(int i=n;i>=0;i--){
			if(i<n){
				ret+="+";
			}
			if(a[i]){
				if(i==0){
					ret+=a[i]+'0';
				}else{
					if(a[i]!=1){
						ret+=a[i]+'0';
					}
					ret+="x";
				}
			}else if(n==0){
				ret+="0";
			}
		}
		return ret;
	}
};

bool operator< (const poly& left,const poly& right){
	if(left.n==right.n){
		for(int i=left.n;i>=0;i--){
			if(left.a[i]!=right.a[i]){
				return left.a[i]<right.a[i];
			}
		}
		return false;
	}else{
		return left.n<right.n;
	}
}
poly operator+(const poly& left,const poly& right){
	poly ret;
	ret.n=max(left.n,right.n);
	ret.a.resize(ret.n+1);
	int last=0;
	REP(i,ret.n+1){
		ret.a[i]=(i<left.a.size()?left.a[i]:0)+(i<right.a.size()?right.a[i]:0);
		if(ret.a[i]){
			last=i;
		}
	}
	ret.n=last;
	ret.a.resize(last+1);
	return ret;
}
poly operator-(const poly& left,const poly& right){
	poly ret;
	ret.n=max(left.n,right.n);
	ret.a.resize(ret.n+1);
	int last=0;
	REP(i,ret.n+1){
		ret.a[i]=(i<left.a.size()?left.a[i]:0)-(i<right.a.size()?right.a[i]:0);
		if(ret.a[i]){
			last=i;
		}
	}
	ret.n=last;
	ret.a.resize(last+1);
	return ret;
}
int num(string &s,int &i){
	int n=0;
	while(isdigit(s[i])){
		n*=10;
		n+=s[i]-'0';
		i++;
	}
	return n;
}
poly to_poly(const string &s){
	int i=0;
	poly p;
	while(i<s.size()){
		int k=1;
		if(isdigit(s[i])){
			k=num(s,i);
		}
		int n=0;
		if(s[i]=='x'){
			i++;
			if(s[i]=='^'){
				i++;
				n=num(s,i);
			}else{
				n=1;
			}
		}
		p.n=max(p.n,n);
		p.a.resize(p.n+1);
		p.a[n]=k;
		if(s[i]=='+'){
			i++;
		}
	}
	return p;
}
struct edge{
	int to,rev;
	poly cap;
	edge(int to,int rev,poly cap):to(to),rev(rev),cap(cap){}
};
typedef vector<edge> ve;
typedef vector<ve> vve;
const poly Zero=to_poly(string("0"));
const poly Inf=to_poly(string("x^51"));

void add_edge(int from,int to,poly& cap,vve &g){
	g[from].push_back(edge(to,g[to].size(),cap));
	g[to].push_back(edge(from,g[from].size()-1,Zero));
}

poly dfs(int v,int t,poly f,vve &g,vi &used){
	if(v==t)return f;
	used[v]=true;
	REP(i,g[v].size()){
		edge &e=g[v][i];
		if(!used[e.to]&&Zero<e.cap){
			poly d=dfs(e.to,t,min(f,e.cap),g,used);
			if(Zero<d){
				e.cap=e.cap-d;
				g[e.to][e.rev].cap=g[e.to][e.rev].cap+d;
				return d;
			}
		}
	}
	return Zero;
}
int main(){
	int n,m;
	while(cin>>n>>m,n|m){
		vve g(n,ve());
		REP(i,m){
			int u,v;
			string p;
			cin>>u>>v>>p,u--,v--;
			poly pol=to_poly(p);
			add_edge(u,v,pol,g);
			add_edge(v,u,pol,g);
		}
		poly ans=Zero;
		while(1){
			vi used(n);
			poly f=dfs(0,n-1,Inf,g,used);
			if(f.n==0&&f.a[0]==0){
				break;
			}
			ans=ans+f;
		}
		cout<<ans.to_s()<<endl;
	}
}

Compiler output:
a.cc: In function 'poly to_poly(const std::string&)':
a.cc:124:31: error: binding reference of type 'std::string&' {aka 'std::__cxx11::basic_string<char>&'} to 'const std::string' {aka 'const std::__cxx11::basic_string<char>'} discards qualifiers
  124 |                         k=num(s,i);
      |                               ^
a.cc:109:17: note:   initializing argument 1 of 'int num(std::string&, int&)'
  109 | int num(string &s,int &i){
      |         ~~~~~~~~^
a.cc:131:39: error: binding reference of type 'std::string&' {aka 'std::__cxx11::basic_string<char>&'} to 'const std::string' {aka 'const std::__cxx11::basic_string<char>'} discards qualifiers
  131 |                                 n=num(s,i);
      |                                       ^
a.cc:109:17: note:   initializing argument 1 of 'int num(std::string&, int&)'
  109 | int num(string &s,int &i){
      |         ~~~~~~~~^