天天写算法之Air Raid

这个题目能用匈牙利的原因是,没有环。
这个题目,我就一个纳闷的地方,因为做过的上上道题,是男女孩问题,那个时候我们把最大匹配数除以了2

而这个题目,思路一样,但是最终并没有进行/2操作。
当时对男孩女孩的理解是,没有办法区分出谁是男孩,谁是女孩,所以就克隆出来了一倍的人,最终就进行了除以2的操作

而这个题目,是路口和路口之间的连接问题,这个地方有点想不明白,求告知。Orz
代码:
#include<iostream>
#include<string.h>
#include<cstdio>
#define MAX 155
using namespace std;

int n ,m , k;
int Map[MAX][MAX];//两个人是否有关系
bool used[MAX];//是否被访问过
int linker[MAX];//查看被连接的那一个集合是否有男生来连接了。

bool dfs(int a){
    for(int j = 1 ; j <= n ; j ++)
    {
        if(!used[j]&&Map[a][j])
        {
            used[j]=1 ;
            if(linker[j]==-1||dfs(linker[j]))
            {
                linker[j]=a;
                return true ;
            }
        }
    }
    return false ;
}

int hungary(){
    int ans = 0 ;
    memset(linker,-1,sizeof(linker));
    for(int i = 1 ; i <=n ; i ++)
    {
        memset(used,0,sizeof(used));
        if(dfs(i)) ans+=1;
    }
    return ans ;
}
int main(){
    int i , j ,num,a,b;
    scanf("%d",&num);
    while(num--)
    {
        scanf("%d%d",&n,&m);
        memset(Map,0,sizeof(Map));
        for(int i = 0 ; i < m; i ++)
        {
            scanf("%d%d",&a,&b);
            Map[a][b]=1 ;
        }
        printf("%d\n",n-hungary());

    }
}

阅读更多

Air Raid

10-08

DescriptionnnConsider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's streets form no cycles. nnWith these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper. nInputnnYour program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format: nnno_of_intersections nno_of_streets nS1 E1 nS2 E2 n...... nSno_of_streets Eno_of_streets nnThe first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections. nnThere are no blank lines between consecutive sets of data. Input data are correct. nOutputnnThe result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town. nSample Inputnn2n4n3n3 4n1 3n2 3n3n3n1 3n1 2n2 3nSample Outputnn2n1

没有更多推荐了,返回首页