Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
发一个短小精悍的递归版isap 效率还过得去 468ms#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int inf=100000000,maxinf=2000000000; struct Edge{int u,v,c;}; Edge edge[150000]; int first[5510],nxt[150000],d[5510],num[5510],tot,s,t,node; bool vis[5510]; void add(int u,int v,int c) { edge[tot].u=u; edge[tot].v=v; edge[tot].c=c; nxt[tot]=first[u]; first[u]=tot++; } int aug(int u,int res) { if(u==t) return res; int flow=0,mindis=node; for(int i=first[u];i!=-1;i=nxt[i]) { Edge &e=edge[i]; if(e.c) { if(d[e.v]+1==d[u]) { long long inc=aug(e.v,min(res-flow,e.c)); e.c-=inc; edge[i^1].c+=inc; flow+=inc; if(flow==res) break; if(d[s]>=node) return flow; } if(d[e.v]<mindis) mindis=d[e.v]; } } if(!flow) { if(!(--num[d[u]])) d[s]=node; num[d[u]=mindis+1]++; } return flow; } long long maxflow() { long long sumflow=0; for(int i=0;i<=node;i++) d[i]=num[i]=0; num[0]=node; while(d[s]<node) sumflow+=aug(s,maxinf); return sumflow; } int cnt; void dfs(int u) { vis[u]=1; for(int i=first[u];i!=-1;i=nxt[i]) { int v=edge[i].v; if(!vis[v] && edge[i].c) dfs(v),cnt++;; } } int main() { int n,m,u,v,k; long long ans; while(scanf("%d%d",&n,&m)!=-1) { memset(first,-1,sizeof(first)); tot=0; ans=0; for(int i=1;i<=n;i++) { scanf("%d",&k); if(k>0) add(0,i,k),add(i,0,0),ans+=k; else if(k<0) add(i,n+1,-k),add(n+1,i,0); } while(m--) { scanf("%d%d",&u,&v); add(u,v,inf); add(v,u,0); } s=0,t=n+1,node=n+2; ans-=maxflow(); cnt=0; memset(vis,0,sizeof(vis)); dfs(0); printf("%d %lld\n",cnt,ans); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator