Original Topic Connection: http://poj.org/problem?id=3041
Asteroids
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 17985 |
|
Accepted: 9798 |
Description
Bessie wants to navigate she spaceship through a dangerous asteroid field in the shape of an n x N grid (1 <= N <= 5 ). The grid contains K asteroids (1 <= k <=), which is conveniently located at the lattice points of the G Rid.
Fortunately, Bessie have a powerful weapon that can vaporize all the asteroids on any given row or column of the grid with A single shot. This weapon was quite expensive, so she wishes to use it sparingly. Given the location of the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate a ll of the asteroids.
Input
* Line 1:two integers N and K, separated to a single space.
* Lines 2..k+1:each line contains, 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 a asteroid and "." is empty space:
x.x
. X.
. X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (all) and (1,3), and then she could fire down column 2 to destroy T He asteroids at (2,2) and (3,2).
Source
Usaco 2005 November Gold test instructions:
Give you a $n*n$ chess board, some of which have pieces, you can eliminate a column or a line at a time, ask you at least how many times to eliminate the pieces.
Exercises
This is a two-point matching classic question. By connecting the horizontal axis of each piece, a binary graph is formed, and the problem is transformed into a minimum point overlay for solving the binary graph. The minimum point coverage is the maximum match, and running a dinic is good.
Code:
#include <iostream>#include<stack>#include<vector>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<cstdio>#include<queue>#definemax_s (1<<10) +10#defineMax_v 1234#defineMax_n Max_v#defineINF 1000009using namespacestd;structEdge {intto, Cap, rev; BOOLIsrev; Edge (intTintCintRBOOLi): to (t), Cap (c), Rev (R), Isrev (i) {} edge () {}};template<classT>inlineBOOLScan_d (T &ret) { CharC; intSGN; if(C=getchar (), c==eof)return 0;//EOF while(c!=' -'&& (c<'0'|| C>'9')) c=GetChar (); SGN= (c==' -')?-1:1; RET= (c==' -')?0:(C-'0' ); while(C=getchar (), c>='0'&&c<='9') ret=ret*Ten+ (C-'0' ); RET*=SGN; return 1;} Vector<edge>G[max_n];intLevel[max_v];intIter[max_v];voidInitintTotnode) { for(inti =0; I <= Totnode; i++) g[i].clear (); Memset (Level,0,sizeof(level)); memset (ITER,0,sizeof(ITER));}voidAdd_edge (int from,intTo,intcap) {g[ from].push_back (Edge (To, Cap, g[to].size (),0)); G[to].push_back (Edge ( from,0, g[ from].size ()-1,1));}voidBFsints) {queue<int>que; Memset (Level,-1,sizeof(level)); Level[s]=0; Que.push (s); while(!Que.empty ()) { intv =Que.front (); Que.pop (); for(inti =0; I < g[v].size (); i++) {Edge&e =G[v][i]; if(E.cap >0&& Level[e.to] <0) {level[e.to]= Level[v] +1; Que.push (e.to); } } }}intDfsintVintTintf) {if(v = = t)returnF; for(int&i = Iter[v]; I < g[v].size (); i++) {Edge&e =G[v][i]; if(E.cap >0&& Level[v] <Level[e.to]) { intD =dfs (e.to, T, Min (f, e.cap)); if(D >0) {E.cap-=D; G[e.to][e.rev].cap+=D; returnD; } } } return 0;}intMax_flow (intSintt) {intFlow =0; for (; ;) {BFS (s); if(Level[t] <0)returnflow; memset (ITER,0,sizeof(ITER)); intF; while(f = DFS (S, T, INF)) >0) {Flow+=F; } }}intn,m;ints=1233, t=1212;intMain () {Cin.sync_with_stdio (false); CIN>> N >>m; for(inti =0; I < m; i++) { intu, v; CIN>> u >>v; Add_edge (U, v+n,1); } for(inti =1; I <= N; i++) {Add_edge (S, I,1); Add_edge (i+n, T,1); } cout<< Max_flow (S, T) <<Endl; return 0;}
POJ 3041 Asteroids two-part map