POJ3469 代码
来源:互联网 发布:js时间戳怎么用 编辑:程序博客网 时间:2024/06/05 05:41
Source Code
Problem: 3469 User: forsonaMemory: 14596K Time: 3329MSLanguage: C++ Result: Accepted- Source Code
#include"iostream"#include"cstdio"#include"algorithm"#define INF 999999999using namespace std;struct edge{ int p,q,maxf,f; }e[1000010];int ep,map[1000020][2],mp,n,m;int h[20010],current[20010],num[40050],so,to,pre[20010],from[20010];int que[20010],more[20010];void Add(int p,int q,int maxf){ e[ep].p=p; e[ep].q=q; e[ep].maxf=maxf; e[ep].f=0; map[mp][0]=ep; map[mp][1]=map[p][1]; map[p][1]=mp++; map[mp][0]=ep; map[mp][1]=map[q][1]; map[q][1]=mp++; ep++;}void Inp(void){ int i,j,k,t; scanf("%d%d",&n,&m); so=0; to=n+1; mp=to+1; for(i=1;i<=n;i++) { scanf("%d%d",&j,&k); Add(so,i,j); Add(i,to,k); } for(i=0;i<m;i++) { scanf("%d%d%d",&j,&k,&t); Add(j,k,t); Add(k,j,t); } }void Greedy(void){ int t,ep,delta,p,q,qbeg,qend; for(t=map[so][1];t;t=map[t][1]) { ep=map[t][0]; more[so]=e[ep].maxf; } qbeg=0; qend=1; que[0]=so; while(qbeg<qend) { p=que[qbeg++]; for(t=map[p][1];t;t=map[t][1]) { ep=map[t][0]; if (e[ep].p==p&&h[e[ep].q]==h[p]-1) { delta=min(more[p],e[ep].maxf-e[ep].f); e[ep].f+=delta; more[e[ep].q]+=delta; more[p]-=delta; que[qend++]=e[ep].q; if (!more[p]) break; } } } for(qbeg=qend-1;qbeg>0;qbeg--) { p=que[qbeg]; if (p==to) continue; for(t=map[p][1];t;t=map[t][1]) { ep=map[t][0]; if (e[ep].q==p&&h[e[ep].p]==h[p]+1) { delta=min(more[p],e[ep].f); e[ep].f-=delta; more[e[ep].p]+=delta; more[p]-=delta; if (!more[p]) break; } } }}void BFS(void){ int qbeg,qend,i,t,p,ep; for(i=so;i<to;i++) h[i]=to+1; h[to]=0; qbeg=0; qend=1; que[qbeg]=to; num[0]++; while(qbeg<qend) { p=que[qbeg++]; for(t=map[p][1];t;t=map[t][1]) { ep=map[t][0]; if (e[ep].q==p&&e[ep].maxf) if (h[e[ep].p]>h[p]+1) { h[e[ep].p]=h[p]+1; que[qend++]=e[ep].p; } } } for(i=so;i<=to;i++) num[h[i]]++; }void Argument(void){ int p,ep,mt=INF; for(p=to;p!=so;) { ep=from[p]; mt=min(mt,e[ep].maxf-e[ep].f); p=e[ep].p; } for(p=to;p!=so;) { ep=from[p]; e[ep].f+=mt; p=e[ep].p; } }bool ReLabel(int p){ int oh=h[p],t,ep; h[p]=to+1; for(t=map[p][1];t;t=map[t][1]) { ep=map[t][0]; if (e[ep].p==p&&e[ep].maxf>e[ep].f) h[p]=min(h[p],h[e[ep].q]+1); } num[oh]--; num[h[p]]++; current[p]=map[p][1]; if (num[oh]) return true; else return false; }void SAP(void){ int t,ep,i,p,r=0; for(i=so;i<=to;i++) current[i]=map[i][1]; BFS(); Greedy(); num[0]=to+1; p=so; while(h[so]!=to+1) { start: if (p==to) { Argument(); p=so; } while(current[p]) { ep=map[current[p]][0]; if (e[ep].p==p&&e[ep].maxf>e[ep].f&&h[p]==h[e[ep].q]+1) { pre[e[ep].q]=p; from[e[ep].q]=ep; p=e[ep].q; goto start; } current[p]=map[current[p]][1]; } if (!ReLabel(p)) break; if (p!=to) p=pre[p]; } for(t=map[so][1];t;t=map[t][1]) { ep=map[t][0]; r+=e[ep].f; } printf("%d/n",r); }int main(){ Inp(); SAP(); system("pause"); return 0; }
- POJ3469 代码
- poj3469
- POJ3469
- POJ3469
- POJ3469
- poj3469 Dinic
- poj3469 - Dual Core CPU
- poj3469最小割
- 网络流-poj3469
- POJ3469 Dual Core CPU
- poj3469(最小割)
- POJ3469-Dual Core CPU
- poj3469 Dual Core CPU
- POJ3469 Dual Core CPU
- POJ3469:Dual Core CPU
- 网络流sap(模板)--poj3469
- 最大流模板(poj3469)
- poj3469(*最小割模型)
- 面向对象的JavaScript(2): 对象就是二元组
- [双语阅读]《贫民富翁》女童星父亲叫卖领养权 开价40万美元
- SQL中EXISTS的用法
- 关于项目交易
- 删除操作时提示确认对话框
- POJ3469 代码
- 软件开发人员的能力模型
- 文件的读写
- [c++ primer plus]c++ 函数模版,类模版,实例化,具体化,模版作参数,模版的友元
- command
- [双语阅读]英媒评十大最差发型球星 罗纳尔多居首
- 入境旅游市场
- 电脑蓝屏
- 失业聚会 pink-slip party