Air Raid
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4974 Accepted Submission(s): 3347

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

最小路径覆盖=点数-最大匹配

最大匹配可以用匈牙利算法来算,也能用最大流来算。

#include <stdio.h>
#include <string.h>
// ALGORITHM_MAXFLOW_SAP -> #define ALGORITHM_MAXFLOW_SAP_MAXN 20010
#define ALGORITHM_MAXFLOW_SAP_MAXM 880010
#define ALGORITHM_MAXFLOW_SAP_INF 0x7FFFFFFF struct ALGORITHM_MAXFLOW_SAP_Node {
int from, to, next;
int cap;
} ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_MAXM];
int ALGORITHM_MAXFLOW_SAP_tol;
int ALGORITHM_MAXFLOW_SAP_head[ALGORITHM_MAXFLOW_SAP_MAXN];
int ALGORITHM_MAXFLOW_SAP_dep[ALGORITHM_MAXFLOW_SAP_MAXN];
int ALGORITHM_MAXFLOW_SAP_gap[ALGORITHM_MAXFLOW_SAP_MAXN];
int ALGORITHM_MAXFLOW_SAP_cur[ALGORITHM_MAXFLOW_SAP_MAXN];
int ALGORITHM_MAXFLOW_SAP_S[ALGORITHM_MAXFLOW_SAP_MAXN];
int ALGORITHM_MAXFLOW_SAP_que[ALGORITHM_MAXFLOW_SAP_MAXN];
int ALGORITHM_MAXFLOW_SAP_n; void ALGORITHM_MAXFLOW_SAP_clear() {
ALGORITHM_MAXFLOW_SAP_tol = ;
memset(ALGORITHM_MAXFLOW_SAP_head, -, sizeof(ALGORITHM_MAXFLOW_SAP_head));
} void ALGORITHM_MAXFLOW_SAP_addedge(int u, int v, int w) {
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].from = u;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].to = v;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].cap = w;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].next = ALGORITHM_MAXFLOW_SAP_head[u];
ALGORITHM_MAXFLOW_SAP_head[u] = ALGORITHM_MAXFLOW_SAP_tol++;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].from = v;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].to = u;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].cap = ;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_tol].next = ALGORITHM_MAXFLOW_SAP_head[v];
ALGORITHM_MAXFLOW_SAP_head[v] = ALGORITHM_MAXFLOW_SAP_tol++;
}
void ALGORITHM_MAXFLOW_SAP_BFS(int start, int end) {
memset(ALGORITHM_MAXFLOW_SAP_dep, -, sizeof(ALGORITHM_MAXFLOW_SAP_dep));
memset(ALGORITHM_MAXFLOW_SAP_gap, , sizeof(ALGORITHM_MAXFLOW_SAP_gap));
ALGORITHM_MAXFLOW_SAP_gap[] = ;
int front, rear;
front = rear = ;
ALGORITHM_MAXFLOW_SAP_dep[end] = ;
ALGORITHM_MAXFLOW_SAP_que[rear++] = end;
while(front != rear) {
int u = ALGORITHM_MAXFLOW_SAP_que[front++];
if(front == ALGORITHM_MAXFLOW_SAP_MAXN) {
front = ;
}
for(int i = ALGORITHM_MAXFLOW_SAP_head[u]; i != -; i = ALGORITHM_MAXFLOW_SAP_edge[i].next) {
int v = ALGORITHM_MAXFLOW_SAP_edge[i].to;
if(ALGORITHM_MAXFLOW_SAP_dep[v] != -) {
continue;
}
ALGORITHM_MAXFLOW_SAP_que[rear++] = v;
if(rear == ALGORITHM_MAXFLOW_SAP_MAXN) {
rear = ;
}
ALGORITHM_MAXFLOW_SAP_dep[v] = ALGORITHM_MAXFLOW_SAP_dep[u] + ;
++ALGORITHM_MAXFLOW_SAP_gap[ALGORITHM_MAXFLOW_SAP_dep[v]];
}
}
}
int ALGORITHM_MAXFLOW_SAP_SAP(int start, int end) {
int res = ;
ALGORITHM_MAXFLOW_SAP_BFS(start, end);
int top = ;
memcpy(ALGORITHM_MAXFLOW_SAP_cur, ALGORITHM_MAXFLOW_SAP_head, sizeof(ALGORITHM_MAXFLOW_SAP_head));
int u = start;
int i;
while(ALGORITHM_MAXFLOW_SAP_dep[start] < ALGORITHM_MAXFLOW_SAP_n) {
if(u == end) {
int temp = ALGORITHM_MAXFLOW_SAP_INF;
int inser;
for(i = ; i < top; i++)
if(temp > ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[i]].cap) {
temp = ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[i]].cap;
inser = i;
}
for(i = ; i < top; i++) {
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[i]].cap -= temp;
ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[i] ^ ].cap += temp;
}
res += temp;
top = inser;
u = ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[top]].from;
}
if(u != end && ALGORITHM_MAXFLOW_SAP_gap[ALGORITHM_MAXFLOW_SAP_dep[u] - ] == ) {
break;
}
for(i = ALGORITHM_MAXFLOW_SAP_cur[u]; i != -; i = ALGORITHM_MAXFLOW_SAP_edge[i].next)
if(ALGORITHM_MAXFLOW_SAP_edge[i].cap != && ALGORITHM_MAXFLOW_SAP_dep[u] == ALGORITHM_MAXFLOW_SAP_dep[ALGORITHM_MAXFLOW_SAP_edge[i].to] + ) {
break;
}
if(i != -) {
ALGORITHM_MAXFLOW_SAP_cur[u] = i;
ALGORITHM_MAXFLOW_SAP_S[top++] = i;
u = ALGORITHM_MAXFLOW_SAP_edge[i].to;
} else {
int min = ALGORITHM_MAXFLOW_SAP_n;
for(i = ALGORITHM_MAXFLOW_SAP_head[u]; i != -; i = ALGORITHM_MAXFLOW_SAP_edge[i].next) {
if(ALGORITHM_MAXFLOW_SAP_edge[i].cap == ) {
continue;
}
if(min > ALGORITHM_MAXFLOW_SAP_dep[ALGORITHM_MAXFLOW_SAP_edge[i].to]) {
min = ALGORITHM_MAXFLOW_SAP_dep[ALGORITHM_MAXFLOW_SAP_edge[i].to];
ALGORITHM_MAXFLOW_SAP_cur[u] = i;
}
}
--ALGORITHM_MAXFLOW_SAP_gap[ALGORITHM_MAXFLOW_SAP_dep[u]];
ALGORITHM_MAXFLOW_SAP_dep[u] = min + ;
++ALGORITHM_MAXFLOW_SAP_gap[ALGORITHM_MAXFLOW_SAP_dep[u]];
if(u != start) {
u = ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[--top]].from;
}
}
}
return res;
} // <- ALGORITHM_MAXFLOW_SAP
int main() {
int T;
scanf("%d", &T);
while(T--) {
ALGORITHM_MAXFLOW_SAP_clear();
int n, m, a, b;
scanf("%d%d", &n, &m);
ALGORITHM_MAXFLOW_SAP_n = + * n;
for(int i = ; i <= n + ; i++) {
ALGORITHM_MAXFLOW_SAP_addedge(, i, );;
}
for(int i = n + ; i <= * n + ; i++) {
ALGORITHM_MAXFLOW_SAP_addedge(i, * n + , );
}
for(int i = ; i < m; i++) {
scanf("%d%d", &a, &b);
ALGORITHM_MAXFLOW_SAP_addedge(a + , b + n + , );
}
int x = ALGORITHM_MAXFLOW_SAP_SAP(, * n + );
printf("%d\n", n - x);
}
return ;
}

