hdu3157有源汇上下界最小流
题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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-12; const int N=300+10,maxn=90000+10,inf=0x3f3f3f3f; struct edge{ int from,to,c,Next,low; }e[maxn<<1]; 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=0; e[cnt].low=low; e[cnt].Next=head[v]; head[v]=cnt++; } bool bfs(int s,int t) { memset(dis,-1,sizeof dis); dis[s]=1; queue<int>q; q.push(s); while(!q.empty()) { int x=q.front(); q.pop(); if(x==t)return 1; for(int i=head[x];~i;i=e[i].Next) { int te=e[i].to; if(dis[te]==-1&&e[i].c>0) { dis[te]=dis[x]+1; q.push(te); } } } return 0; } int dfs(int x,int mx,int t) { if(x==t)return mx; int flow=0; for(int i=head[x];~i;i=e[i].Next) { int te=e[i].to,f; if(dis[te]==dis[x]+1&&e[i].c>0&&(f=dfs(te,min(mx-flow,e[i].c),t))) { e[i].c-=f; e[i^1].c+=f; flow+=f; } } if(!flow)dis[x]=-2; return flow; } int maxflow(int s,int t) { int ans=0,f; while(bfs(s,t)) { while((f=dfs(s,inf,t)))ans+=f; } return ans; } void init() { cnt=0; memset(head,-1,sizeof head); memset(in,0,sizeof in); memset(out,0,sizeof out); } int change(string a,int s,int t) { if(a=="+")return s; else if(a=="-")return t; else { int ans=0; for(int i=0;i<a.size();i++) ans=ans*10+a[i]-'0'; return ans; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n,m; while(cin>>n>>m) { if(!n&&!m)break; init(); int s=n+m+1,t=n+m+2; for(int i=0;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+3,tt=n+m+4,sum=0; for(int i=1;i<=n+m+2;i++) { if(in[i]>out[i])add(ss,i,in[i]-out[i],0),sum+=in[i]-out[i]; else add(i,tt,out[i]-in[i],0); } int flow=maxflow(ss,tt); add(t,s,inf,0); flow+=maxflow(ss,tt); if(flow!=sum)cout<<"impossible"<<endl; else cout<<e[cnt-1].c<<endl; } return 0; } /******************** ********************/