UVA 11419 SAM I am topic: Give a rxc grid, cotton spinning on the grid some goals. Bullets can be fired in the net, and the bullets will fly vertically or horizontally, and all targets on the flight path are knocked down. Your task is to figure out the minimum number of bullets to be fired from each location to get all the targets knocked out. Problem solving: K?nig theorem: The minimum number of overlays equals the maximum number of matches. The coordinates of the target are converted into XY nodes, and the rows are treated as x nodes, and the columns are considered as Y nodes. Now the problem becomes, how to choose the least node, covering all the edges. The steps for minimum coverage are as follows: 1) on the right side, find an unmatched point, mark. 2) walk an edge that has not been matched, to the left of the point, mark. 3) walk a matched edge to the right, mark. 4) Repeat the 2,3 step until you can no longer walk. 5) Go back to step one until you find a point that is not matched and is not marked to the right. 6) At the end of the mark, there are no marked points on the right, and the points on the left are marked to cover all edges.
To understand more thoroughly, you can see here.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intN =1005;typedef Long LongllintN, M, K; vector<int>X, Y;voidInit () {x.clear (); Y.clear ();}structbpm{intN, M; vector<int >G[n];intLeft[n];intRight[n];BOOLT[n];BOOLS[n];voidInitintNintm) { This->n = n; This->m = m; for(inti =0; i < N; i++) g[i].clear (); }voidAddedge (intUintV) {g[u].push_back (v);//Build side}BOOLMatchintu) {S[u] =true;//Mark to the right of the dot u for(inti =0; I < g[u].size (); i++) {//Traverse the left point of the connection from the U point intv = g[u][i];if(! T[v]) {//Left unmarked point, go to the side that has not been matchedT[V] =true;if(Left[v] = =-1|| Match (Left[v])) {//Go to the point that matches the edge to the rightLEFT[V] = u; Right[u] = v;return true; } } }return false; }intSolve () {memset(Left,-1,sizeof(left));memset(Right,-1,sizeof(right));intAns =0; for(intU =0; U < n; u++) {memsetS0,sizeof(S));memsetT0,sizeof(T));if(Match (U)) ans++;//First use Hungarian algorithm to find the maximum match}returnAns }intMincover ( vector<int>& X, vector<int>& Y) {intans = solve ();memsetS0,sizeof(S));memsetT0,sizeof(T)); for(intU =0; U < n; u++)//Find an unmarked point in the right point set if(Right[u] = =-1) match (U);//From this unmarked point to the augmented path . for(intU =0; U < n; u++)if(! S[u]) X.push_back (U);//After the mark has ended, record the point that is not marked on the right for(intv =0; v < n; v++)if(T[v]) Y.push_back (v);//Record the point marked to the left returnAns }}bpm;voidInput () {intx, y; for(inti =0; I < K; i++) {scanf("%d%d", &x, &y); x--, y--; Bpm.addedge (x, y); }}intMain () { while(scanf(" %d%d%d", &n, &m, &k) = =3) {if(!n &&!m &&!k) Break; Bpm.init (n, m); Init (); Input ();printf("%d", Bpm.mincover (X, Y)); for(inti =0; I < x.size (); i++)printf("r%d", X[i] +1); for(inti =0; I < y.size (); i++)printf("c%d", Y[i] +1);puts(""); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint.
UVA 11419 SAM I AM (minimum cover König theorem)