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 |
Runtime的神奇真相竟是....恭恭敬敬的放上代码 :) 标题只是用来勾引大牛的~ #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #define INF 0x3f3f3f3f using namespace std; const int N=20010; const int M=200010; int n,m,s,t,ect=-1; int cur[N],flo[M<<1],dis[N]; struct E {int u,v;} _ei[M<<1]; vector<int> ei[N]; void E_Add(int u,int v,int c){ _ei[++ect].u=u,_ei[ect].v=v,ei[u].push_back(ect); flo[ect]=c; _ei[++ect].u=v,_ei[ect].v=u,ei[v].push_back(ect); return; } void Clear(){ memset(flo,0,sizeof(flo)); memset(cur,0,sizeof(cur)); memset(dis,INF,sizeof(dis)); memset(_ei,0,sizeof(_ei)),ect=-1; for(int i=1;i<=N;i++) ei[i].clear(); return; } bool Dc_Bfs(){ queue<int> qi; qi.push(s),dis[s]=0; while(!qi.empty()){ int p=qi.front(); qi.pop(); for(int i=0;i<ei[p].size();i++){ int k=ei[p][i],v=_ei[k].v; if (dis[v]==INF&&flo[k]){ qi.push(v); dis[v]=dis[p]+1; } } } return dis[t]==INF?0:1; } int Dc_Dfs(int p,int low){ if (p==t) return low; for(int i=cur[p];i<ei[p].size();i++){ int k=ei[p][i],v=_ei[k].v,flow; if (dis[v]==dis[p]+1&&flo[k]&&(flow=Dc_Dfs(v,min(low,flo[k])))){ cur[p]=i; flo[k]-=flow; flo[k^1]+=flow; return flow; } } return 0; } int main(){ while(~scanf("%d%d",&n,&m)){ s=0,t=n+1; Clear(); for(int i=1;i<=n;i++){ int c1,c2; cin>>c1>>c2; E_Add(s,i,c1),E_Add(i,t,c2); } for(int i=1;i<=m;i++){ int u,v,c; cin>>u>>v>>c; E_Add(u,v,c),E_Add(v,u,c); } int ans=0,d; while(Dc_Bfs()){ while(d=Dc_Dfs(s,INF)) ans+=d; memset(cur,0,sizeof(cur)),memset(dis,INF,sizeof(dis)); } printf("%d\n",ans); } return 0; } 用的是DINIC算法,然而惨败而归... Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator