网络流/二分图最小点权覆盖


  果然还是应该先看下胡伯涛的论文……

  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 ;
}
Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7511   Accepted: 2399   Special Judge

Description

Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that Bob tries to destroy it. In a move he may take any vertex of the graph and remove either all arcs incoming into this vertex, or all arcs outgoing from this vertex.
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

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

On
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

Northeastern Europe 2003, Northern Subregion

[Submit]   [Go Back]   [Status]   [Discuss]

【POJ】【2125】Destroying the Graph的更多相关文章

  1. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  2. 【POJ 1459 power network】

    不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...

  3. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

  4. 【POJ 2976 Dropping tests】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...

  5. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  6. 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)

    1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...

  7. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  8. 【POJ 2823 Sliding Window】 单调队列

    题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...

  9. 【POJ 2406 Power Strings】

    Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...

随机推荐

  1. Google Play支付校验

    关于Google Play支付校验我之前在网上也找过大量的相关资料,发现大多数都是采用publicKey的方式来校验订单,但是在Google Play提供的官方实例中publicKey其实在客户端也是 ...

  2. ping命令的用法大全!

    1)如何查看本机所开端口: 用netstat -an命令查看!再stat下面有一些英文,我来简单说一下这些英文具体都代表什么- LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发 ...

  3. iOS Core Animation Advanced Techniques

    Book Descripter Core Animation is the technology underlying Apple's iOS user interface. By unleashin ...

  4. Python的functools.reduce用法

    python 3.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce. reduce的用法 reduce(fun ...

  5. springmvc 精华

    Spring Mvc简介: Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求 ...

  6. Codevs 1078 ==Poj 1258 Agri-Net

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53270   Accepted: 22140 Description D ...

  7. STL--vector(转载)

    函数 表述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数据赋值给c. 将n个elem的拷贝赋值给c. c.at(idx) 传回索引idx所指的 ...

  8. POJ 2533

    最长上升子序列裸题在网上看到有两种方法...一种复杂度O(N^2),一种O(NlogN).orz O(N^2): #include<cstdio> #define N 1001 int m ...

  9. Windows Phone 8 蓝牙编程

    蓝牙是手机的近距离无限传输的技术,在之前的Windows Phone 7系统手机里面仅支持蓝牙耳机功能,并不支持蓝牙文件信息传输,那么在Windows Phone 8手机里面将全面支持蓝牙技术,并且提 ...

  10. windows phone 自定义铃声

    屌丝的电话是一个月都响不了几次的,无聊还是下了一个XX铃声,自娱自乐一下,google了一下实现原理,,,,,,真相啊!!!就是用了一个Task(SaveRingtoneTask),以前看的资料都没有 ...