Language: C++

#include <bits/stdc++.h>
#define int long long
#define INF 1e9
#define MAX_V 505
using namespace std;
/*?????§?????¢?????´?????????(Ford_Fulerson????????¨)???O(F|E|)*/

//????????¨????§???????(???????????????????????????)
struct edge{
  int to;
  vector<int> cap;
  int rev;
};

vector<edge> G[MAX_V]; //??°???????????£??\?????????
bool used[MAX_V];      //DFS??§?????§??????????????????????????°

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

bool check(vector<int> &A){
  for(int i=0;i<A.size();i++)
    if(A[i]!=0) return A[i]>0;
  return false;
}

bool check2(vector<int> &A){
  for(int i=0;i<A.size();i++)
    if(A[i]==INF) return false;
  return true;
}

void Minus(vector<int> &res, vector<int> &v){
  for(int i=0;i<res.size();i++) res[i]-=v[i];
}

void Plus(vector<int> &res, vector<int> &v){
  for(int i=0;i<res.size();i++) res[i]+=v[i];
}

//?¢?????????????DFS??§??¢???
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] && check(e.cap) ){
      vector<int> d = dfs(e.to ,t , min(f,e.cap));
      if(check(d)&&check2(d)){
	Minus(e.cap, d);
	Plus(G[e.to][e.rev].cap, d);
	return d;
      }
    }
  }
  return vector<int>((int)f.size(),0);
}

bool isall0(vector<int> v){
  for(int i=0;i<v.size();i++)
    if(v[i]!=0) return false;
  return true;
}

bool isallnotINF(vector<int> v){
  for(int i=0;i<v.size();i++)
    if(v[i]>=INF) return false;
  return true;
}

int V=55;

//s??????t???????????§???????±???????
vector<int> max_flow(int s,int t){
  vector<int> flow = vector<int>(V,0);
  for(;;){
    memset(used,0,sizeof(used));
    vector<int> f = dfs(s, t, vector<int>(V,INF));
    if(isall0(f)) return flow;
    if(isallnotINF(f)) Plus(flow, f);
  }
}

vector<int> tov(string s){
  
  vector<int> res = vector<int>(V,0);
  
  int num1, num2, p=0;
  
  if('0'<=s[p]&&s[p]<='9'){
    num1=0;
    while('0'<=s[p]&&s[p]<='9') num1=num1*10+s[p++]-'0';
  }else num1=1;
  
  if(p==s.size()){
    num2=0;
  }else{
    if(p+1==s.size()) num2=1;
    else{
      p+=2;
      num2=0;
      while('0'<=s[p]&&s[p]<='9') num2=num2*10+s[p++]-'0';
    }
  }
  
  res[num2]=num1;
  
  return res;
}

vector<int> tocap(string s){
  
  vector<int> res = vector<int>(V,0);
  s+='+';
  
  string t;
  
  for(int i=0;i<s.size();i++){
    if(s[i]=='+'){
      Plus(res,tov(t));
      t="";
    }else t+=s[i];
  }
  
  reverse(res.begin(), res.end());
  
  return res;
}

string itos(int num){

  string res;
  
  while(num){
    res=(char)(num%10+'0')+res;
    num/=10;
  }
  
  return res;
}

void output(vector<int> ans){
  
  reverse(ans.begin(), ans.end());
  
  for(int i=0;i<ans.size();i++)
    if(ans[i]<0) ans[i]=0;
  
  string s;
  int flag=0;
  
  for(int i=ans.size()-1;i>=0;i--){
    if(ans[i]==0) continue;
    if(flag) s+="+";
    flag=1;
    if(i==0||(i!=0&&ans[i]!=1)) s+=itos(ans[i]);
    if(i==1) s+="x";
    else if(i) s+="x^", s+=itos(i);
  }
  if(s=="") s="0";
  cout<<s<<endl;
}

signed main(){
    
  int n, m;

  while(1){
    
    cin>>n>>m;
    if(!n&&!m) break;
    
    int a, b;
    string s;
    
    for(int i=0;i<m;i++){
      cin>>a>>b>>s;
      vector<int> cap=tocap(s);
      add_edge(a-1,b-1,cap);
      add_edge(b-1,a-1,cap);
    }

    output(max_flow(0,n-1));
    
    for(int i=0;i<MAX_V;i++) G[i].clear();
  }
  
  return 0;
}

Compiler output:
a.cc: In function 'std::vector<long long int> tocap(std::string)':
a.cc:123:19: error: cannot bind non-const lvalue reference of type 'std::vector<long long int>&' to an rvalue of type 'std::vector<long long int>'
  123 |       Plus(res,tov(t));
      |                ~~~^~~
a.cc:40:42: note:   initializing argument 2 of 'void Plus(std::vector<long long int>&, std::vector<long long int>&)'
   40 | void Plus(vector<int> &res, vector<int> &v){
      |                             ~~~~~~~~~~~~~^