码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2125 最小点权覆盖

时间:2015-04-26 13:43:32      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

Language:
Destroying The Graph
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7469   Accepted: 2381   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 +

一道单纯的最小点权覆盖,但是本小白还是不会写,看了题解,然后还没看懂输出路径的部分,跪求大神指点为嘛是那样输出路径,代码如下:

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<climits>
#define MAXE 220*220
#define MAXP 220
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
struct Edge
{
    int s,t,f,next;
} edge[MAXE];
int head[MAXP];
int cur[MAXP];
int pre[MAXP];
int stack[MAXE];
int sign[MAXE];
int ent;
int sum;
int n,m,s,t,supers,supert;
int num;
void add(int start,int last,int f)
{
    edge[ent].s=start;
    edge[ent].t=last;
    edge[ent].f=f;
    edge[ent].next=head[start];
    head[start]=ent++;
    edge[ent].s=last;
    edge[ent].t=start;
    edge[ent].f=0;
    edge[ent].next=head[last];
    head[last]=ent++;
}
bool bfs(int S,int T)
{
    memset(pre,-1,sizeof(pre));
    pre[S]=0;
    queue<int>q;
    q.push(S);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=head[temp]; i!=-1; i=edge[i].next)
        {
            int temp2=edge[i].t;
            if(pre[temp2]==-1&&edge[i].f)
            {
                pre[temp2]=pre[temp]+1;
                q.push(temp2);
            }
        }
    }
    return pre[T]!=-1;
}
int dinic(int start,int last)
{
    int flow=0,now;
    while(bfs(start,last))
    {
        int top=0;
        memcpy(cur,head,sizeof(head));
        int u=start;
        while(1)
        {
            if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流
            {
                int minn=INT_MAX;
                for(int i=0; i<top; i++)
                {
                    if(minn>edge[stack[i]].f)
                    {
                        minn=edge[stack[i]].f;
                        now=i;
                    }
                }
                flow+=minn;
                for(int i=0; i<top; i++)
                {
                    edge[stack[i]].f-=minn;
                    edge[stack[i]^1].f+=minn;
                }
                top=now;
                u=edge[stack[top]].s;
            }
            for(int i=cur[u]; i!=-1; cur[u]=i=edge[i].next) //找出从u点出发能到的边
                if(edge[i].f&&pre[edge[i].t]==pre[u]+1)
                    break;
            if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯
            {
                if(top==0)break;
                pre[u]=-1;
                u=edge[stack[--top]].s;
            }
            else//如果找到了继续运行
            {
                stack[top++]=cur[u];
                u=edge[cur[u]].t;
            }
        }
    }
    return flow;
}
void dfs(int S)
{
    sign[S]=1;
    for(int i=head[S]; i!=-1; i=edge[i].next)
    {
        if(edge[i].f>0&&!sign[edge[i].t])
        {
            dfs(edge[i].t);
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        memset(sign,0,sizeof(sign));
        ent=0;
        s=0;
        t=2*n+1;
        int cost;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&cost);
            add(i+n,t,cost);
        }
        int temp=ent;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&cost);
            add(s,i,cost);
        }
        int temp2=ent;
        for(int i=1; i<=m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v+n,INT_MAX);
        }
        printf("%d\n",dinic(s,t));
        dfs(s);
        cout<<sign[3]<<endl;
        int cnt=0;
        for(int i=1; i<=2*n; i++)
        {
            if(!sign[i]&&i<=n)cnt++;
            else if(sign[i]&&i>n)cnt++;
        }
        printf("%d\n",cnt);
        for(int i=1; i<=2*n; i++)
        {
            if(!sign[i]&&i<=n)printf("%d -\n",i);
            else if(sign[i]&&i>n)printf("%d +\n",i-n);
        }
    }
    return 0;
}

 

poj 2125 最小点权覆盖

标签:

原文地址:http://www.cnblogs.com/lthb/p/4457599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!