标签:names code using while namespace span head empty min
#include <cstdio> #include <queue> #define N 30005 #define inf 0x3f3f3f3f using namespace std; bool vis[N]; int n,m,c,S,T,cnt=1,to[N<<1],fa[N],DIS[N],came[N],flow[N<<1],val[N<<1],head[N],nextt[N<<1]; void ins(int u,int v,int f,int w) { nextt[++cnt]=head[u];to[cnt]=v;flow[cnt]=f;val[cnt]=w;head[u]=cnt; nextt[++cnt]=head[v];to[cnt]=u;flow[cnt]=0;val[cnt]=-w;head[v]=cnt; } inline int min(int a,int b) {return a>b?b:a;} bool spfa() { for(int i=S;i<=T;++i) DIS[i]=inf,came[i]=inf,vis[i]=false; DIS[S]=0; queue<int>q; q.push(S); for(int u;!q.empty();) { u=q.front(); q.pop(); vis[u]=false; for(int i=head[u];i;i=nextt[i]) { int v=to[i]; if(DIS[v]>DIS[u]+val[i]&&flow[i]) { DIS[v]=DIS[u]+val[i]; fa[v]=i; came[v]=min(came[u],flow[i]); if(!vis[v]) {vis[v]=true;q.push(v);} } } } return DIS[T]!=inf; } int main(int argc,char *argv[]) { scanf("%d%d%d",&n,&m,&c); S=0,T=n+1; int ans=0,left=n; for(int s,t,v;m--;) { scanf("%d%d%d",&s,&t,&v); ins(s,t,1,v); ins(S,s,inf,inf-1); ins(t,T,inf,inf-1); } while(spfa()) { int t=came[T]; left-=t; for(int i=T;i!=S&&i;i=to[fa[i]^1]) { flow[fa[i]]-=t; flow[fa[i]^1]+=t; } ans+=t*DIS[T]; } printf("%d\n",ans+left*c); return 0; }
标签:names code using while namespace span head empty min
原文地址:http://www.cnblogs.com/ruojisun/p/7739700.html