poj 3041 (水题)

https://vjudge.net/problem/POJ-3041

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).

题意:

行与行之间相互独立,一个行可以就炸掉很多列。(列的道理一样),如果替换一些字。

点与点之间相互独立,一个点就可以炸掉很多边。

思路就是上图

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 2005
typedef long long ll;
#define inf 10000009
using namespace std;
int n,k;
int zzz,x,y;
bool can[maxn][maxn];//can[][]表示计算机i能够处理的任务
struct edge{
   int to,cap,rev;    //边的终点,容量,反向边
};
vector<edge>mp[maxn];  // 邻接图
bool vis[maxn];
void add_edge(int from,int to,int cap)   //建图
{
    mp[from].push_back((edge){to,cap,mp[to].size()});
     mp[to].push_back((edge){from,0,mp[from].size()-1});  //反向弧
}
int dfs(int v,int t,int f)   //找增广路 v,t是最终点  用了f的流量
{
   if(v==t)return f;
   vis[v]=true;
   for(int i=0;i<mp[v].size();i++)
   {
       edge &e=mp[v][i];
       if(!vis[e.to]&&e.cap>0)
       {
           int d=dfs(e.to,t,min(f,e.cap));  //遍历所有的路径
           if(d>0)
           {
               e.cap-=d;                    //求增加的流量
               mp[e.to][e.rev].cap+=d;
               return d;
           }
       }
   }
   return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    for(;;)
    {
        memset(vis,0,sizeof(vis));   //初始化
        int f=dfs(s,t,inf);
        if(f==0)return flow;
        flow+=f;
    }
}
void solve()
{
    int s=n+n+1;
    int t=s+1;
    for(int i=0;i<n;i++)
    {
        add_edge(s,i,1);
    }
    for(int i=0;i<n;i++)
    {
        add_edge(n+i,t,1);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(can[i][j]==1)
            {
                //cout<<i<<" "<<j;
               add_edge(i,n+j,1);
            }
        }
    }
    cout<<max_flow(s,t)<<endl;
}
int main()
{
    while(cin>>n>>k)
    {
        memset(can,0,sizeof(can));
        for(int i=0;i<k;i++)
        {
            cin>>x>>y;
            x--;
            y--;
          can[x][y]=1;
        }
        solve();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/huangzzz/p/9459196.html

时间: 2024-10-13 17:29:40

poj 3041 (水题)的相关文章

poj 2245 水题

求组合数,dfs即可 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 using namespace std; 8 int N,m,t; 9 int a[15],vis[15]; 10 void dfs(int st,int n) 11 { 12

poj 2369 Permutations 置换水题

找循环节求lcm就够了,若答案是12345应该输出1,被坑了下. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; #define INF 0x3FFFFFF #define MAXN 2222 #define eps 1e-6 i

poj 1005:I Think I Need a Houseboat(水题,模拟)

I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85149   Accepted: 36857 Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land,

POJ 3030. Nasty Hacks 模拟水题

Nasty Hacks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13136   Accepted: 9077 Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends.

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

POJ 2545+2591+2247+1338简单水题

[题意简述]:就是有这样的一个序列,就拿当p1 = 2,p2 = 3, p3 = 5,来举例,由这三个数为基准组成的序列是: 2,3,4,5,6,8,9,10,12--现在给你这个序列数组的下标,让你求得这个数组中,这个下标里的数是多少. [分析]:2,3,4,5,6,8,9,10,12--这个序列式由2,3,5这三个数生成的,具体如何生成,就是: 详见代码: 这里以POJ2545为例: //弄清其中的逻辑关系,可以从最简单的2,3,5试着做起! #include<iostream> #inc

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

poj 1002:487-3279(水题,提高题 / hash)

487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 236746   Accepted: 41288 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phras

POJ 1488 Tex Quotes --- 水题

POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 Tex Quotes --- 水题 */ #include <cstdio> #include <cstring> int main() { #ifdef _LOCAL freopen("D:\\input.txt", "r", stdin); #