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


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

  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了……唉我一开始居然没想到

  1 Source Code
  2 Problem: 2125        User: sdfzyhy
  3 Memory: 848K        Time: 79MS
  4 Language: G++        Result: Accepted
  5
  6     Source Code
  7
  8     //BZOJ 2125
  9     #include<vector>
 10     #include<cstdio>
 11     #include<cstring>
 12     #include<cstdlib>
 13     #include<iostream>
 14     #include<algorithm>
 15     #define rep(i,n) for(int i=0;i<n;++i)
 16     #define F(i,j,n) for(int i=j;i<=n;++i)
 17     #define D(i,j,n) for(int i=j;i>=n;--i)
 18     #define fore(i,x) for(int i=head[x];i;i=next[i])
 19     #define pb push_back
 20     using namespace std;
 21     inline int getint(){
 22         int v=0,sign=1; char ch=getchar();
 23         while(ch<'0'||ch>'9'){ if (ch=='-') sign=-1; ch=getchar();}
 24         while(ch>='0'&&ch<='9'){ v=v*10+ch-'0'; ch=getchar();}
 25         return v*sign;
 26     }
 27     const int N=310,M=20010,INF=~0u>>2;
 28     typedef long long LL;
 29     /******************tamplate*********************/
 30
 31     struct edge{
 32         int from,to,v;
 33     };
 34     int n,m;
 35     struct Net{
 36         edge E[M];
 37         int head[N],next[M],cnt;
 38         void add(int x,int y,int z){
 39             E[++cnt]=(edge){x,y,z};
 40             next[cnt]=head[x]; head[x]=cnt;
 41             E[++cnt]=(edge){y,x,0};
 42             next[cnt]=head[y]; head[y]=cnt;
 43         }
 44         int s,t,d[N],cur[N],Q[N];
 45         void init(){
 46             n=getint(); m=getint();
 47             s=0; t=n*2+1; cnt=1;
 48             int x,y;
 49             F(i,1,n){
 50                 x=getint();
 51                 add(i+n,t,x);
 52             }
 53             F(i,1,n){
 54                 x=getint();
 55                 add(s,i,x);
 56             }
 57             F(i,1,m){
 58                 x=getint(); y=getint();
 59                 add(x,y+n,INF);
 60             }
 61         }
 62         bool mklevel(){
 63             memset(d,-1,sizeof d);
 64             d[s]=0;
 65             int l=0,r=-1;
 66             Q[++r]=s;
 67             while(l<=r){
 68                 int x=Q[l++];
 69                 fore(i,x){
 70                     edge&e=E[i];
 71                     if (d[e.to]==-1 && e.v>0){
 72                         d[e.to]=d[x]+1;
 73                         Q[++r]=e.to;
 74                     }
 75                 }
 76             }
 77             return d[t]!=-1;
 78         }
 79         int dfs(int x,int a){
 80             if (x==t) return a;
 81             int flow=0;
 82             for(int &i=cur[x];i && flow<a;i=next[i]){
 83                 edge&e=E[i];
 84                 if (!e.v || d[e.to]!=d[x]+1) continue;
 85                 int f=dfs(e.to,min(a-flow,e.v));
 86                 if (f>0){
 87                     flow+=f;
 88                     e.v-=f;
 89                     E[i^1].v+=f;
 90                 }
 91             }
 92             if (!flow) d[x]=-1;
 93             return flow;
 94         }
 95         int Dinic(){
 96             int flow=0;
 97             while(mklevel()){
 98                 F(i,s,t) cur[i]=head[i];
 99                 flow+=dfs(s,INF);
100             }
101             return flow;
102         }
103         bool vis[N];
104         void dfs1(int x){
105             if (vis[x]) return;
106             vis[x]=1;
107             for(int i=head[x];i;i=next[i])
108                 if (E[i].v) dfs1(E[i].to);
109         }
110         void solve(){
111             printf("%d\n",Dinic());
112             int num=0;
113             memset(vis,0,sizeof vis);
114             dfs1(s);
115             F(i,1,n) num+=(!vis[i])+vis[i+n];
116             printf("%d\n",num);
117             F(i,1,n){
118                 if (!vis[i]) printf("%d -\n",i);
119                 if (vis[i+n]) printf("%d +\n",i);
120             }
121         }
122     }G1;
123
124     int main(){
125     #ifndef ONLINE_JUDGE
126         freopen("2125.in","r",stdin);
127         freopen("2125.out","w",stdout);
128     #endif
129         G1.init();
130         G1.solve();
131         return 0;
132     }

