zoukankan      html  css  js  c++  java
  • poj3041

    Asteroids
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12162   Accepted: 6620

    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 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>
    using namespace std;
    
    int n,k;  //n矩阵规格,k星体数量
    int V1,V2;       //二分图顶点集
     /*矩阵的行列分别属于二分图的两个顶点集V1、V2,其中行x∈V1,列y∈V2*/
    bool grid[501][501];  //存储数据方式:可达矩阵
    bool vis[501];     //记录V2的点每个点是否已被搜索过
    int link[501];     //记录 V2中的点y 在 V1中 所匹配的点x的编号
    int m;  //最大匹配数
    
    /*Hungary Algorithm*/
    
    bool dfs(int x)
    {
    	for(int y=1;y<=V2;y++)
    		if(grid[x][y] && !vis[y])  //x到y相邻(有边) 且 节点y未被搜索
    		{
    			vis[y]=true;   //标记节点y已被搜索
    			if(link[y]==0 || dfs(link[y])) //link[y]==0 : 如果y不属于前一个匹配M
    			{                               //find(link[y] : 如果被y匹配到的节点可以寻找到增广路
    				link[y]=x; //那么可以更新匹配M'(用M替代M')
    				return true;  //返回匹配成功的标志
    			}
    		}
    	return false;  //继续查找V1下一个x的邻接节点
    }
    
    void search(void)
    {
    	for(int x=1;x<=V1;x++)
    	{
    		memset(vis,false,sizeof(vis)); //清空上次搜索时的标记
    		if(dfs(x))  //从V1中的节点x开始寻找增广路
    			m++;
    	}
    	return;
    }
    
    int main(void)
    {
    	cin>>n>>k;
    	V1=V2=n;
    
    	int x,y;         //坐标(临时变量)
    	for(int i=1;i<=k;i++)
    	{
    		cin>>x>>y;
    		grid[x][y]=true;   //相邻节点标记
    	}
    
    	/*增广轨搜索*/
    
    	search();
    
    	/*Output*/
    
    	cout<<m<<endl;
    
    	return 0;
    }


  • 相关阅读:
    Oracle存储过程(Stored Procedure)使用自定义类实体参数
    RTF格式富文本图片文本导出到Excel
    将EXCEL表格文字图片信息转成RTF格式写入数据库BLOB字段
    C# 阿拉伯数字转换为中文数字/中文数字转换为阿拉伯数字
    JavaScript操作select下拉框
    JavaScript操作浏览器一直往下滚动
    Microsoft Office 2003 Web Components 自动计数/自动求和
    SQLite递归
    WPF获取验证码倒计时
    System.UnauthorizedAccessException: 对路径“***”的访问被拒绝。
  • 原文地址:https://www.cnblogs.com/riskyer/p/3225885.html
Copyright © 2011-2022 走看看