Poj_3041 Asteroids -最大边覆盖

题目:给你一个n*n的矩阵,你有一把贯穿型的毁灭武器,一发可以整行或整列的小行星射得灰飞湮灭,问最少射多少发把所有行星射掉

吐槽:看到这么多点,边不是很多,然后改邻接表,后来发现,时间也差不多。

/************************************************
Author        :DarkTong
Created Time  :2016/7/31 17:00:15
File Name     :Poj_2538.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 500 + 10;
//int w[maxn][maxn];
vector<int> w[maxn];
int n, m;
int Left[maxn];
bool used[maxn];
bool match(int j)
{
    for(int i=0;i<w[j].size();++i) if(!used[w[j][i]])
    {
        int v = w[j][i];
        used[v] = true;
        if(!Left[v]||match(Left[v]))
        {
            Left[v] = j;
            return true;
        }
    }
    return false;
}
//返回最大匹配数
int hungary()
{
    int res=0;
    memset(Left, 0, sizeof(Left));
    for(int i=1;i<=m;++i)
    {
        memset(used, 0, sizeof(used));
        if(match(i)) res++;
    }
    return res;
}

int main()
{
    int T, cas=1, k;
    while(scanf("%d%d", &n, &k)==2)
    {
    //    memset(w, 0, sizeof(w));
        for(int i=1;i<maxn;++i) w[i].clear();
        m=n;

        int u, v;
        for(int i=1;i<=k;++i)
        {
            scanf("%d%d", &u, &v);
        //    w[u][v]=1;
            w[v].push_back(u);
        }
        printf("%d\n", hungary());
    }

    return 0;
}

时间: 2024-10-25 01:05:54

Poj_3041 Asteroids -最大边覆盖的相关文章

二分图匹配(匈牙利算法) POJ 3041 Asteroids

题目传送门 1 /* 2 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 3 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 4 趣味入门:http://blog.csdn.net/dark_scope/article/details/8880547 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include

HDU 1240.Asteroids!

Asteroids! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1240 Description You're in space. You want to get home. There are asteroids. You don't want to hit them. Input Input to this problem will

HDU 1240 Asteroids!(BFS)

题目链接 Problem Description You're in space.You want to get home.There are asteroids.You don't want to hit them. Input Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the fo

POJ 3041 Asteroids 二分图

原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17985   Accepted: 9798 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <

poj 3020Antenna Placement 最小边覆盖

//最小边覆盖 //最小边覆盖=最大独立集=n-最大匹配 //这个是在原图是二分图上进行的 //由于此题为无向图 //最小边覆盖=最大独立集=n-最大匹配/2; #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 45*15; int line[maxn][maxn]; int match[maxn]; int vis[maxn]; cha

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 Description 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 astero

POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

题目链接: http://poj.org/problem?id=3041 Description 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

hdu4185+poj3020(最大匹配+最小边覆盖)

传送门:hdu4185 Oil Skimming 题意:n*n的方格里有字符*和#,只能在字符#上放1*2的板子且不能相交,求最多能放多少个. 分析:直接给#字符编号,然后相邻的可以匹配,建边后无向图跑匈牙利算法,最后得到的最大匹配数/2. #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <

poj3041 Asteroids --- 最小点覆盖

#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<queue> const int maxn=510; using namespace std; int my[maxn],mx[maxn],vis[maxn],e[maxn][maxn],n;