POJ2125 Destroying The Graph
题目链接:ヾ(≧∇≦*)ゝ
大致题意:
给出一个有向图D=(V,E).对于每个点U,定义两种操作a(u),b(u)
操作a(u):删除点U的所有出边,即属于E,操作花费为Ca(u).
操作b(u):删除点U的所有入边,即属于E,操作花费为Cb(u).
求将原图的边集的边全部删除的最小代价,总操作数和具体操作
Solution:
第一问很简单,首先,对于每一个点,把它分成出点和入点。
把每个点的出点与S相连,入点与T相连。边容量分别为删除该点所有入边和出边的花费。
然后对于每条边 a -> b,就把a的出点与b的入点连一条容量为inf的边。
根据最大流=最小割,跑一遍dinic就能得到答案了。
对于第二、三问,我们分别统计a操作和b操作。
我们先对剩余网络进行bfs(),把能够扫到的点都标记为1,不能的标记为0。
对于一个点u,如果要使用a(u),那么显然,需要至少存在一个点v,满足u -> v &&
vis[u]vis[v]0。
而对于点u,如果要使用b(u),只需要满足vis[u]==1就行了。
为了防止重复输出,在定义一个apr数组记录每个数是否加入到答案中就行了。
详见代码
Code:
#include<queue>
#include<ctype.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1001
#define M 20001
#define inf 1926081700
using namespace std;
int S,T,head[N];
int n,m,cnt=1;
int ru[N],cu[N];
int ans,vis[N],apr[N];
int t1,t2,fst[N],sec[N];
struct Edge{int nxt,to,val;}edge[M];
void ins(int x,int y,int z){
edge[++cnt].nxt=head[x];
edge[cnt].to=y;edge[cnt].val=z;
head[x]=cnt;
}
namespace Network_Flow{
queue<int> q;
int dep[N];
int bfs(){
memset(dep,0,sizeof(dep));
q.push(S);dep[S]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to,v=edge[i].val;
if(!dep[y]&&v){
q.push(y);
dep[y]=dep[x]+1;
}
}
}
return dep[T];
}
int dfs(int x,int rest){
if(x==T||rest<=0) return rest;
int flow=0;
for(int i=head[x];i;i=edge[i].nxt){
int y=edge[i].to,v=edge[i].val;
if(dep[y]==dep[x]+1&&v){
int now=dfs(y,min(rest,v));
edge[i].val-=now;
edge[i^1].val+=now;
flow+=now;rest-=now;
if(!rest) break;
}
}
return flow;
}
int dinic(){
int maxflow=0;
while(bfs()) maxflow+=dfs(S,inf);
return maxflow;
}
}
void getspj(){
queue<int> s;
s.push(S);vis[S]=1;
while(!s.empty()){
int x=s.front();s.pop();
for(int i=head[x];i;i=edge[i].nxt)
if(!vis[edge[i].to]&&edge[i].val){
s.push(edge[i].to);
vis[edge[i].to]=1;
}
}
apr[S]=apr[T]=1;
}
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int main(){
n=read(),m=read();
S=n*2+1,T=S+1;
for(int i=1;i<=n;i++) ru[i]=read();
for(int i=1;i<=n;i++) cu[i]=read();
for(int i=1;i<=n;i++){
ins(S,i,cu[i]);ins(i,S,0);
ins(i+n,T,ru[i]);ins(T,i+n,0);
}
for(int x,y,i=1;i<=m;i++){
x=read(),y=read();
ins(x,n+y,inf);
ins(n+y,x,0);
}
using namespace Network_Flow;
printf("%d\n",dinic());getspj();
for(int i=1;i<=n;i++)
for(int j=head[i];j;j=edge[j].nxt){
int y=edge[j].to;
if(!vis[i]&&!vis[y]&&!apr[i]){
sec[++t2]=i;
ans++;apr[i]=1;
}
if(!apr[y]&&vis[y]){
fst[++t1]=y%n;
if(!fst[t1]) fst[t1]=n;
ans++;apr[y]=1;
}
}
printf("%d\n",ans);
for(int i=1;i<=t1;i++) printf("%d +\n",fst[i]);
for(int i=1;i<=t2;i++) printf("%d -\n",sec[i]);
return 0;
}
POJ2125 Destroying The Graph的更多相关文章
- POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)
Destroying The Graph Time Limit: 2000MS Memo ...
- POJ2125 Destroying The Graph(二分图最小点权覆盖集)
最小点权覆盖就是,对于有点权的有向图,选出权值和最少的点的集合覆盖所有的边. 解二分图最小点权覆盖集可以用最小割: vs-X-Y-vt这样连边,vs和X部点的连边容量为X部点的权值,Y部和vt连边容量 ...
- POJ2125 Destroying The Graph 二分图 + 最小点权覆盖 + 最小割
思路来源:http://blog.csdn.net/lenleaves/article/details/7873441 求最小点权覆盖,同样求一个最小割,但是要求出割去了那些边, 只要用最终的剩余网络 ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- 【POJ】【2125】Destroying the Graph
网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...
- 图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph
Destroying The Graph Description Alice and Bob play the following game. First, Alice draws some di ...
- POJ 2125 Destroying The Graph [最小割 打印方案]
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8311 Accepted: 2 ...
- poj 2125 Destroying The Graph (最小点权覆盖)
Destroying The Graph http://poj.org/problem?id=2125 Time Limit: 2000MS Memory Limit: 65536K ...
- AC日记——Destroying The Graph poj 2125
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8356 Accepted: 2 ...
随机推荐
- 微信小程序开发 [05] wx.request发送请求和妹纸图
1.wx.request 微信小程序中用于发起网络请求的API就是wx.request了,具体的参数太多,此处就不再一一详举了,基本使用示例如下: wx.request({ url: 'test.ph ...
- python_环境的配置
1.首先登入官网:https://www.python.org/downloads/windows/ 下载: 下载executable installer 2.安装 ipython,jupyter 地 ...
- mysql事务,select for update,及数据的一致性处理
在MySQL的InnoDB中,预设的Tansaction isolation level 为REPEATABLE READ(可重读) 在SELECT 的读取锁定主要分为两种方式: SELECT ... ...
- 20155227《网络对抗》Exp9 Web安全基础实践
20155227<网络对抗>Exp9 Web安全基础实践 实验内容 关于WebGoat Cross-Site Scripting(XSS)练习 Injection Flaws练习 CSRF ...
- 20155238 Java第13周课堂实践
类定义 实验内容及要求 设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter.定义至少三个构造方法,接收并初始化 ...
- 20155331《网络对抗技术》Exp4:恶意代码分析
20155331<网络对抗技术>Exp4:恶意代码分析 实验过程 计划任务监控 在C盘根目录下建立一个netstatlog.bat文件(先把后缀设为txt,保存好内容后记得把后缀改为bat ...
- Egret(白鹭引擎)——“TypeError: Cannot read property 'asCom' of null”
前言 相信我,这个错误新手都不陌生:TypeError: Cannot read property 'asCom' of null 还有,一定要看我上一篇,哦不(人家应该是报了这个错,才找到看到这篇文 ...
- webVR全景图多种方案实现(pannellum,aframe,Krpano,three,jquery-vrview)
前言 有一篇文章我说了H5实现全景图预览,全景视频播放的原理,有需要的小伙伴可以自行去看一下 今天我就拿出我的实践干货出来,本人实测实测过 需求 老板:我需要可以上传全景图片,然后手机网站上都可以36 ...
- 你应该知道Go语言的几个优势
要说起GO语言的优势,我们就得从GO语言的历史讲起了-- 本文由腾讯技术工程官方号发表在腾讯云+社区 2007年,受够了C++煎熬的Google首席软件工程师Rob Pike纠集Robert Grie ...
- python 连接 hive 的 HiveServer2 的配置坑
环境: hadoop 2.7.6 hive 2.3.4 Hive 的 thirft 启动: hadoop 单机或者集群需要: 启动 webhdfs 修改 hadoop 的代理用户 <proper ...