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 ;
}

AC日记——Destroying The Graph poj 2125的更多相关文章

  1. AC日记——青蛙的约会 poj 1061

    青蛙的约会 POJ - 1061   思路: 扩展欧几里得: 设青蛙们要跳k步,我们可以得出式子 m*k+a≡n*k+b(mod l) 式子变形得到 m*k+a-n*k-b=t*l (m-n)*k-t ...

  2. poj 2125 Destroying The Graph (最小点权覆盖)

    Destroying The Graph http://poj.org/problem?id=2125 Time Limit: 2000MS   Memory Limit: 65536K       ...

  3. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  4. 【POJ】【2125】Destroying the Graph

    网络流/二分图最小点权覆盖 果然还是应该先看下胡伯涛的论文…… orz proverbs 题意: N个点M条边的有向图,给出如下两种操作.删除点i的所有出边,代价是Ai.删除点j的所有入边,代价是Bj ...

  5. 图论(网络流,二分图最小点权覆盖):POJ 2125 Destroying The Graph

    Destroying The Graph   Description Alice and Bob play the following game. First, Alice draws some di ...

  6. POJ 2125 Destroying The Graph [最小割 打印方案]

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 2 ...

  7. POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)

                                                          Destroying The Graph Time Limit: 2000MS   Memo ...

  8. AC日记——codevs1688求逆序对

    AC日记--codevs1688求逆序对 锵炬 掭约芴巷 枷锤霍蚣 蟠道初盛 到被他尽情地踩在脚下蹂躏心中就无比的兴奋他是怎么都 ㄥ|囿楣 定要将他剁成肉泥.挫骨扬灰跟随着戴爷这么多年刁梅生 圃鳋 ...

  9. Destroying The Graph 最小点权集--最小割--最大流

    Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...

随机推荐

  1. Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)

    Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...

  2. 变量赋值理解--Pyton中让两个值互换的方法

    #Pyton中让两个值互换的方法 #方法一:可以理解为相当于是同时赋值 a = 5 b = 4 a,b = b,a print(a,b) #方法二:可以理解为拿箱子过程 c = 10 d = 20 e ...

  3. 分享几个能用的 editplus 注册码

    转载自: https://www.cnblogs.com/shihaiming/p/6422441.html 原文:http://host.zzidc.com/wljc/1286.html EditP ...

  4. destoon 后台入口文件分析

    <?php/* [Destoon B2B System] Copyright (c) 2008-2015 www.destoon.com This is NOT a freeware, use ...

  5. 【Hadoop/Hive/mapreduce】系列之使用union all 命令之后如何对hive表格使用python进行去重

    业务场景大概是这样的,这里由两个hive表格,tableA 和 tableB, 格式内容都是这样的: uid cate1 cate2 在hive QL中,我们知道union有着自动去重的功能,但是那是 ...

  6. python中的内建函数

    本文用作记录python中的内建函数及其功能,本文内容随时补充. 完整的内建函数及其说明参考官方文档:    https://docs.python.org/3.5/library/functions ...

  7. CSS动画小结

    CSS动画 原理:1.画面之间变化  2.视觉暂留作用 常见问题 1.CSS 动画的实现方式有几种 1.transition  2. keyframes(animation) 2.过渡动画和关键帧动画 ...

  8. BZOJ 4027: [HEOI2015]兔子与樱花

    贪心 #include<cstdio> #include<algorithm> using namespace std; int cnt,n,m,F[2000005],c[20 ...

  9. springboot-vue-前后端数据交互

    前端项目: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  10. MongoDB学习-->设置通用的自增ID替代ObjectId

    插入mongodb数据时,会为其分配一个随机id,想要设置通用的自增id,可以进行以下操作 1.创建自增序列 package com.tangzhe.autoid; import lombok.Dat ...