POJ 2125 Destroying The Graph
来源:互联网 发布:美国公务员 知乎 编辑:程序博客网 时间:2024/05/17 02:39
传送门:POJ 2125
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#define N 300#define M 20000#define INF 99999999using namespace std;int to[M],next[M],head[N],len[M],cnt,layer[N],n,m,S,T,q[M];bool vis[N];inline void add(int u,int v,int w){ to[cnt]=v; len[cnt]=w; next[cnt]=head[u]; head[u]=cnt++; to[cnt]=u; len[cnt]=0; next[cnt]=head[v]; head[v]=cnt++;}void read(){ memset(head,-1,sizeof head);cnt=0; scanf("%d%d",&n,&m); S=0,T=n+n+1; for(int i=1,a;i<=n;i++) scanf("%d",&a),add(i+n,T,a); for(int i=1,a;i<=n;i++) scanf("%d",&a),add(S,i,a); for(int i=1,a,b;i<=m;i++) scanf("%d%d",&a,&b),add(a,b+n,INF);}bool bfs(){ memset(layer,-1,sizeof layer); int h=1,t=2,sta; q[1]=S; layer[S]=0; while(h<t) { sta=q[h++]; for(int i=head[sta];~i;i=next[i]) if(len[i]>0&&layer[to[i]]<0) { layer[to[i]]=layer[sta]+1; q[t++]=to[i]; } } return layer[T]!=-1;}int Find(int u,int cur_flow){ if(u==T) return cur_flow; int result=0,tmp; for(int i=head[u];~i;i=next[i]) if(len[i]>0&&layer[to[i]]==layer[u]+1) { tmp=Find(to[i],min(cur_flow-result,len[i])); len[i]-=tmp; len[i^1]+=tmp; result+=tmp; } if(!result) layer[u]=-1; return result;}void dfs(int u){ vis[u]=true; for(int i=head[u];~i;i=next[i]) if(!vis[to[i]]&&len[i]) dfs(to[i]);}void dinic(){ int ans=0; while(bfs()) ans+=Find(S,INF); printf("%d\n",ans); dfs(S); ans=0; for(int i=1;i<=n;i++) ans+=(!vis[i])+(vis[i+n]); printf("%d\n",ans); for(int i=1;i<=n;i++) { if(!vis[i]) printf("%d -\n",i); if(vis[i+n]) printf("%d +\n",i); }}int main(){ read(); dinic(); return 0;}
0 0
- POJ 2125 Destroying The Graph
- POJ 2125 Destroying The Graph
- poj 2125 Destroying The Graph
- poj 2125 Destroying The Graph
- POJ 2125 Destroying The Graph
- POJ 2125 Destroying The Graph
- 【网络流】 POJ 2125 Destroying The Graph
- POJ 2125 Destroying The Graph 最小点权覆盖
- POJ 2125 - Destroying The Graph 构图最小割
- POJ 2125 Destroying The Graph 最小点权覆盖集
- 【POJ】2125 Destroying The Graph 最小点权覆盖
- poj 2125 Destroying The Graph 最小点权覆盖
- Destroying The Graph (poj 2125 最小点权覆盖)
- poj 2125 Destroying The Graph 最小点权覆盖集
- poj 2125 Destroying The Graph(最小点权覆盖集)
- POJ2125:Destroying The Graph
- POJ2125 Destroying The Graph
- POJ2125 Destroying The Graph
- 解决JSP中文乱码问题
- 正确使用 Volatile 变量
- shell编程基础
- 设计模式六大原则(2):里氏替换原则
- href="javascript:;"
- POJ 2125 Destroying The Graph
- Angular学习1
- Android:职业路线—如何成为高级工程师
- treap树的操作--查找区间第k大
- Android Jmail 发送邮件
- SQL基本查询小结
- java基础——Java对象的序列化与反序列化
- [USACO NOV06 GOLD]block 路障(Spfa)
- js中的数据类型和typeof