Asteroids

其实是个水题,二分图的最大匹配,二分图为两边为坐标x,y,直接匈牙利算法做,邻接表边数老是写错,第三次了!!!

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;

const int MAXV = 505;

int pnt[MAXV*MAXV],nxt[MAXV*MAXV],head[MAXV]; //边数为500*500!!!!!

int match[MAXV];

bool vis[MAXV];

int E = 0;

void add(int u,int v){

nxt[E]=head[u];

pnt[E]=v;

head[u]=E++;

}

bool dfs(int u)

{

for(int i=head[u];i!=-1;i=nxt[i]){

int v=pnt[i];

if(!vis[v]){

vis[v]=1;

if(match[v]==-1||dfs(match[v])){

match[v]=u;

return true;

}

}

}

return false;

}

int solve(int n)

{

int ans=0;

memset(match,-1,sizeof(match));

for(int i=1;i<=n;i++)

{

memset(vis,0,sizeof(vis));

if(dfs(i)) ans++;

}

return ans;

}

int main()

{

int n,k;

memset(head,-1,sizeof(head));

memset(vis,0,sizeof(vis));

scanf("%d%d",&n,&k);

while(k--)

{

int x,y;

scanf("%d%d",&x,&y);

add(x,y);

}

printf("%d\n",solve(n));

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-14 11:35:14

Asteroids的相关文章

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

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;

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 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 as

1741: [Usaco2005 nov]Asteroids 穿越小行星群

1741: [Usaco2005 nov]Asteroids 穿越小行星群 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 231  Solved: 166[Submit][Status][Discuss] Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N

HDOJ 1240 Asteroids!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1240 Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3643    Accepted Submission(s): 2413 Problem Description You're in space. You want

HDU 1240 (简单三维广搜) Asteroids!

给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很基础的一道题 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <algorithm