Air Raid[HDU1151]的更多相关文章

  1. HDU1151 Air Raid —— 最小路径覆盖

    题目链接:https://vjudge.net/problem/HDU-1151 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  2. hdu1151 二分图(无回路有向图)的最小路径覆盖 Air Raid

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  3. Hdu1151 Air Raid(最小覆盖路径)

    Air Raid Problem Description Consider a town where all the streets are one-way and each street leads ...

  4. HDU1151:Air Raid(最小边覆盖)

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

  5. 【网络流24题----03】Air Raid最小路径覆盖

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

  6. hdu-----(1151)Air Raid(最小覆盖路径)

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

  7. hdu 1151 Air Raid(二分图最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS   Memory Limit: 10000K To ...

  8. HDOJ 1151 Air Raid

    最小点覆盖 Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. Air Raid(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 Descript ...

随机推荐

  1. 忘记Mysql登录密码

    1,使用安全模式跳过验证: 如果 Mysql在运行,Kill掉. 如果mysqld_safe无法启动,可用管理员权限sudo . 2,本地登录: 启动Mysql 3,修改密码: 5.7之后, 更改密码 ...

  2. bootstrap垂直下拉菜单默认展开

    HTML: <div class="col-md-3"> <nav class="navbar"> <div class=&quo ...

  3. string int 转换

    int转stringint n = 0;std::stringstream ss;std::string str;ss<<n;ss>>str;string转intstd::st ...

  4. Python 实现发送、抄送邮件功能

    发送邮件 问题 在web.py中,如何发送邮件? 解法 在web.py中使用web.sendmail()发送邮件. web.sendmail('cookbook@webpy.org', 'user@e ...

  5. ios 拨打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  6. EF – 1.模式

      3种数据库 code first model first database first       创建EF http://www.cnblogs.com/tangge/p/3834578.htm ...

  7. ListView优化中ViewHolder要不要定义为static静态内部类?

    给学生讲课的时候,发现存在这个问题,下来百度了下,发现很纠结,涉及到了内部类对外部类的引用,静态类的生命周期等java知识,现总结如下: static class ViewHolder { //定义l ...

  8. Delphi中线程类TThread实现多线程编程2---事件、临界区、Synchronize、WaitFor……

    接着上文介绍TThread. 现在开始说明 Synchronize和WaitFor 但是在介绍这两个函数之前,需要先介绍另外两个线程同步技术:事件和临界区 事件(Event) 事件(Event)与De ...

  9. 无废话ExtJs 入门教程十五[员工信息表Demo:AddUser]

    无废话ExtJs 入门教程十五[员工信息表Demo:AddUser] extjs技术交流,欢迎加群(201926085) 前面我们共介绍过10种表单组件,这些组件是我们在开发过程中最经常用到的,所以一 ...

  10. golang基础知识之encoding/json package

    golang基础知识之json 简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.可以去json.org 查看json标准的清晰定义.json pack ...