poj2125最小点权覆盖+找一个割集

Destroying The Graph

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8503   Accepted: 2753   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 +

主要是找割边。有构造出来的图知道这个是个二部图加两个源点汇点,二部图之间的连边不可能是割边(INF),所以就dfs(S)然后用vis标记,那么vis[S]一定是1,并且vis[T]一定是0.因为S,T不可能在一个集合里那么从源点处找一下和它相连的边,看vis[]是不是0,是的话就是割边。然后从汇点处找一下和它相连的边,看vis[]是不是1,是的话就是割边。因为是个二部图所以不用dfs直接找一次就可以了
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=280;
const int M=5888;
const int INF=1e9+7;
int head[N],tot,S,T;
int q[N],dis[N],n,m,Q;
bool vis[N];
struct node
{
    int next,v,w;
} e[M<<2];
void add(int u,int v,int w)
{
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}
bool bfs()
{
    memset(dis,-1,sizeof(dis));
    dis[S]=0;
    int l=0,r=0;
    q[r++]=S;
    while(l<r)
    {
        int u=q[l++];
        for(int i=head[u]; ~i; i=e[i].next)
        {
            int v=e[i].v;
            if(dis[v]==-1&&e[i].w>0)
            {
                q[r++]=v;
                dis[v]=dis[u]+1;
                if(v==T)  return true;
            }
        }
    }
    return false;
}
int dfs(int s,int low)
{
    if(s==T||!low) return low;
    int ans=low,a;
    for(int i=head[s]; ~i; i=e[i].next)
    {
        if(e[i].w>0&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans))))
        {
            e[i].w-=a;
            e[i^1].w+=a;
            ans-=a;
            if(!ans) return low;
        }
    }
    if(low==ans) dis[s]=-1;
    return low-ans;
}
void dfs(int u){
    vis[u]=1;
    for(int i=head[u];~i;i=e[i].next) if(!vis[e[i].v]&&e[i].w) dfs(e[i].v);
}
int a[N],b[N];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        S=0,T=2*n+1;
        int x,f,t;
        memset(head,-1,sizeof(head));
        tot=0;
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&x);
            add(S,i,x);
            add(i,S,0);
        }
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&x);
            add(i+n,T,x);
            add(T,i+n,0);
        }
        while(m--)
        {
            scanf("%d%d",&f,&t);
            add(t,f+n,INF);
            add(f+n,t,0);
        }
        int ans=0;
        while(bfs()) ans+=dfs(S,INF);
        printf("%d\n",ans);
        dfs(S);
        int ct1=0,ct2=0;
        for(int i=head[S];~i;i=e[i].next) if(!vis[e[i].v]) a[ct1++]=e[i].v;
        for(int i=head[T];~i;i=e[i].next) if(vis[e[i].v]) b[ct2++]=e[i].v-n;
        printf("%d\n",ct1+ct2);
        for(int i=0;i<ct1;++i) printf("%d +\n",a[i]);
        for(int i=0;i<ct2;++i) printf("%d -\n",b[i]);

    }
}
时间: 2024-07-31 08:27:19

poj2125最小点权覆盖+找一个割集的相关文章

【网络流】【最小点权覆盖】【NEERC 2003】【POJ2125】【cogs 1575】有向图破坏

1575. [NEERC 2003][POJ2125]有向图破坏 ★★★ 输入文件:destroyingthegraph.in 输出文件:destroyingthegraph.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] Alice和Bob正在玩如下的游戏.首先Alice画一个有N个顶点,M条边的有向图.然后Bob试着摧毁它.在一次操作中他可以找到图中的一个点,并且删除它所有的入边或所有的出边. Alice给每个点定义了两个值:Wi+和Wi-.如果Bob删除了第i个点

POJ2125 Destroying The Graph 二分图 + 最小点权覆盖 + 最小割

