POJ訓練計劃3041_Asteroids(二分圖/最小點覆蓋=最大匹配),poj3041_asteroids
解題報告
http://blog.csdn.net/juncoder/article/details/38135053
題目傳送門
題意:
給出NxN的矩陣,有M個點是障礙
每次只能刪除一行或者一列,最少刪除多少次才能清除障礙
思路:
把行和列看作兩個集合結點,把障礙看作集合結點的連線,這樣就轉化成求用最少的點來消滅邊,也就是最小點覆蓋。
在二分圖中:(n個結點,且沒有孤立的點)
最小點覆蓋=最大匹配
最大點獨立=結點數-最大匹配
#include <queue>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#define N 510using namespace std;int mmap[N+N][N+N],n,k,vis[N+N],pre[N+N];int dfs(int x){ int i; for(i=n+1; i<=n+n; i++) { if(!vis[i]&&mmap[x][i]) { vis[i]=1; if(pre[i]==-1||dfs(pre[i])) { pre[i]=x; return 1; } } } return 0;}int main(){ int i,j,x,y; while(cin>>n>>k) { memset(mmap,0,sizeof(mmap)); memset(pre,-1,sizeof(pre)); for(i=0; i<k; i++) { cin>>x>>y; mmap[x][n+y]=1; } int ans=0; for(i=1; i<=n; i++) { memset(vis,0,sizeof(vis)); ans+=dfs(i); } cout<<ans<<endl; }}
Asteroids
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 14694 |
|
Accepted: 8016 |
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 41 11 32 23 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).
Source
USACO 2005 November Gold
ACM入門學什
初學者建議購買,《演算法競賽入門經典》 劉汝佳作,十分好,在深入可以是他的另外一本,黑書,《演算法藝術與資訊學競賽》。
計劃:
ACM的演算法(覺得很好,有層次感)POJ上的一些水題(可用來練手和增加自信)
(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)
初期:
一.基本演算法:
(1)枚舉. (poj1753,poj2965)
(2)貪心(poj1328,poj2109,poj2586)
(3)遞迴和分治法.
(4)遞推.
(5)構造法.(poj3295)
(6)類比法.(poj1068,poj2632,poj1573,poj2993,poj2996)
二.圖演算法:
(1)圖的深度優先遍曆和廣度優先遍曆.
(2)最短路徑演算法(dijkstra,bellman-ford,floyd,heap+dijkstra)
(poj1860,poj3259,poj1062,poj2253,poj1125,poj2240)
(3)最小產生樹演算法(prim,kruskal)
(poj1789,poj2485,poj1258,poj3026)
(4)拓撲排序 (poj1094)
(5)二分圖的最大匹配 (匈牙利演算法) (poj3041,poj3020)
(6)最大流的增廣路演算法(KM演算法). (poj1459,poj3436)
三.資料結構.
(1)串 (poj1035,poj3080,poj1936)
(2)排序(快排、歸併排(與逆序數有關)、堆排) (poj2388,poj2299)
(3)簡單並查集的應用.
(4)雜湊表和二分尋找等高效尋找法(數的Hash,串的Hash)
(poj3349,poj3274,POJ2151,poj1840,poj2002,poj2503)
(5)哈夫曼樹(poj3253)
(6)堆
(7)trie樹(靜態建樹、動態建樹) (poj2513)
四.簡單搜尋
(1)深度優先搜尋 (poj2488,poj3083,poj3009,poj1321,poj2251)
(2)廣度優先搜尋(poj3278,poj1426,poj3126,poj3087.poj3414)
(3)簡單搜尋技巧和剪枝(poj2531,poj1416,poj2676,1129)
五.動態規劃
(1)背包問題. (poj1837,poj1276)
(2)型如下表的簡單DP(可參考lrj的書 page149):
1.E[j]=opt{D[i]+w(i,j)} (poj3267,poj1836,poj1260,poj2533)
2.E[i,j]=opt{D[i-1,j]+xi,D[i,j-1]+yj,D[i-1][j-1]+zij} (最長公用子序列)
(poj3176,poj1080,poj1159)
3.C[i,j]=w[i,j]+opt{C[i,k-1]+C[k,j]}.(最優二分檢索樹問題)
六.數學
(1)組合數學:
1.加法原理和乘法原理.
2.排列組合.
......餘下全文>>
小弟大一學ACM,推薦幾本書,
我是一個大三的學生!自大學一年級,我開始做的ACM,我覺得你可能有資格在杭州新華下載了一套杭電ACM課件
PPT,你將有很大的提高!有什麼不明白在網上搜尋!現在有很多的線上答疑!我覺得網上資源太豐富。比一本好書,而不是為了金錢!你就會快速的得到提高,對細節的關注,恒功率,以協助您確定問題是否糾正你做,有時你認為你自己的問題,正確的,但不完全。杭電可以幫你瞭解一下。事實上,遊戲的時間,很多的答案認為是正確的,但筆者不上去,細節決定成敗。一個良好的開端,從大一在杭州新華有權這樣做。你會成為高手!