POJ1469 COURSES binary map matching Hungarian algorithm

Source: Internet
Author: User

Original link http://www.cnblogs.com/zhouzhendong/p/8232649.html

Topic Portal-POJ1469Test Instructions Summary

In a large matrix, there are some obstacle points.

Now let's cover the non-barrier point with a small rectangle of 1*2, ask not to overwrite the obstacle point and not repeat the overlay, asking if all non-barrier points can be covered.

Solving

The topic is almost bare.

First notice that the two-tuple (x, y) of the read-in representation of the barrier points is a row and X is a column.

This toxic < poor assessment >

Then consider the algorithm. Readers can refer to the author's previous blog post.

For adjacent non-barrier points we built the edges back and forth. Then we give the original image according to a certain point of the Manhattan distance of the odd and even black and white dyeing, found that the black can only be connected to the white, white can only be connected to the black. So this is a two-part diagram.

Then we ran a gun to Hungary.

Because the side of the connection is back and forth, it is equivalent to repeating the calculation once, that is, the maximum number of matches doubled.

So it becomes the maximum number of non-barrier points to cover. The total number of direct and barrier points can be compared.

Note that using a two-dimensional array to store the graph will be tle (I will tle), the change to a group of analog linked list has passed. < poor evaluation ++>

Code
#include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm >using namespace Std;const int n=35,k=1100;struct gragh{int cnt,y[k*4],nxt[k*4],fst[k];void Clear () {Cnt=0;memset ( Fst,0,sizeof FST);} void Add (int a,int b) {y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;}} G;int m,n,k,cnt,pl[n][n],tn[n][n],match[k],vis[k];bool Check (int x,int y) {return 1<=x&&x<=m&& 1<=y&&y<=n&&!pl[x][y];} BOOL Match (int x) {for (int i=g.fst[x];i;i=g.nxt[i]) {int y=g.y[i];if (!vis[y]) {vis[y]=1;if (!match[y]| | Match (Match[y])) {Match[y]=x;return 1;}}} return 0;} int Hungary () {int res=0;memset (match,0,sizeof match), for (int i=1;i<=cnt;i++) {memset (vis,0,sizeof Vis), if (Match (i )) res++;} return res;} int main () {while (~scanf ("%d%d%d", &m,&n,&k)) {memset (pl,0,sizeof pl); memset (tn,0,sizeof tn); for (int i=1, a,b;i<=k;i++) scanf ("%d%d", &b,&a), pl[a][b]=1;cnt=0;for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) if ( !pl[i][J]) Tn[i][j]=++cnt;g.clear (); for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) {if (Pl[i][j]) continue;if (check (i,j-1 ) G.add (Tn[i][j],tn[i][j-1]), if (check (i,j+1)) G.add (tn[i][j],tn[i][j+1]), if (check (i-1,j)) G.add (Tn[i][j],tn[i-1] [j]); if (check (i+1,j)) G.add (Tn[i][j],tn[i+1][j]);} Puts ((Hungary () ==cnt)? " YES ":" NO ");} return 0;}

  

POJ1469 COURSES binary map matching Hungarian algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.