思路来源:http://blog.csdn.net/lenleaves/article/details/7873441 求最小点权覆盖,同样求一个最小割,但是要求出割去了那些边, 只要用最终的剩余网络进行一次遍历就可以了,比较简单. 建图:同样是一个二分图,左边的点代表去掉出边, 右边的点代表去掉入边(小心别弄混),左边去掉出边的点与源点相连, 容量为wi- . 然后更据给出的弧进行连线,权值为INF 使用很好理解的EK算法:(360MS) //#pragma comment(linker, "

POJ2125 Destroying The Graph 最小点权覆盖

题目链接: poj2125 题意: 给出一张N个顶点M条边的有向图. 对于每个顶点x,有两种操作: 1,删除所有进入x的边,花费为a; 2.删除所有从x出去的边,花费为b. 问把图中所有边删除所需要的最小花费.并输出对应的操作. 解题思路: 由题目条件(删除入边,删除出边)首先想到应该是拆点. 这样题目的问题转化为最小点权覆盖问题.即用最少(花费)的顶点覆盖所有边 对于这个问题,我们可以用网络流中的最小割解决,方法如下: 源点连接拆后的出点,容量为b 汇点连接拆后的入点,容量为a 已有边容量为无

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

Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8158   Accepted: 2620   Special Judge Description Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that B

HLG1407Leyni的游戏【最小点权覆盖集】

大意: 给你一个n行m列的矩阵 1 2 1 1 每次操作可使一整行或一整列的一个数减少1(如果是0则不变) 问最少多少次操作会使所有的数变为零 分析: 该题很像poj消灭外星人的那道题 思路也差不很多 将x轴当左集合,y轴当右集合,边权值为所在点的数字 那么一条边就代表了矩阵中的一个点 只要找出最小的权值去覆盖所有的边就能把所有的数字变为零 也就是传说中的最小点权覆盖集 最小点权覆盖集 = 最大权匹配 KM跑一遍就可以了 但是需要注意的是如果两边点的个数不相等 那么我们用虚拟点代替就可以了 代码

HDU 1569 - 方格取数(2) - [最大点权独立集与最小点权覆盖集]

嗯,这是关于最大点权独立集与最小点权覆盖集的姿势,很简单对吧,然后开始看题. HDU1569: Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description 给你一个m*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取数所在的2个格子不能相邻,并且取出的数的和最大. Input 包括多个测试实例,

poj 3308 Paratroopers 最小割 最小点权覆盖

题目链接:http://poj.org/problem?id=3308 题意: 有一个M*N的图,上面的一些点上有伞兵. 可以设置一些枪在每行或者每列上,通过射击,这行或这列的伞兵就会被消灭.每个枪的设置有一个花费,如果设置多个枪,那么花费是设置每个枪的乘积. 问消灭所有伞兵最少的花费是多少. 思路: 每个点的伞兵至少要用那一列或者那一行设置的枪去消灭,那么就可以应用点覆盖的模型.把伞兵看成是一条边,这条边至少要用一个点来覆盖. 而题目中最终花费是所有花费的乘积,那么可以用对数log(x)+lo

poj3308 Paratroopers --- 最小点权覆盖-&amp;gt;最小割

题目是一个非常明显的二分图带权匹配模型, 加入源点到nx建边,ny到汇点建边,(nx.ny)=inf建边.求最小割既得最小点权覆盖. 在本题中因为求的是乘积,所以先所有取log转换为加法,最后再乘方回来. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #in

POJ 3308 Paratroopers (二分图最小点权覆盖 -&gt; 最小割 -&gt; 最大流)

POJ 3308 Paratroopers 链接:http://poj.org/problem?id=3308 题意:有一个N*M的方阵,有L个伞兵降落在方阵上.现在要将所有的伞兵都消灭掉,可以在每行每列装一个高射炮,如果在某行(某列)装上高射炮之后,能够消灭所有落在该行(该列)的伞兵.每行每列安高射炮有费用,问如何安装能够使得费用之积最小. 思路:首先题目要求乘积最小,将乘积对e取对数,会发现就变成了求和.然后抽象出一个二分图,每一行是x部的一个点,每个点有权值,权值为费用取ln.每一列是y部