"POJ 1151" Air Raid

Source: Internet
Author: User

"POJ 1151" Air Raid

Minimum path coverage problem for Dag graphs (no loop directed graphs)

= max Match + (n-2* Max match)
(Maximum match * * For all the points that can be connected
n-2* maximum match to find all independent points plus maximum matching is the minimum required path)

The following references blog: http://blog.csdn.net/whosemario/article/details/8513836

Defined:

一个有向无环图,要求用尽量少的不相交的简单路径覆盖所有的节点。

Composition:

建立一个二分图,把原图中的所有节点分成两份(X集合为i,Y集合为i‘),如果原来图中有i->j的有向边,则在二分图中建立i->j‘的有向边。最终|最小路径覆盖|=|V|-|M|

Prove:

, the left side of the DAG is constructed with the right dichotomy, and you can find a maximum matching m:1->3 ' of the binary graph.
3->4 ', then how do these two matching edges of M-weight correspond to the edges of the paths in the Dag?
Make an edge of the binary map correspond to one of the forward edges in the Dag: 1->3 ' corresponds to the 1->3 of the left figure, so that 1 in the DAG has a successor node (the only successor of 3 to 1, because a vertex in the binary graph is associated with an edge!) ), so 1 does not become the end vertex in a path in the DAG, and similarly, 3->4 ' 3->4,3 that corresponds to the left figure will not be the end vertex, so there are 4 vertices in total, minus 2 with successive vertices, and two vertices, the end vertex of the DAG path, Each vertex corresponds to a path. Finding the maximum match m in the binary graph is to find the maximum number of non-path end vertices in the corresponding DAG, then in the Dag | v|-| M| is the minimum number of end vertices in the DAG, which is the minimum number of paths for the DAG.

The code is as follows:

#include <cstdlib>#include <cstdio>#include <cstring>using namespace STD;intNBOOLvis[121],mp[121][121];intlink[121];BOOLCanintP) {intI for(i =1; I <= N; ++i) {if(!vis[i] && mp[p][i]) {Vis[i] =true;if(Link[i] = =-1|| Can (Link[i])) {link[i] = p;return 1; } Vis[i] =false; }    }return 0;}intMain () {intt,m,a,b,cnt;scanf("%d", &t); while(t--) {memset(MP,false,sizeof(MP));scanf("%d%d", &n,&m); while(m--) {scanf("%d%d", &a,&b); MP[A][B] =true; }memset(link,-1,sizeof(link)); CNT =0; for(inti =1; I <= N; ++i) {memset(Vis,0,sizeof(VIS));if(Can (i)) cnt++; }printf("%d\n", n-cnt); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"POJ 1151" Air Raid

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.