View Code

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]

转载于:https://www.cnblogs.com/Tunix/p/4335741.html

【POJ】【2125】Destroying the Graph相关推荐

  1. 【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宫问题】

    POJ3126Prime Path 给定两个四位素数a  b,要求把a变换到b 变换的过程要 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数  与  前一步得到的素数  只能有一个位 ...

  2. 【poj题集整理】【存下来并不会看】

    主要是整理起来自己用的.网上有多个版本. 初级: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)   ...

  3. POJ 2125 Destroying The Graph Acwing 2325. 有向图破坏(拆点+最小权点覆盖集)

    原题链接 POJ 2125:Destroying The Graph Acwing 2325:有向图破坏 题目大意 我们要删除一个有向图中的所有边,有两种删法,一是删除某点的所有入边,二是删除某点的所 ...

  4. 【POJ/算法】 3259 Wormholes(Bellman-Ford算法, SPFA ,FLoyd算法)

    Bellman-Ford算法 Bellman-Ford算法的优点是可以发现负圈,缺点是时间复杂度比Dijkstra算法高.而SPFA算法是使用队列优化的Bellman-Ford版本,其在时间复杂度和编 ...

  5. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  6. BZOJ 2287 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...

  7. 【POJ 3026】Borg Maze

    [POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...

  8. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  9. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...

  10. 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)

    2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...

最新文章

  1. linux mint 修改dns,如何在Ubuntu和LinuxMint中刷新DNS缓存
  2. Python自动化开发学习6
  3. Mac上更新Ruby
  4. python自动化测试数据驱动_利用Python如何实现数据驱动的接口自动化测试
  5. 使用client-go自定义开发Kubernetes
  6. python编写格斗游戏_C语言实现的开源 2D 格斗游戏: Punch Kick
  7. 第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波11 - 直方图处理 - 使用直方图统计量增强图像
  8. atitit.设计文档---操作日志的实现
  9. STM32F103定时器输出频率测试
  10. 五通信算法:五种编码增益比较matlab模拟
  11. Linux下conda 安装以后 activate无法使用
  12. 拆解查看unity游戏资源
  13. ps自定义形状工具_Acorn for Mac(轻量级图片处理工具)
  14. android studio : amend commit
  15. 简简单单做股票读书笔记(1/8)
  16. 霍尔 磁电 光电式测数传感器的优缺点比较
  17. 单月营业额一个亿,任泉李冰冰黄晓明追着投钱!这家企业是谁?
  18. AttributeError: ‘str‘ object has no attribute ‘spilt‘ on line 9
  19. 仿小米通讯录 右侧滑动条与带动画的悬停列表实现(一)
  20. 云时代编程语言Ballerina发布,TIOBE9月排行榜PHP排名在边缘飘摇(2019/09/16)

热门文章

  1. FISCO BCOS流量控制实现
  2. kubernetes视频教程笔记 (31)-安全-鉴权Authorization
  3. 怎么注册开通个人微信小程序
  4. Django中Python3安装Crypto使用RSA
  5. android 串口调试数据手机收不到,记录一次安卓串口一次接收全部数据时,发生的错误...
  6. python语法学习第十天--类与对象
  7. 开机一直转圈_天气转凉,电脑早上开机也需要预热了吗?
  8. 1.4.PHP7.1 狐教程-环境(Mac下 PHP开发环境 配置及安装 php7.1.x nginx mysql)
  9. list中随机获取n条、随机生成4位6位数工具类,1-n范围随机数,Math.random()生成随机数
  10. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_09-webpack研究-webpack介绍