poj-3041 Asteroids (binary graph max match + Hungarian algorithm)

Source: Internet
Author: User

http://poj.org/problem?id=3041

There are K asteroids in the N*n grid, where the asteroid I is (RI,CI), and now there is a powerful weapon that can blast a whole row or a whole array of asteroids to ashes with a luminous speed, and to use this weapon to destroy all asteroids requires at least a few luminous beams.

Mainly the composition, will each line into a point, constitute a set of 1, each column also as a point, the composition of the set 2, each obstacle position coordinates will set 1 and set 2 points together, that is, each obstacle as the edge of the connection node, so that the subject is a minimum point coverage problem = The maximum match of the binary graph.

Can be solved by the Hungarian algorithm.

The adjacency table implements 0MS.

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 classedge{6  Public:7     intV,nex;8 };9Edge e[10001];Ten intn,m,k,head[10001]; One intlink[501]; A BOOLvis[501]; -  - voidInit () the { -k=0; -Memset (Head,0,sizeof(head)); - } + voidAddedge (intBinta) -{//The algorithm to add edges to the graph, note that the forward edge is added//the subsequent nodes of B are both a---->b +e[k].v=A; Ae[k].nex=Head[b]; athead[b]=k;k++; - } - BOOLDfsintu) { -      for(inti = Head[u]; I! =0; i =E[i].nex) { -         intv =e[i].v; -         if(!Vis[v]) { inVIS[V] =true; -             if(Link[v] = =-1||DFS (Link[v])) { toLINK[V] =u; +                 return true; -             } the         } *     } $     return false;Panax Notoginseng } - intMain () the { +     intu,v; Ascanf"%d%d",&n,&m); the init (); +      for(intI=0; i<m;i++) -     { $scanf"%d%d",&u,&v); $Addedge (V-1, U-1); -     } -     intans=0; thememset (link,-1,sizeof(link)); -      for(inti =0; I < n; i + +){Wuyimemset (Vis,0,sizeof(Vis)); the         if(Dfs (i)) ans++; -     } Wuprintf"%d\n", ans); -     return 0; About}

Adjacency Matrix:

1#include <cstdio>2#include <cstring>3 Const intmaxn=501;4 intUN,VN;//Number of U,v5 intG[MAXN][MAXN];//the number is 0~n-1.6 intLINKER[MAXN];7 BOOLUSED[MAXN];8 BOOLDfsintu)9 {Ten     intv; One      for(v=0; v<un;v++) A         if(g[u][v]&&!Used[v]) -         { -used[v]=true; the             if(linker[v]==-1||DFS (Linker[v])) -             { -linker[v]=u; -                 return true; +             } -         } +     return false; A } at intHungary () - { -     intres=0; -     intu; -memset (linker,-1,sizeof(linker)); -      for(u=0; u<un;u++) in     { -memset (Used,0,sizeof(used)); to         if(Dfs (U)) res++; +     } -     returnRes; the } *  $ intMain ()Panax Notoginseng { -     intu,v; thescanf"%d%d",&un,&VN); +Memset (G,0,sizeof(g)); A      while(vn--) the     { +scanf"%d%d",&u,&v); -g[u-1][v-1]=1; $     } $printf"%d\n", Hungary ()); -     return 0; -  the}

poj-3041 Asteroids (binary graph max match + 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.