★ poj 2125 二分图的最小点权覆盖+输出解
来源:互联网 发布:淘宝的旗舰店靠谱吗 编辑:程序博客网 时间:2024/06/06 02:19
Destroying The Graph
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7104 Accepted: 2256 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.
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 61 2 34 2 11 21 13 21 23 12 3
Sample Output
531 +2 -2 +
Source
Northeastern Europe 2003, Northern Subregion
分析:就是二分图的最小点权覆盖问题,输出解的话,只要dfs即可。源点S,汇点T,将其余每个点拆成两个点,i和i+n,S和每一个i点连一条边,T点与每个i+n点连一条边,跑一遍最大流即可,对于输出解的情况,只要从S点开始dfs,标记走到的点即可。
代码:
//Dinic算法,复杂度O(n^2m)#pragma comment(linker,"/STACK:102400000,102400000")#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <vector>#include <string>#include <math.h>#include <queue>#include <stack>#include <map>#include <set>using namespace std;typedef long long ll; //记得必要的时候改成无符号const int maxn=505;const int maxm=1000005;const int INF=1000000000;struct EdgeNode{ int from; int to; int flow; int next;}edge[maxm];int head[maxn],cnt;void add(int x,int y,int z){ edge[cnt].from=x;edge[cnt].to=y;edge[cnt].flow=z;edge[cnt].next=head[x];head[x]=cnt++; edge[cnt].from=y;edge[cnt].to=x;edge[cnt].flow=0;edge[cnt].next=head[y];head[y]=cnt++; //printf("%d %d %d\n",x,y,z);}void init(){ cnt=0; memset(head,-1,sizeof(head));}int S,T,n,m;int d[maxn];bool bfs(int S,int T){ int que[maxn],iq=0,top; memset(d,0,sizeof(d)); d[S]=1; que[iq++]=S; for(int i=0;i<iq;i++) { top=que[i]; if(top==T)return true; for(int k=head[top];k!=-1;k=edge[k].next) { if(!d[edge[k].to]&&edge[k].flow) { que[iq++]=edge[k].to; d[edge[k].to]=d[top]+1; } } } return false;}int dfs(int now,int maxf,int T){ if(now==T)return maxf; int ret=0,f; for(int k=head[now];k!=-1;k=edge[k].next) { if(edge[k].flow&&d[edge[k].to]==d[now]+1) { f=dfs(edge[k].to,min(maxf-ret,edge[k].flow),T); edge[k].flow-=f; edge[k^1].flow+=f; ret+=f; if(ret==maxf)return ret; } } return ret;}int Dinic(int S,int T){ int ans=0; while(bfs(S,T))ans+=dfs(S,INF,T); return ans;}int a[maxn],b[maxn];bool mark[3*maxn];void dfs_cut(int x){ mark[x]=1; int y; for(int i=head[x];i!=-1;i=edge[i].next) { y=edge[i].to; if(edge[i].flow&&!mark[y]) dfs_cut(y); }}int main(){ int i,x,y,cs; while(~scanf("%d%d",&n,&m)) { init(); S=0; T=2*n+1; for(i=1;i<=n;i++){ scanf("%d",&a[i]); add(i+n,T,a[i]); } for(i=1;i<=n;i++){ scanf("%d",&b[i]); add(S,i,b[i]); } for(i=1;i<=m;i++){ scanf("%d%d",&x,&y); add(x,y+n,INF); } printf("%d\n",Dinic(S,T)); memset(mark,false,sizeof(mark)); dfs_cut(S); cs=0; for(i=1;i<=n;i++)if(!mark[i])cs++; for(i=n+1;i<=2*n;i++)if(mark[i])cs++; printf("%d\n",cs); for(i=1;i<=n;i++)if(!mark[i])printf("%d -\n",i); for(i=n+1;i<=2*n;i++)if(mark[i])printf("%d +\n",i-n); } return 0;}
0 0
- ★ poj 2125 二分图的最小点权覆盖+输出解
- poj 2125 二分图最小点权覆盖
- 二分图最小点权覆盖(输出覆盖的点)poj2025
- POJ 1325二分图,最小点覆盖
- POJ 2226 二分图 最小点覆盖
- poj 2226 二分图最小点覆盖
- POJ 3308 二分图最小点权覆盖 最大流
- POJ 3308 Paratroopers 二分图最小点权覆盖
- POJ 2125 Destroying The Graph 二分图最小点权覆盖 最小割
- POJ 2125 最小点权覆盖集(输出方案)
- poj 2125 最小割解决 "最小点权覆盖问题" +输出解(割边集)
- poj 2125 最小割解决 "最小点权覆盖问题" +输出解(割边集)
- 【二分图|最小点权覆盖】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 二分图 最小点权覆盖
- 最新的Xcode6_beta_4下载
- 读写锁的特性
- 在O(1时间删除链表节点
- rt5350 捕获sn9c291 ov9712 模块jpeg图片效果
- Codeforces 453B Little Pony and Harmony Chest(状压)
- ★ poj 2125 二分图的最小点权覆盖+输出解
- C打印图形
- 配置前端自动构建架构及工具编写
- Maven nexus安装时提示 nexus : wrapper | OpenSCManager failed - 拒绝访问
- openGL线宽设置
- 数据结构精要------直接选择和堆排序算法
- UVA 11898 - Killer Problem(鸽笼原理+计数排序+大力出奇迹)
- 螺旋打印二维数组
- 看Linux网络编程笔记