poj 1422 Air Raid (最小路径覆盖)

链接:poj 1422

题意:有n个点和m条有向边,现在要在点上放一些伞兵,伞兵可以沿着图走,

直到不能走为止,每条边有且仅有一个伞兵走过,问最少放多少个伞兵

思路:求的最小路径覆盖,用二分图来做

对于这样的一个有向图做最小路径覆盖,首先建图

然后每一条有向边对应左边的点指向右边的点

这样建好图之后求最大匹配数

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

[cpp] view
plain
copyprint?

  1. #include<stdio.h>
  2. #include<string.h>
  3. int n,edge[150][150],link[150],used[150];
  4. int dfs(int pos)
  5. {
  6. int i;
  7. for(i=1;i<=n;i++)
  8. if(edge[pos][i]&&!used[i]){
  9. used[i]=1;
  10. if(link[i]==-1||dfs(link[i])){
  11. link[i]=pos;
  12. return 1;
  13. }
  14. }
  15. return 0;
  16. }
  17. int main()
  18. {
  19. int T,i,m,a,b,s;
  20. scanf("%d",&T);
  21. while(T--){
  22. scanf("%d%d",&n,&m);
  23. memset(edge,0,sizeof(edge));
  24. for(i=1;i<=m;i++){
  25. scanf("%d%d",&a,&b);
  26. edge[a][b]=1;
  27. }
  28. memset(link,-1,sizeof(link));
  29. s=0;
  30. for(i=1;i<=n;i++){
  31. memset(used,0,sizeof(used));
  32. s+=dfs(i);
  33. }
  34. printf("%d\n",n-s);
  35. }
  36. return 0;
  37. }
时间: 2024-10-24 06:22:24

poj 1422 Air Raid (最小路径覆盖)的相关文章

[POJ] 1422 Air Raid(最小路径覆盖)

题目地址:http://poj.org/problem?id=1422 一个地图上有n个小镇,以及连接着其中两个小镇的有向边,而且这些边无法形成回路.现在选择一些小镇空降士兵(1个小镇最多1个士兵),士兵能沿着边走到尽头,问最少空降几个士兵,能遍历完所有的小镇.最小路径覆盖问题.先拆点,将每个点分为两个点,左边是1到n个点,右边是1到n个点然后每一条有向边对应左边的点指向右边的点 因为最小路径覆盖 = 节点数 - 最大匹配数,所以匈牙利算法求一下二分图最大匹配就能得出结果了. 1 #includ

poj 1422 Air Raid 最少路径覆盖

题目链接:http://poj.org/problem?id=1422 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 t

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

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

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

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)

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

(hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把所有的顶点都覆盖)

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

POJ 1422 二分图(最小路径覆盖)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7278   Accepted: 4318 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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3894    Accepted Submission(s): 2573 Problem Description Consider a town where all the streets are one-way and each street leads from on

hdu 1151 Air Raid 最小路径覆盖

Air Raid Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1151 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

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