POJ-3469-Dual Core CPU
来源:互联网 发布:淘宝和别人互刷商品 编辑:程序博客网 时间:2024/05/21 11:33
题目不难,就是求最小割,用网络流做即可,
建图完毕后用Dinic做的,卡超时了很久,唉~最后找到一种新的优化方法才勉强过的
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=20100;const int maxm=500100;const int inf=1<<29;int n,m,st,des,e,pnt[maxm],nxt[maxm],cost[maxm],head[maxn],level[maxn],q[maxn];void AddEdge(int u,int v,int c1,int c2){ pnt[e]=v;nxt[e]=head[u];cost[e]=c1;head[u]=e++; pnt[e]=u;nxt[e]=head[v];cost[e]=c2;head[v]=e++;}void read(){ st=0,des=n+1,e=0; memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) {int a,b;scanf("%d%d",&a,&b);AddEdge(st,i,a,0);AddEdge(i,des,b,0); } for(int i=1;i<=m;i++) {int u,v,c;scanf("%d%d%d",&u,&v,&c);AddEdge(u,v,c,c); }}bool BFS(){ memset(level,0,sizeof(level)); level[st]=1; int pre=0,last=1; q[pre]=st; while(pre<last) {if(q[pre]==des) return true;for(int i=head[q[pre]];i!=-1;i=nxt[i]) if(cost[i]&&!level[pnt[i]]) {level[pnt[i]]=level[q[pre]]+1;q[last++]=pnt[i]; } pre++; } return level[des];}int DFS(int u,int sum){ if(u==des||!sum)return sum; int ans=0; for(int i=head[u],t;i!=-1&&sum>ans;i=nxt[i])if(cost[i]&&level[pnt[i]]==level[u]+1){ int t=DFS(pnt[i],min(sum-ans,cost[i])); if(!t) {level[pnt[i]]=0;continue; } cost[i]-=t; cost[i^1]+=t; ans+=t;} return ans;}void Dinic(){ int ans=0; while(BFS())ans+=DFS(st,inf); printf("%d\n",ans);}int main(){ while(scanf("%d%d",&n,&m)!=EOF) {read();Dinic(); } return 0;}
- POJ 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- POJ-3469-Dual Core CPU
- POJ 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- 【Poj 3469】 Dual Core CPU
- POJ 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- [poj 3469]Dual Core CPU
- POJ 3469 Dual Core CPU
- poj 1963 Corporative Network
- 二分查找的两种方式
- C++之MFC访问ACCESS数据库注意要点
- matlab中load与savd函数的用法
- Activity跳转——返回数据到前Activity
- POJ-3469-Dual Core CPU
- 百度谷歌关键词排名的小技巧,SEO小技巧
- ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang
- 低功耗STM32L-Discovery开发板评测(二)
- WinDbg
- js2dx --- UI
- Maya: 菜单 编辑曲线 > 复制曲面曲线
- Hibernate的Session
- hadoop开发:cenos网络配置