hdu1151——Air Raid

网络和图的算法,包含树、有向图、哈密尔顿回路、最短路径以及最大匹配等 该书是M.N.S.SWAMY 和K.THULASIRAMAN的 <Graph, Network and Algorithm>的中译本。 包含树、有向图、哈密尔顿回路、最短路径以及最大匹配等。 立即下载

Air Raid

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3433    Accepted Submission(s): 2265


Problem Description
Consider 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.

With 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.
 

Input
Your 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:

no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets

The 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.

There are no blank lines between consecutive sets of data. Input data are correct.
 

Output
The 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.
 

Sample Input
  
2 4 3 3 4 1 3 2 3 3 3 1 3 1 2 2 3
 

Sample Output
  
2 1
 

Source
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1281  1507  1528  1498  1533




每个伞兵要走一条边,用最少的伞兵走光所有的点,那么显然是最小路径覆盖了, 一条路只可能有一个伞兵有,所以路最少->人最少


#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 222;
struct node 
{
    int next;
    int to;
}edge[2500];

int head[N];
int mark[N];
bool vis[N];
int tot, n, m, k;

void addedge(int from, int to)
{
    edge[tot].to = to;
    edge[tot].next = head[from];
    head[from] = tot++;
}

bool dfs(int u)
{
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (!vis[v])
        {
            vis[v] = 1;
            if (mark[v] == -1 || dfs(mark[v]))
            {
                mark[v] = u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    memset (mark, -1, sizeof(mark) );
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        memset (vis, 0, sizeof(vis) );
        if (dfs(i))
        {
            ans++;
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        memset (head, -1, sizeof(head) );
        tot = 0;
        int u, v;
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &u, &v);
            addedge(u, v);
        }
        printf("%d\n", n - hungary());
    }
    return 0;
}


hdu 1151 最小覆盖路径算法证明 又是二分图。若还不知道匈牙利算法,看我前面的文章 先把每个点拆成两个,一个表示出,一个表示入,根据数据输入,对应的出点和对应入点之间构造了一条边。这样就有了一个二分图。 有向图的最小路径覆盖 = 总的点数(为拆分前的) - 最大匹配数。 证明如下: 先假设所有点我们都派出一个伞兵。然后每增加一条匹配,就代表我们从一个点出走向一个点。并且根据最大匹配的定义,我们可以直接看作是将这两个点合并到 阅读详情

相关推荐

【Hopcroft-Karp && 二分图的最大匹配数 && 有向图】HDU - 2389 Rain on your Parade

Problem Description 给你时间Time。给你n个人,每个人给你所在位置和移动速度。给你m个雨伞的坐标。问你在Time时间内,最多有多少个人能得到雨伞。 思路: 暴力n*m求人与雨伞之间的距离,判断人在时间Time内能否到达雨伞处,如果能够到达建一条边。 然后就是最二分图的最大匹配,左边是人,右边是雨伞。 匈牙利算法超时。 得换一个快一点的算法Hopcrof

ACMer 梁剑锋 的博客 538

poj 1422 二分图最小路径覆盖

二分图最小路径覆盖==点数n--二分图最大匹配,,,知道了这个公式,,又是一道水题。。。二分图方面的题,,关键是建图,把图建好了,剩下的就简单了。。。题目: Air Raid Time Limit:1000MS Memory Limit:10000K Total Submissions:4600 Accepted:2722 D...

hunxiejun 276

hdu 1151 最小路径覆盖数 = n - 最大匹配数

题意:n个点m条边的有向无环图,每个点走一次,问最小沿着图走几次可以遍历每个点。 题解:最小路径覆盖数 1.有向图求最大匹配数,最小路径覆盖数 = n - 最大匹配数。 2.套用匈牙利算法的模板即可。 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;stdio.h&gt; #include&lt;math.h&gt...

Irving0323的博客 355

图论核心问题——最小覆盖、最大匹配、最大流最小割

