Air Raid[HDU1151]

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 = 0;
    memset(ALGORITHM_MAXFLOW_SAP_head, -1, 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 = 0;
    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, -1, sizeof(ALGORITHM_MAXFLOW_SAP_dep));
    memset(ALGORITHM_MAXFLOW_SAP_gap, 0, sizeof(ALGORITHM_MAXFLOW_SAP_gap));
    ALGORITHM_MAXFLOW_SAP_gap[0] = 1;
    int front, rear;
    front = rear = 0;
    ALGORITHM_MAXFLOW_SAP_dep[end] = 0;
    ALGORITHM_MAXFLOW_SAP_que[rear++] = end;
    while(front != rear) {
        int u = ALGORITHM_MAXFLOW_SAP_que[front++];
        if(front == ALGORITHM_MAXFLOW_SAP_MAXN) {
            front = 0;
        }
        for(int i = ALGORITHM_MAXFLOW_SAP_head[u]; i != -1; i = ALGORITHM_MAXFLOW_SAP_edge[i].next) {
            int v = ALGORITHM_MAXFLOW_SAP_edge[i].to;
            if(ALGORITHM_MAXFLOW_SAP_dep[v] != -1) {
                continue;
            }
            ALGORITHM_MAXFLOW_SAP_que[rear++] = v;
            if(rear == ALGORITHM_MAXFLOW_SAP_MAXN) {
                rear = 0;
            }
            ALGORITHM_MAXFLOW_SAP_dep[v] = ALGORITHM_MAXFLOW_SAP_dep[u] + 1;
            ++ALGORITHM_MAXFLOW_SAP_gap[ALGORITHM_MAXFLOW_SAP_dep[v]];
        }
    }
}
int ALGORITHM_MAXFLOW_SAP_SAP(int start, int end) {
    int res = 0;
    ALGORITHM_MAXFLOW_SAP_BFS(start, end);
    int top = 0;
    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 = 0; 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 = 0; i < top; i++) {
                ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[i]].cap -= temp;
                ALGORITHM_MAXFLOW_SAP_edge[ALGORITHM_MAXFLOW_SAP_S[i] ^ 1].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] - 1] == 0) {
            break;
        }
        for(i = ALGORITHM_MAXFLOW_SAP_cur[u]; i != -1; i = ALGORITHM_MAXFLOW_SAP_edge[i].next)
            if(ALGORITHM_MAXFLOW_SAP_edge[i].cap != 0 && ALGORITHM_MAXFLOW_SAP_dep[u] == ALGORITHM_MAXFLOW_SAP_dep[ALGORITHM_MAXFLOW_SAP_edge[i].to] + 1) {
                break;
            }
        if(i != -1) {
            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 != -1; i = ALGORITHM_MAXFLOW_SAP_edge[i].next) {
                if(ALGORITHM_MAXFLOW_SAP_edge[i].cap == 0) {
                    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 + 1;
            ++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 = 2 + 2 * n;
        for(int i = 2; i <= n + 1; i++) {
            ALGORITHM_MAXFLOW_SAP_addedge(1, i, 1);;
        }
        for(int i = n + 2; i <= 2 * n + 1; i++) {
            ALGORITHM_MAXFLOW_SAP_addedge(i, 2 * n + 2, 1);
        }
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &a, &b);
            ALGORITHM_MAXFLOW_SAP_addedge(a + 1, b + n + 1, 1000000);
        }
        int x = ALGORITHM_MAXFLOW_SAP_SAP(1, 2 * n + 2);
        printf("%d\n", n - x);
    }
    return 0;
}

时间: 2024-10-12 23:46:24

Air Raid[HDU1151]的相关文章

【二分图匹配入门专题1】E - Air Raid hdu1151【最小路径覆盖】

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

hdu1151 Air Raid --- 最小路径覆盖

给一个DAG图,一个人可以走一条路,或者就在一个点(路径长度为0),问至少需要多少人可以覆盖所有点. 根据二分图的性质: DAG的最小路径覆盖,将每个点拆点后求最大匹配数m,结果为n-m,求具体路径的时候顺着匹配边走就可以,匹配边i→j',j→k',k→l'....构成一条有向路径. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algori

HDU1151:Air Raid(最小边覆盖)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6528    Accepted Submission(s): 4330 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 Description: Consider a town where all the stree

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

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3378    Accepted Submission(s): 2223 Problem Description Consider a town where all the streets are one-way and each street leads from one

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 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 i

POJ1422 Air Raid 【DAG最小路径覆盖】

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6763   Accepted: 4034 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 i

HDU 1151 Air Raid(最小路径覆盖 = 顶点数 - 最大匹配数)

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 also known that starting from an intersection and walking through town's streets you can never reach the same

poj 1422 Air Raid (二分匹配)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6520   Accepted: 3877 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 i

Air Raid(最小路径覆盖)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7511   Accepted: 4471 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 i