Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8356   Accepted: 2696   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

思路:

  最小点权覆盖;

来,上代码:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream> #define INF 0x7ffffff using namespace std; struct EdgeType {
int v,e,f;
};
struct EdgeType edge[<<]; int n,m,vout[],vin[],s,t;
int head[],deep[],cnt=,ans; bool if_[]; char Cget; inline void in(int &now)
{
now=,Cget=getchar();
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline void edge_add(int u,int v,int f)
{
edge[++cnt].v=v,edge[cnt].f=f,edge[cnt].e=head[u],head[u]=cnt;
edge[++cnt].v=u,edge[cnt].f=,edge[cnt].e=head[v],head[v]=cnt;
} bool BFS()
{
for(int i=s;i<=t;i++) deep[i]=-;
queue<int>que;que.push(s);deep[s]=;
while(!que.empty())
{
int now=que.front();
for(int i=head[now];i;i=edge[i].e)
{
if(deep[edge[i].v]<&&edge[i].f>)
{
deep[edge[i].v]=deep[now]+;
if(edge[i].v==t) return true;
que.push(edge[i].v);
}
}
que.pop();
}
return false;
} int flowing(int now,int flow)
{
if(now==t||flow==) return flow;
int oldflow=;
for(int i=head[now];i;i=edge[i].e)
{
if(deep[edge[i].v]!=deep[now]+||edge[i].f==) continue;
int pos=flowing(edge[i].v,min(flow,edge[i].f));
flow-=pos;
oldflow+=pos;
edge[i].f-=pos;
edge[i^].f+=pos;
if(flow==) return oldflow;
}
if(oldflow==) deep[now]=-;
return oldflow;
} void check(int now)
{
if_[now]=true;
for(int i=head[now];i;i=edge[i].e)
{
if(!edge[i].f||if_[edge[i].v]) continue;
check(edge[i].v);
}
} int main()
{
in(n),in(m);
s=,t=n+n+;
for(int i=;i<=n;i++)
{
in(vout[i]);
edge_add(i+n,t,vout[i]);
}
for(int i=;i<=n;i++)
{
in(vin[i]);
edge_add(s,i,vin[i]);
}
int u,v;
for(int i=;i<=m;i++)
{
in(u),in(v);
edge_add(u,v+n,INF);
}
while(BFS()) ans+=flowing(s,INF);
cout<<ans;ans=;
putchar('\n');check(s);
for(int i=;i<=n;i++)
{
if(!if_[i]) ans++;
if(if_[i+n]) ans++;
}
cout<<ans;putchar('\n');
for(int i=;i<=n;i++)
{
if(!if_[i]) printf("%d -\n",i);
if(if_[i+n]) printf("%d +\n",i);
}
return ;
}

最新文章

  1. C语言基础(11)-随机数发生器
  2. 如何去掉HTML5Viewer中的滚动条
  3. ecshop后台新功能及权限的添加
  4. oracle查询一个时间段每天的数据量
  5. Tomcat环境配置部署测试环境及架构
  6. Android定位&amp;地图&amp;导航——基于百度地图移动获取位置和自动定位
  7. python把str转换为int
  8. HTTP接口功能自动化测试入门
  9. hdu 1558 Segment set
  10. js 作用域,变量提升
  11. Redis学习 - 入门
  12. HDU2276 - Kiki &amp;amp; Little Kiki 2(矩阵高速幂)
  13. 20165321 测试-3-ch02
  14. node.js fs、http使用
  15. jquery 笔记总结
  16. 通过NAT转发实现私网对外发布信息
  17. jQuery 遮盖层弹出后禁止页面滚动
  18. 银联支付-产品测试sdk使用流程
  19. Cocos2d-x调用Java 代码
  20. Oracle SQL developer 连接 MySQL 数据库安装配置

热门文章

  1. 为PHPcms扩展json采集
  2. thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题
  3. 封装,封装的原理,Property ,setter ,deleter,多态,内置函数 ,__str__ , __del__,反射,动态导入模块
  4. java做http接口
  5. Python学习笔记:py2exe打包Python程序
  6. python中的内建函数
  7. octave-basic
  8. Linux学习-RPM 软件管理程序: rpm
  9. UVa 11149 Power of Matrix 矩阵快速幂
  10. Python虚拟机函数机制之名字空间(二)