记录编号 | 251544 | 评测结果 | AAAAAAAAAA | ||
---|---|---|---|---|---|
题目名称 | 能量网络 | 最终得分 | 100 | ||
用户昵称 | 是否通过 | 通过 | |||
代码语言 | C++ | 运行时间 | 0.097 s | ||
提交时间 | 2016-04-18 11:09:21 | 内存使用 | 19.21 MiB | ||
#include<cstdio> #include<vector> #include<deque> #include<iostream> #include<algorithm> using namespace std; const int SIZEN=1010,INF=0x7fffffff/2,SIZEM=550010; int N,M; int A[SIZEN],B[SIZEN]; vector<int> G[SIZEN]; vector<int> s[SIZEM]; void read() { scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) scanf("%d",&A[i]); for(int i=1;i<=N;i++) scanf("%d",&B[i]); int fr,to; for(int i=1;i<=M;i++) { scanf("%d%d",&fr,&to); G[fr].push_back(to); } } class miku { public: int fr,to,cap,flow; miku(){} miku(int a,int b,int c,int d) { fr=a;to=b;cap=c;flow=d; } }E[SIZEM]; int S,T,tot=0; bool cmp(const int &i,const int &j){ return A[i]>A[j];} void add(int fr,int to,int cap) { s[fr].push_back(tot);E[tot++]=miku(fr,to,cap,0); s[to].push_back(tot);E[tot++]=miku(to,fr,0,0); } void make_gragh() { S=0;T=550000; int c=N; for(int i=1;i<=N;i++) { add(S,i,B[i]); sort(G[i].begin(),G[i].end(),cmp); for(int j=0;j<G[i].size();j++) { int now=G[i][j]; ++c; add(now,c,INF); if(j!=(int)G[i].size()-1) add(c,T,A[now]-A[G[i][j+1]]); else add(c,T,A[now]); if(j) add(c-1,c,INF); } } } int e[SIZEM],H[SIZEM]; deque<int> Q; void push(int x,int t) { miku &v=E[t]; int flow=min(e[x],v.cap-v.flow); e[x]-=flow;v.flow+=flow; E[t^1].flow-=flow;e[v.to]+=flow; //cout<<v.to<<" "<<flow<<endl; if(v.to!=S&&v.to!=T&&e[v.to]) Q.push_back(v.to); } void use_up(int x) { int mi; while(e[x]) { mi=INF; //cout<<x<<" "<<e[x]<<endl; for(int i=0;i<s[x].size();i++) { miku &v=E[s[x][i]]; if(v.cap<=v.flow) continue; if(H[x]-1==H[v.to]) { push(x,s[x][i]); if(!e[x]) break; } else mi=min(mi,H[v.to]); } if(!e[x]) break; H[x]=mi+1; } } int maxflow() { e[S]=INF; H[S]=100; for(int i=0;i<s[S].size();i++) push(S,s[S][i]); while(!Q.empty()) { int x=Q.front();Q.pop_front(); //cout<<x<<endl; use_up(x); } return e[T]; } int main() { freopen("energynet.in","r",stdin); freopen("energynet.out","w",stdout); read(); make_gragh(); int ans=maxflow(); printf("%d\n",ans); return 0; }