zoukankan      html  css  js  c++  java
  • 最小覆盖路径两道题整合

     

    第一道是poj3020.题意:

    1. 题意:‘*’代表城市 
    2.        'o'代表空地 
    3.        要求你用最小的无线电雷达来覆盖所有的城市 
    4.        雷达只可以建立在城市上 
    5.        每一个雷达,只可以覆盖它自己所在的位置和与它相邻的一个位置 
    6.        【上 || 下 || 左】 
    7.        注意:也就是说一个雷达只可以覆盖两个城市 
    8.  
    9. 算法:二分图的匹配 ———— 最小路径覆盖 
    10.       最小路径覆盖=最小路径覆盖=|G|-最大匹配数 
    11.  
    12. 思路:拆点,把每一个城市看成是两个点 
    13.       具体分析还是看这篇博客学去吧,不能比她说的更好了Orz 
    14.       http://blog.csdn.net/lyy289065406/article/details/6647040 
    15. PS:感觉还是分不怎么清什么时候用最小路径覆盖,什么时候用最大匹配 
    16.     ╮(╯▽╰)╭先记住直接匹配的就用最大匹配 
    17.     要拆点的就用最小路径覆盖好了,水菜伤不起 

    另一篇博客的解释:

    Antenna Placement
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
    Submit
     
    Status
     
    Practice
     
    POJ 3020
    Description
    The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them. 
     
    Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered? 
    
    Input
    On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set ['*','o']. A '*'-character symbolises a point of interest, whereas a 'o'-character represents open space. 
    
    Output
    For each scenario, output the minimum number of antennas necessary to cover all '*'-entries in the scenario's matrix, on a row of its own.
    Sample Input
    2
    7 9
    ooo**oooo
    **oo*ooo*
    o*oo**o**
    ooooooooo
    *******oo
    o*o*oo*oo
    *******oo
    10 1
    *
    *
    *
    o
    *
    *
    *
    *
    *
    *
    Sample Output
    17
    5
    View Code

     重要结论:无向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数/2

    把有向图转换为二分图,寻找最大匹配,根据公式计算出最小覆盖路径。

    在转换为二分图的时候,有两种情况,一种是本题所说,无向二分图,另一种则是有向二分图,在做题的时候应该注意区分。

    他们的区别在于最后是否除以2.

     重要结论:有向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数

    一般用于DAG(有向无环)的最小覆盖路径。

    下面是第一种情况:建立无向二分图:

    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299322779
    提示:别被图片的圈圈误导了,看清楚题目,'*'是城市,'o'是空地,椭圆的天线覆盖范围要覆盖的是城市'*',而不是覆盖空地
     
    题目大意:
    一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市。
    问至少放置多少个基站才能使得所有的城市都覆盖无线?
    
    解题思路:
    思前想后,依稀可以认为是一道求二分图的最小路径覆盖问题
    (注意不是最小点覆盖)
     
    那么接下来需要确认的是,
    究竟是求 有向二分图的最小路覆盖,还是求 无向二分图的最小路覆盖
    因为有向和无向是截然不同的计算方法。
     
    要确认是构造有向图,还是构造无向图,那么就需要先根据题意,看看构造二分图时所使用的方式,更适合构造哪一种二分图。
     
    然后就进入了本题难点:如何构造二分图
     
    首先要明确的是,输入的一堆“圈圈星星”可以看做是一张大地图,地图上有所有城市的坐标,但是这里有一个误区:不能简单地把城市的两个x、y坐标作为准备构造的二分图的两个顶点集。
    城市才是要构造的二分图的顶点!
    构造方法如下:
    例如输入:
    *oo
    ***
    O*o
    时,可以抽象为一个数字地图:
    100
    234
    050
    数字就是根据输入的城市次序作为该城市的编号,0代表该位置没有城市。
    然后根据题目的“范围”规则,从第一个城市开始,以自身作为中心城市,向四个方向的城市进行连线(覆盖)
    因此就能够得到边集:
    e12  e21     e32     e43    e53
         e23     e34
                 e35
    可以看到,这些边都是有向边,但是每一条边都有与其对应的一条相反边。
    即任意两个城市(顶点)之间的边是成对出现的
    那么我们就可以确定下来,应该 构造无向二分图(其实无向=双向)
    因为若要构造有向的二分图时,需要判断已出现的边,是很麻烦的工作
     
    为了把有向图G构造为无向二分图,这里需要引入一个新名词“拆点”
    其实就是把原有向图G的每一个顶点都”拆分(我认为复制更准确)”为2个点,分别属于所要构造的二分图的两个顶点集
     
    例如在刚才的例子中抽出一条有向边e12举例说明:
    复制顶点1和顶点2,使得1,2∈V1;  1’,2’∈V2 ,不难发现|V1|=|V2|
    根据边e12和e21,得到无向二分图:
    
     
    那么同理就可以得到刚才的例子的 无向二分图为:
    
     
    再继而通过无向二分图,以V1的元素作为row,V2的元素作为col,构造 可达矩阵 存储到计算机
       1’  2’  3’  4’  51  F  T   F   F   F
    2  T  F   T   F   F
    3  F  T   F   T   T
    4  F  F   T   F   F
    5  F  F   T   F   F
     
    接下来就是要求这个 无向二分图的最小路径覆盖 了
    利用公式:
     
    无向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数/2
     
    顶点数:就是用于构造无向二分图的城市数,即进行“拆点”操作前的顶点数量
    最大二分匹配书之所以要除以2,是因为进行了“拆点”擦奥做做使得匹配总数多了一倍,因此除以2得到原图的真正的匹配数
     
    最后剩下的问题就是求最大二分匹配数了,用匈牙利算法,这就不多说了,参考POJ3041的做法,基本一摸一样。
     
    
    从这道题得出了一个结论:
    当二分图的两个顶点子集基数相等时,该二分图所有顶点的匹配数 等于 任意一个顶点子集匹配数的2倍 
    
    其实匈牙利算法解题是极为简单的,但是图论的难并不是难在解答,而是建图的过程,也难怪会有牛曰:用匈牙利算法,建图是痛苦的,最后是快乐的。
    View Code

    题解:

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    const int maxx=505;
    char ma[45][15];
    int ma_int[45][15];
    int g[maxx][maxx];
    int result[maxx];
    bool vis[maxx]= {false};
    int row,col;
    int num;
    bool dfs(int pos)
    {
        for(int i=1; i<=num; i++)
        {
            if(!vis[i]&&g[pos][i])
            {
                vis[i]=true;
                if(result[i]==0||dfs(result[i]))
                {
                    result[i]=pos;
                    return true;
                }
            }
        }
        return false;
    
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            num=0;
            memset(ma_int,0,sizeof(ma_int));
            memset(g,0,sizeof(g));
            memset(result,0,sizeof(result));
            scanf("%d%d",&row,&col);
            for(int i=0; i<row; i++)
            {
                scanf("%s",ma[i]);
            }
            for(int i=0; i<row; i++)
            {
                for(int j=0; j<col; j++)
                {
                    if(ma[i][j]=='*') ma_int[i][j]=++num;
                }
            }
            for(int i=0; i<row; i++)
            {
                for(int j=0; j<col; j++)
                {
                    if(ma_int[i][j])
                    {
                        if(i-1>-1&&ma_int[i-1][j]) g[ma_int[i][j]][ma_int[i-1][j]]=1;
                        if(i+1<row&&ma_int[i+1][j]) g[ma_int[i][j]][ma_int[i+1][j]]=1;
                        if(j-1>-1&&ma_int[i][j-1]) g[ma_int[i][j]][ma_int[i][j-1]]=1;
                        if(j+1<col&&ma_int[i][j+1]) g[ma_int[i][j]][ma_int[i][j+1]]=1;
                    }
                }
            }
            int road=0;
    
            for(int i=1; i<=num; i++)
            {
                memset(vis,false,sizeof(vis));
                if(dfs(i)) road++;
            }
            printf("%d
    ",num-(road/2));
    
        }
        return 0;
    }
    View Code

    另一种情况:建立有向二分图:poj2063

    Investment
    Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%lld & %llu
    Submit
     
    Status
     
    Practice
     
    POJ 2063
    Description
    John never knew he had a grand-uncle, until he received the notary's letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor. 
    John did not need that much money for the moment. But he realized that it would be a good idea to store this capital in a safe place, and have it grow until he decided to retire. The bank convinced him that a certain kind of bond was interesting for him. 
    This kind of bond has a fixed value, and gives a fixed amount of yearly interest, payed to the owner at the end of each year. The bond has no fixed term. Bonds are available in different sizes. The larger ones usually give a better interest. Soon John realized that the optimal set of bonds to buy was not trivial to figure out. Moreover, after a few years his capital would have grown, and the schedule had to be re-evaluated. 
    Assume the following bonds are available: 
    Value    Annual
    interest
    4000
    3000    400
    250
    With a capital of e10 000 one could buy two bonds of $4 000, giving a yearly interest of $800. Buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. After two years the capital has grown to $11 800, and it makes sense to sell a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. This is where this story grows unlikely: the bank does not charge for buying and selling bonds. Next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200. 
    Here is your problem: given an amount to begin with, a number of years, and a set of bonds with their values and interests, find out how big the amount may grow in the given period, using the best schedule for buying and selling bonds.
    Input
    The first line contains a single positive integer N which is the number of test cases. The test cases follow. 
    The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000), and the number of years the capital may grow (at most 40). 
    The following line contains a single number: the number d (1 <= d <= 10) of available bonds. 
    The next d lines each contain the description of a bond. The description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. The value of a bond is always a multiple of $1 000. The interest of a bond is never more than 10% of its value.
    Output
    For each test case, output – on a separate line – the capital at the end of the period, after an optimal schedule of buying and selling.
    Sample Input
    1
    10000 4
    2
    4000 400
    3000 250
    Sample Output
    14050
    View Code

    题解:

    #include<string>
    #include<cstring>
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    const int maxx= 505;
    int n,m;
    int g[maxx][maxx];
    int result[maxx]= {0};
    bool vis[maxx]= {false};
    int ans;
    void inint()
    {
        ans=0;
        memset(g,0,sizeof(g));
        memset(result,0,sizeof(result));
    }
    void floyd()
    {
        for(int k=1; k<=n; k++)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    g[i][j]|=(g[i][k]&g[k][j]);
                }
            }
        }
    }
    bool dfs(int pos)
    {
        for(int i=1; i<=n; i++)
        {
            if(g[pos][i]==1&&!vis[i])
            {
                vis[i]=true;
                if(result[i]==0||dfs(result[i]))
                {
                    result[i]=pos;
                    return true;
                }
            }
        }
        return false;
    }
    void solve()
    {
        for(int i=1; i<=n; i++)
        {
            memset(vis,false,sizeof(vis));
            if(dfs(i)) ans++;
        }
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)&&((n+m)!=0))
        {
            inint();
            for(int i=0; i<m; i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                g[u][v]=1;
            }
            floyd();
            solve();
            //cout<<ans;
            printf("%d
    ",n-ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    #负分小组WEEK1#本周工作小结+下周计划
    #负分小组WEEK1#软件开发之路——需求获取与相关建模
    #负分小组WEEK1#第一次会议纪要
    #负分小组WEEK1#软件开发之路——准备阶段
    #负分小组WEEK1#确定项目——“宝宝睡吧!”儿童睡前服务服务软件+计划分工
    p6spy sql 执行记录
    apache common-lang3 工具类
    根据 ip 定位
    springcloud 与 spring boot 版本对应关系
    PowerDesign 基于mysql 数据库建模
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5747257.html
Copyright © 2011-2022 走看看