摘要:本文系统介绍了图论中的最小覆盖、最大匹配及最大流最小割三大核心问题。在二分图部分,详细讲解了匈牙利算法和König定理,通过相亲大会的生动例子阐释了最大匹配与最小覆盖的对偶关系及其在任务分配、监控布置等场景的应用。在网络流部分,分析了Ford-Fulkerson、Edmonds-Karp和Dinic算法,结合水管网络的比喻说明最大流最小割定理,并提供了Python实现代码。最后引入对偶图概念,展示了其在空间划分问题中的独特价值。全文通过生活化案例、算法对比和可视化实现,将复杂的图论问题转化为直观可操作

zkl_zkl_的博客 1508

hdu1151Air Raid——最小路径覆盖

hdu1151Air Raid:http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4289    Accepted

hahaAll的博客 451

hdu 1151 Air Raid

http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Problem Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is a

大太阳 701

hdu1151air raid

Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4907 Accepted Submission(s): 3296Problem Description Consider a town where all th

wo的只属于我 321

HDU1151 Air Raid

题目链接:https://vjudge.net/problem/HDU-1151 题目大意:   这个问题本质上就是有向图的最小不相交路径覆盖问题,在此推荐一篇博客。 知识点:  最小路径覆盖 解题思路:   把原图的每个点 \(V\) 拆成\(V_x\)和\(V_y\)两个点,如果有一条有向边\(A \rightarrow B\),那么就加边\(A_x \rightarrow...

dielie6462的博客 160

Air Raid HDU - 1151(最小路径覆盖)

Air Raid  HDU - 1151 Consider 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...

nucleare的博客 258

HDU 1151 Air Raid

目录知识点:二分匹配、贪心、思维题目输入输出样例输入输出题意思路代码 知识点:二分匹配、贪心、思维 题目 Consider 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 ne

夏午Sharve的博客? 281

hdu1151 air Raid(最小路径覆盖)

//hdu 1151 //访问小镇最多需要几个士兵,每个士兵的路无交点,实际上就是求有向无环图的最小路径覆盖 //最小路径覆盖(不要求二分图):在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联 //最小路径覆盖 = 顶点数 - 最大匹配数 //对于有向无环图,首先拆点,建成二分图再进行求解 #include #include #include using name

野生的TFgirl 401

hdu1151Air Raid

Air Raid Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2704Accepted Submission(s): 1762 Problem Description Consider a...

weixin_30505043的博客 189

hdu1151 Air Raid(最小边覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1151 题意:给你n个城镇和m条边,边是单向的且图中没有环。选择一些小兵空降在城镇上,小兵可以沿着单向边走到路的尽头,问最少需要多少个小兵可以遍历所有的顶点。 思路:最小边覆盖,用尽量少的不相交简单路径覆盖有向无环图(DAG)G的所有顶点。依旧是定理[金馆长哭],怎么这几道题都是定理啊,想吐槽。

Iguodala的博客 432

HDU 1151解题报告

Air Raid                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                                      

jhljx的专栏 559

HDU [P1151] Air Raid

二分图匹配求DAG图上的最小路径覆盖 应用了拆点的思想,将DAG图上的每一个点拆成二分图的x集合与y集合,对于一条有向边u->v来说,我们在ux与vy之间连一条边,然后求二分图的最大匹配 DAG图上的最小路径覆盖数=DAG图上的顶点数-二分图最大匹配数.这是路径不能重合的情况,对于路径可以重合的来说,用传递闭包做 #include <iostream> #include...

165

Air Raid HDU 1151

题意 给定n个路口 加上一些单向路 求遍历完所有路口需要多少人 人是空降的 哪里都可以作为起点 求 最小边覆盖 (用最少的边覆盖所有的点) 也叫最小路径覆盖(更形象) 也叫二分图的最大独立集合 最小边覆盖==所有点-最大匹配数(匈牙利算法) 理解: 如果没有路 则要n个人 多一条路 则可以减一个人 . #include<bits/stdc++.h> us...

weixin_30915275的博客 109
上一篇: hdu1150——Machine Schedule
下一篇: hdu1179——Ollivanders: Makers of Fine Wands since 382 BC.
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值