[POJ2125]Destroying The Graph 做题笔记
来源:互联网 发布:mac安装sublime text2 编辑:程序博客网 时间:2024/05/16 11:23
题目链接:http://poj.org/problem?id=2125
拆点成二分图后,可以看出问题有点类似于二分图最大点覆盖集,实际上它是最大点权覆盖集,根据对偶原理,把二分图转化,二分图原有的边变为inf,s->左部点,容量为点权,右部点->t,容量同样为点权,跑最大流,就是最大点权覆盖集。
关键是判断割边。这里的割边代表“割点”,即拆掉点的入度或点的出度,可以用一遍dfs实现。
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int N=500;const int M=40000;const int inf=0x3fffffff;int head[N],d[N],edge[M],ver[M],next[M];bool v[N];int n,m,s,t,tot=1,maxflow=0;void add (int u,int v,int cap) { ver[++tot]=v;edge[tot]=cap;next[tot]=head[u];head[u]=tot; ver[++tot]=u;edge[tot]=0; next[tot]=head[v];head[v]=tot;}bool bfs () { memset(d,0,sizeof(d)); queue<int> q; q.push(s); d[s]=1; while (!q.empty()) { int x=q.front(); q.pop(); for (int i=head[x];i;i=next[i]) if (edge[i] && d[ver[i]]==0) { q.push(ver[i]); d[ver[i]]=d[x]+1; if (ver[i]==t) return true; } } return false;}int dinic (int x,int f) { if (x==t) return f; int rest=f,now; for (int i=head[x];i&&rest;i=next[i]) if (edge[i]&&d[ver[i]]==d[x]+1) { now=dinic(ver[i],min(rest,edge[i])); if (!now) d[ver[i]]=0; edge[i]-=now; edge[i^1]+=now; rest-=now; } return f-rest;}void dfs (int u) { v[u]=1; for (int i=head[u];i;i=next[i]) if (!v[ver[i]]&&edge[i]) dfs(ver[i]);}int main () { int x,y,w,ans=0; while (scanf("%d%d",&n,&m)!=EOF) { s=0;t=2*n+1;tot=1;maxflow=ans=0; memset(head,0,sizeof(head)); memset(v,0,sizeof(v)); for (int i=1;i<=n;i++) { scanf("%d",&x); add(i+n,t,x); } for (int i=1;i<=n;i++) { scanf("%d",&x); add(s,i,x); } for (int i=1;i<=m;i++) { scanf("%d%d",&x,&y); add(x,y+n,inf); } while (bfs()) while (w=dinic(s,inf)) maxflow+=w; printf("%d\n",maxflow); dfs(s); for (int i=1;i<=n;i++) ans+=!(v[i])+(v[i+n]); printf("%d\n",ans); for (int i=1;i<=n;i++) { if (v[i+n]) printf("%d +\n",i); if (!v[i]) printf("%d -\n",i); } } return 0;}
0 0
- [POJ2125]Destroying The Graph 做题笔记
- POJ2125:Destroying The Graph
- POJ2125 Destroying The Graph
- POJ2125 Destroying The Graph
- POJ2125:Destroying The Graph
- poj2125 Destroying The Graph
- poj2125:Destroying The Graph(网络流)
- POJ2125 Destroying The Graph(最小割求割点)
- POJ2125 Destroying The Graph 最小点权覆盖
- 【POJ2125】【网络流】【最小割】Destroying The Graph 题解
- 【POJ2125】Destroying The Graph(最小权覆盖点集)
- 【poj2125】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
- java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting ‘name pattern’
- sqlserver实现oracle的instr函数(未完)
- AngularJS中的Provider们:Service和Factory等的区别
- Tomcat 启动,错误:The APR based Apache Tomcat Native library
- 玩具侠-C语言学习之函数指针(2)
- [POJ2125]Destroying The Graph 做题笔记
- CEF中访问修改HTML DOM元素
- java中有没有指针
- 经典排序算法总结篇
- 分治法来解决大整数乘法问题
- iOS开发: initializer element is not constant错误
- jquery的ajax传送乱码问题
- [JavaScript]操作下拉列表
- photon引擎