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 |
贴个巨搓SAP代码,大牛请无视,1600+MS#include<iostream> #include<queue> using namespace std; #pragma pack(4) const int MAXV = 5010; const int MAXE = 100000; const int inf = 0x7f7f7f7f; int n,m,u,v,ans1; long long w,ans2; struct node { int v; long long w; }G[MAXE]; int _index,pre[MAXV],next[MAXE]; inline void clear(void) { _index=0; memset(pre,-1,sizeof(pre)); } inline void add(int u,int v,long long w) { G[_index].v=v; G[_index].w=w; next[_index]=pre[u]; pre[u]=_index++; } inline long long max(long long a,long long b){return a>b?a:b;} inline long long min(long long a,long long b){return a<b?a:b;} bool vis[MAXV]; int gap[MAXV],dis[MAXV],prev[MAXV],curr[MAXV]; long long SAP(int src,int des,int n) { bool flag; int u=src; long long cnt=inf,sum=0; memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); gap[0]=n; while(dis[src]!=n) { flag=false; for(int i=pre[u];i!=-1;i=next[i]) { if(G[i].w&&dis[G[i].v]==dis[u]-1) { cnt=min(cnt,G[i].w); prev[G[i].v]=u; curr[G[i].v]=i; u=G[i].v; flag=true; if(u==des) { while(u!=src) { G[curr[u]].w-=cnt; G[curr[u]^1].w+=cnt; u=prev[u]; } sum+=cnt; cnt=inf; } else break; } } if(!flag) { if(!--gap[dis[u]]) break; else { dis[u]=n; for(int i=pre[u];i!=-1;i=next[i]) { if(G[i].w) dis[u]=min(dis[u],dis[G[i].v]+1); } gap[dis[u]]++; if(u!=src) u=prev[u]; } } } return sum; } inline void DFS(int src,int des) { if(src!=des) { vis[src]=true; for(int i=pre[src];i!=-1;i=next[i]) { if(!vis[G[i].v]&&G[i].w) { DFS(G[i].v,des); } } } } int main() { while(scanf("%d %d",&n,&m)!=EOF) { clear(); ans1=ans2=0; for(int i=1;n-i>=0;i++) { scanf("%I64d",&w); if(w>=0) { add(0,i,w); add(i,0,0); ans2+=w; } else { add(i,n+1,-w); add(n+1,i,0); } } for(int i=1;m-i>=0;i++) { scanf("%d %d",&u,&v); add(u,v,inf); add(v,u,0); } ans2-=SAP(0,n+1,n+2); DFS(0,n+1); for(int i=1;n-i>=0;i++) if(vis[i]) ans1++; printf("%d %I64d\n",ans1,ans2); } return 0; } /* 5 5 8 -9 -20 12 -10 1 2 2 5 1 4 3 4 4 5 */ Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator