题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; struct edge{
int from,to,c,Next,low;
}e[maxn<<];
int cnt,head[N];
int dis[N];
int in[N],out[N];
void add(int u,int v,int c,int low)
{
out[u]+=low;
in[v]+=low;
e[cnt].from=u;
e[cnt].to=v;
e[cnt].c=c;
e[cnt].low=low;
e[cnt].Next=head[u];
head[u]=cnt++;
e[cnt].from=v;
e[cnt].to=u;
e[cnt].c=;
e[cnt].low=low;
e[cnt].Next=head[v];
head[v]=cnt++;
}
bool bfs(int s,int t)
{
memset(dis,-,sizeof dis);
dis[s]=;
queue<int>q;
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
if(x==t)return ;
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to;
if(dis[te]==-&&e[i].c>)
{
dis[te]=dis[x]+;
q.push(te);
}
}
}
return ;
}
int dfs(int x,int mx,int t)
{
if(x==t)return mx;
int flow=;
for(int i=head[x];~i;i=e[i].Next)
{
int te=e[i].to,f;
if(dis[te]==dis[x]+&&e[i].c>&&(f=dfs(te,min(mx-flow,e[i].c),t)))
{
e[i].c-=f;
e[i^].c+=f;
flow+=f;
}
}
if(!flow)dis[x]=-;
return flow;
}
int maxflow(int s,int t)
{
int ans=,f;
while(bfs(s,t))
{
while((f=dfs(s,inf,t)))ans+=f;
}
return ans;
}
void init()
{
cnt=;
memset(head,-,sizeof head);
memset(in,,sizeof in);
memset(out,,sizeof out);
}
int change(string a,int s,int t)
{
if(a=="+")return s;
else if(a=="-")return t;
else
{
int ans=;
for(int i=;i<a.size();i++)
ans=ans*+a[i]-'';
return ans;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,m;
while(cin>>n>>m)
{
if(!n&&!m)break;
init();
int s=n+m+,t=n+m+;
for(int i=;i<m;i++)
{
string a,b;
int c;
cin>>a>>b>>c;
add(change(a,s,t),change(b,s,t),inf-c,c);
}
int ss=n+m+,tt=n+m+,sum=;
for(int i=;i<=n+m+;i++)
{
if(in[i]>out[i])add(ss,i,in[i]-out[i],),sum+=in[i]-out[i];
else add(i,tt,out[i]-in[i],);
}
int flow=maxflow(ss,tt);
add(t,s,inf,);
flow+=maxflow(ss,tt);
if(flow!=sum)cout<<"impossible"<<endl;
else cout<<e[cnt-].c<<endl;
}
return ;
}
/******************** ********************/