POJ 3041 Asteroids(最大流)

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid.

Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input

  • Line 1: Two integers N and K, separated by a single space.
  • Lines 2…K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
    Output
  • Line 1: The integer representing the minimum number of times Bessie must shoot.
    Sample Input
    3 4
    1 1
    1 3
    2 2
    3 2
    Sample Output
    2
    Hint
    INPUT DETAILS:
    The following diagram represents the data, where “X” is an asteroid and “.” is empty space:
    X.X
    .X.
    .X.

OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).

题意:给你一个n*n的矩阵,其中有k个怪物,你有一个及其强大的武器,每次可以消灭一整行或者一整列的怪物,但是这个武器发射一次需要消耗巨大的费用,现在问你最小需要使用多少次武器。

思路:其实这就是一个二分图的最大匹配,可以使用匈牙利算法求得,但是二分图的最大匹配也等价与网络流的最大流,设输入的位置怪兽位置为(u,v),可以建立一个源点s,将s指向u,建立一个汇点t
v指向t,再将u,v相连接,这样就可以直接直接使用网络流跑一遍就可以了

为什么可以使用二分图最大匹配?
因为如果你已经经过某行或者某列上的一个点,那么该行或者该列上的所有怪物都会被消灭,而二分图最大匹配就是找到最大的不相交的点集个数,不可能存在同一个点选两次的情况,正好与题目相符
因为如果选同一个点两次,必然存在交集

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<map>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
struct node{
    int v,w,next;
}edge[Max];
int head[Max],cur[Max],dis[Max];
int index;
int st,en;
queue<int>p;
map<pair<int,int>,int>m;//标记是否添加过该条边
void init(){
    index=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w){
    edge[index].v=v,edge[index].w=w;
    edge[index].next=head[u],head[u]=index++;

    edge[index].v=u,edge[index].w=0;
    edge[index].next=head[v],head[v]=index++;
}
bool bfs(){
    while(p.size())p.pop();
    memset(dis,0,sizeof(dis));
    p.push(st);
    dis[st]=1;
    while(p.size()){
        int top=p.front();
        p.pop();
        for(int t=head[top];t!=-1;t=edge[t].next){
            if(edge[t].w>0 && dis[edge[t].v]==0){
                dis[edge[t].v]=dis[top]+1;
                p.push(edge[t].v);
            }
        }
    }
    return dis[en];
}
int dfs(int u,int flow){
    if(u==en)
        return flow;
    for(int& t=cur[u];t!=-1;t=edge[t].next){
        if(dis[edge[t].v]==dis[u]+1 && edge[t].w>0){
            int mn=dfs(edge[t].v,min(edge[t].w,flow));
            if(mn>0){
                edge[t].w-=mn;
                edge[t^1].w+=mn;
                return mn;
            }
        }
    }
    return 0;
}
int Dinic(){
    int ans=0,res;
    while(bfs()){
        for(int i=st;i<=en;i++)
            cur[i]=head[i];
        while(res=dfs(st,INT_MAX))
            ans+=res;
    }
    return ans;
}
int main()
{
    int k,n;
    init();
    scanf("%d%d",&n,&k);
    int u,v;
    for(int i=0;i<k;i++){
        scanf("%d%d",&u,&v);
        if(!m.count(make_pair(0,u)))
            add(0,u,1);
        if(!m.count(make_pair(n+v,2*n+1)))
             add(n+v,2*n+1,1);
        if(!m.count(make_pair(u,n+v)))
            add(u,n+v,1);
        m[make_pair(0,u)]=1;
        m[make_pair(n+v,2*n+1)]=1;
        m[make_pair(u,n+v)]=1;
    }
    st=0,en=2*n+1;
    printf("%d\n",Dinic());
    return 0;
}

此题也可以使用二分图最大匹配

AC代码:

//#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
int road[505][10005],used[Max];
int match[Max];
int n,k;
bool dfs(int x){
    for(int i=1;i<=n;i++)
    if(!used[i] && road[x][i]){
        used[i]=1;
        if(!match[i] || dfs(match[i])){
            match[i]=x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    scanf("%d%d",&n,&k);
    int x,y;
    for(int i=0;i<k;i++){
        scanf("%d%d",&x,&y);
        road[x][y]=1;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        memset(used,0,sizeof(used));
        if(dfs(i))
            ans++;
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值