【POJ】【2125】Destroying the Graph
网络流/二分图最小点权覆盖
果然还是应该先看下胡伯涛的论文……
orz proverbs
题意:
N个点M条边的有向图,给出如下两种操作。
删除点i的所有出边,代价是Ai。
删除点j的所有入边,代价是Bj。
求最后删除图中所有的边的最小代价。其实就是二分图最小点权覆盖。
定义:从x或者y集合中选取一些点,使这些点覆盖所有的边,并且选出来的点的权值尽可能小。
题解:
拆点。n个点拆成2n个点(左右各n个,i与(i+n)对应,之间连容量INF的边),S和i连容量为Ai的边,(i+n)与T之间连容量为Bi的边,求最小割即可
这样做为什么对呢?
当一条边存在的条件就是网络中还存在从S到T的非满流边!
方案输出不多说。。
汗……输出方案我WA了N次T_T,直接从S进行dfs,对于左边的点,如果走不到则表明 s->i 这条边被割掉了,对于右边的点,如果走的到则表明 i+n->t 这条边被割掉了,因为如果没割掉就直接从这个点走到t了……唉我一开始居然没想到
Source Code
Problem: User: sdfzyhy
Memory: 848K Time: 79MS
Language: G++ Result: Accepted Source Code //BZOJ 2125
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
#define fore(i,x) for(int i=head[x];i;i=next[i])
#define pb push_back
using namespace std;
inline int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*sign;
}
const int N=,M=,INF=~0u>>;
typedef long long LL;
/******************tamplate*********************/ struct edge{
int from,to,v;
};
int n,m;
struct Net{
edge E[M];
int head[N],next[M],cnt;
void add(int x,int y,int z){
E[++cnt]=(edge){x,y,z};
next[cnt]=head[x]; head[x]=cnt;
E[++cnt]=(edge){y,x,};
next[cnt]=head[y]; head[y]=cnt;
}
int s,t,d[N],cur[N],Q[N];
void init(){
n=getint(); m=getint();
s=; t=n*+; cnt=;
int x,y;
F(i,,n){
x=getint();
add(i+n,t,x);
}
F(i,,n){
x=getint();
add(s,i,x);
}
F(i,,m){
x=getint(); y=getint();
add(x,y+n,INF);
}
}
bool mklevel(){
memset(d,-,sizeof d);
d[s]=;
int l=,r=-;
Q[++r]=s;
while(l<=r){
int x=Q[l++];
fore(i,x){
edge&e=E[i];
if (d[e.to]==- && e.v>){
d[e.to]=d[x]+;
Q[++r]=e.to;
}
}
}
return d[t]!=-;
}
int dfs(int x,int a){
if (x==t) return a;
int flow=;
for(int &i=cur[x];i && flow<a;i=next[i]){
edge&e=E[i];
if (!e.v || d[e.to]!=d[x]+) continue;
int f=dfs(e.to,min(a-flow,e.v));
if (f>){
flow+=f;
e.v-=f;
E[i^].v+=f;
}
}
if (!flow) d[x]=-;
return flow;
}
int Dinic(){
int flow=;
while(mklevel()){
F(i,s,t) cur[i]=head[i];
flow+=dfs(s,INF);
}
return flow;
}
bool vis[N];
void dfs1(int x){
if (vis[x]) return;
vis[x]=;
for(int i=head[x];i;i=next[i])
if (E[i].v) dfs1(E[i].to);
}
void solve(){
printf("%d\n",Dinic());
int num=;
memset(vis,,sizeof vis);
dfs1(s);
F(i,,n) num+=(!vis[i])+vis[i+n];
printf("%d\n",num);
F(i,,n){
if (!vis[i]) printf("%d -\n",i);
if (vis[i+n]) printf("%d +\n",i);
}
}
}G1; int main(){
#ifndef ONLINE_JUDGE
freopen("2125.in","r",stdin);
freopen("2125.out","w",stdout);
#endif
G1.init();
G1.solve();
return ;
}
| Time Limit: 2000MS | Memory Limit: 65536K | |||
| Total Submissions: 7511 | Accepted: 2399 | Special Judge | ||
Description
Alice assigns two costs to each vertex: Wi+ and Wi-. If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.
Input
file describes the graph Alice has drawn. The first line of the input
file contains N and M (1 <= N <= 100, 1 <= M <= 5000). The
second line contains N integer numbers specifying Wi+. The third line defines Wi- in a similar way. All costs are positive and do not exceed 106
. Each of the following M lines contains two integers describing the
corresponding arc of the graph. Graph may contain loops and parallel
arcs.
Output
the first line of the output file print W --- the minimal sum Bob must
have to remove all arcs from the graph. On the second line print K ---
the number of moves Bob needs to do it. After that print K lines that
describe Bob's moves. Each line must first contain the number of the
vertex and then '+' or '-' character, separated by one space. Character
'+' means that Bob removes all arcs incoming into the specified vertex
and '-' that Bob removes all arcs outgoing from the specified vertex.
Sample Input
3 6
1 2 3
4 2 1
1 2
1 1
3 2
1 2
3 1
2 3
Sample Output
5
3
1 +
2 -
2 +
Source
[Submit] [Go Back] [Status] [Discuss]
【POJ】【2125】Destroying the Graph的更多相关文章
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- 【POJ 1459 power network】
不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
- 【POJ 2976 Dropping tests】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...
- 【POJ 3080 Blue Jeans】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...
- 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)
1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- 【POJ 2823 Sliding Window】 单调队列
题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...
- 【POJ 2406 Power Strings】
Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...
随机推荐
- QA在网站建设中的作用
在网站建设项目中,有一个团队负责产品测试并识别产品中的缺陷是很有意义的.问题在于,不应该只依赖这个团队来发现所有的缺陷,就像航空公司不能只依靠空乘人员确保飞机安全一样.这个观点的核心是一个简单的事实, ...
- Cocos2d-js中Chipmunk引擎
我们先介绍轻量级的物理引擎——Chipmunk.Chipmunk物理引擎,由Howling Moon Software的Scott Lebcke开发,用纯C编写.Chipmunk的下载地址是http: ...
- (转)RabbitMQ消息队列(五):Routing 消息路由
上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings绑定 上 ...
- floodfill算法解题示例
Flood fill算法是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法.因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Floo ...
- 编译内核模块出现error: negative width in bit-field 错误
今天在写一个简单的内核测试模块的时候出现了一个挺奇怪的问题,网上查了一下也没人解决,自己试了好久终于解决了,所以分享出来供大家参考,先贴出源码: /************************** ...
- android的入门学习
android 入门学习. 活动:就是一个包含应用程序的用户界面的窗口.目的就是与用户交互. 意图:就是能够将来自不同应用程序的不同活动无缝连接在一起工作的"胶水",确保这些任务执 ...
- OS X EI Capitan安装mcrypt
OS X EI Capitan安装mcrypt (我的博客原文:http://www.jmolboy.com/2015/12/01/mcrypt-extension-on-EI-Capitan/) ...
- 重拾C,一天一点点_5
switch(表达式){ case 整型常量表达式:语句序列 case 整型常量表达式:语句序列 default:语句序列} while(表达式) 语句 for(表达式1; 表 ...
- Eclipse 代码提示功能设置。
1. 解决实例化时自动补全不必要的单词问题 2. 以MyEclipse 6.5重新配图 鉴 于网上的批评之声甚大,我只想说明我的想法:这样的增强代码提示,最终是用来辅助我们 ...
- hdu 2256 Problem of Precision 构造整数 + 矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2256 题意:给定 n 求解 ? 思路: , 令 , 那么 , 得: 得转移矩阵: 但是上面求出来的并 ...