zoukankan      html  css  js  c++  java
  • poj 2125(最小点权覆盖)

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define N 250
    #define M 20100
    #define INF 0x3fffffff
    
    struct node
    {
        int from,to,next,w;
    }edge[M];
    
    int n,m;
    int cnt,pre[N];
    int s1,nn,t;
    int lv[N],gap[N];
    int kk;
    int mark[N];
    int save[110][2];
    
    void add_edge(int u,int v,int w)
    {
        edge[cnt].from=u;
        edge[cnt].to=v;
        edge[cnt].w=w;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    int sdfs(int s,int w)
    {
        if(s==t) return w;
        int f=0;
        int mi=nn;
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(edge[p].w!=0)
            {
                if(lv[v]==lv[s]-1)
                {
                    int tmp=sdfs(v,min(w-f,edge[p].w));
                    edge[p].w-=tmp;
                    edge[p^1].w+=tmp;
                    f += tmp;
                    if(f==w||lv[s]==nn) return f;
                }
                if(lv[v] < mi) mi=lv[v];
            }
        }
        if(f==0)
        {
            gap[ lv[s] ]--;
            if(gap[lv[s]]==0)
            {
                lv[s]=nn;
                return 0;
            }
            lv[s]=mi+1;
            gap[ lv[s] ]++;
        }
        return f;
    }
    
    int sap()
    {
        nn=t+1;
        int sum=0;
        memset(lv,0,sizeof(lv));
        memset(gap,0,sizeof(gap));
        gap[0]=nn;
        while(lv[s1]<nn)
        {
            sum += sdfs(s1,INF);
        }
        return sum;
    }
    
    void dfs(int s)
    {
        mark[s]=1;
        for(int p=pre[s];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            if(mark[v]==0 && edge[p].w!=0)
            {
                if(v>n) kk++;
                else kk--;
                dfs(v);
            }
        }
    }
    
    int main()
    {
        cnt=0;
        memset(pre,-1,sizeof(pre));
        scanf("%d%d",&n,&m);
        s1=0; t=2*n+1;
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            add_edge(n+i,t,tmp);
            add_edge(t,n+i,0);
        }
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            add_edge(s1,i,tmp);
            add_edge(i,s1,0);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge(x,n+y,INF);
            add_edge(n+y,x,0);
        }
        kk=n;
        printf("%d\n",sap());
        memset(mark,0,sizeof(mark));
        dfs(s1);
        printf("%d\n",kk);
        for(int i=1;i<=n;i++)
        {
            if(mark[i]==0) printf("%d -\n",i);
            if(mark[i+n]==1) printf("%d +\n",i);
        }
        return 0;
    }

    想了N 久, 竟然把这题出在了二分图里面本来我都想到了用网络流做。。。

    知道用网络流,就是基础的最小割模型了。

    然后又卡在了一个地方,怎样把最小割集输出来。 百思不得其姐

    发现用dfs一次就行了。 不过这种用dfs的方法貌似只能用在这样的图(基本的最小割模型)上

    Destroying The Graph
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6031   Accepted: 1911   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
  • 相关阅读:
    遍历二维数组
    冒泡跟扫描共用
    1.8作业
    1.7作业
    1.5作业
    百文百鸡 水仙花数 百马百担
    for循环输出九九乘法表
    循环语句
    1.4作业
    07、找出1-99之间的同构数
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3020490.html
Copyright © 2011-2022 走看看