zoukankan      html  css  js  c++  java
  • HDU1054 Strategic Game——匈牙利算法

    Strategic Game
    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? 

    Your program should find the minimum number of soldiers that Bob has to put for a given tree. 

    The input file contains several data sets in text format. Each data set represents a tree with the following description: 

    the number of nodes 
    the description of each node in the following format 
    node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier 
    or 
    node_identifier:(0) 

    The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data. 

    For example for the tree: 

     

    the solution is one soldier ( at the node 1). 

    The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table: 

    Input

    4
    0:(1) 1
    1:(2) 2 3
    2:(0)
    3:(0)
    5
    3:(3) 1 4 2
    1:(1) 0
    2:(0)
    0:(0)
    4:(0)

    Output

    1
    2

    题意:在一棵树里面查找最小的点覆盖。

    _______________________________________________________________________________________________________

    求最小点覆盖,匈牙利算法。

    匈牙利算法,求二分图最大匹配。

    一些定义,自己的话:

    二分图:可以把图中的点看出人,男女分成2组,男男,女女同性之间没有直接连边。

    匹配:二分图中,2中中各取一个人,有边相连,就是一个匹配。就是两夫妻一个在这边一个在那边,这就是匹配,当然一夫一妻制。

    最大匹配:最多可组成的夫妻对数就是了。

    下面有一些定理(引自网络):

    (1)二分图的最小顶点覆盖 

    最小顶点覆盖要求用最少的点(X或Y中都行),让每条边都至少和其中一个点关联。

    Knoig定理:二分图的最小顶点覆盖数等于二分图的最大匹配数。

     

    (2)DAG图的最小路径覆盖 

    用尽量少的不相交简单路径覆盖有向无环图(DAG)G的所有顶点,这就是DAG图的最小路径覆盖问题。

    结论:DAG图的最小路径覆盖数 = 节点数(n)- 最大匹配数(m)

     

    (3)二分图的最大独立集

    最大独立集问题: 在N个点的图G中选出m个点,使这m个点两两之间没有边.求m最大值

    结论:二分图的最大独立集数 = 节点数(n)— 最大匹配数(m)

    匈牙利算法可以参考程序中的代码,如果看不懂(本人水平实在有限)可以参考http://blog.csdn.net/dark_scope/article/details/8880547 真正的通俗易懂!

    双向边要除以2,切记!

    _______________________________________________________________________________________________________

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 const int maxn=1510;
     8 int n;
     9 int head[maxn],js;
    10 struct edge
    11 {
    12     int u,v,next;
    13 }e[maxn*2];
    14 int link[maxn];
    15 bool vis[maxn];
    16 void init()
    17 {
    18     memset(e,0,sizeof(e));
    19     memset(head,0,sizeof(head));
    20     js=0;
    21     memset(link,-1,sizeof(link));
    22 }
    23 void addage(int u,int v)
    24 {
    25     e[++js].u=u;e[js].v=v;
    26     e[js].next=head[u];head[u]=js;
    27 }
    28 bool dfs(int u)
    29 {
    30     for(int i=head[u];i;i=e[i].next)
    31     {
    32         int v=e[i].v;
    33         if(!vis[v])
    34         {
    35             vis[v]=1;
    36             if(link[v]==-1 || dfs(link[v]))
    37             {
    38                 link[v]=u;
    39                 return 1;
    40             }
    41         }
    42     }
    43     return 0;
    44 }
    45 int main()
    46 {
    47     while(scanf("%d",&n)==1)
    48     {
    49         init();
    50         for(int u,s,i=0;i<n;i++)
    51         {
    52             scanf("%d:(%d)",&u,&s);
    53             for(int v,j=0;j<s;j++)
    54             {
    55                 scanf("%d",&v);
    56                 addage(u,v);
    57                 addage(v,u);
    58             }
    59         }
    60         int ans=0;
    61         for(int i=0;i<n;i++)
    62         {
    63             memset(vis,0,sizeof(vis));
    64             if(dfs(i))ans++;
    65         }
    66         printf("%d
    ",ans/2);
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    [BZOJ2729]排队
    [BZOJ2839]集合计数
    [BZOJ2111] Perm 排列计数
    Unet 项目部分代码学习
    数据增强代码
    论文阅读笔记五:U-Net: Convolutional Networks for Biomedical Image Segmentation(CVPR2015)
    CTPN项目部分代码学习
    论文阅读笔记四:CTPN: Detecting Text in Natural Image with Connectionist Text Proposal Network(ECCV2016)
    R2CNN项目部分代码学习
    VOC数据集生成代码使用说明
  • 原文地址:https://www.cnblogs.com/gryzy/p/6237138.html
Copyright © 2011-2022 走看看