匈牙利算法 hdu 1151有向图的最小路径覆盖=顶点数-最大匹配数
Air Raid
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.
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.
2 4 3 3 4 1 3 2 3 3 3 1 3 1 2 2 3
2 1
//考察知识点:匈牙利算法模板 //有向图无环图的最小路径覆盖=顶点数-最大匹配数 #include<stdio.h> #include<string.h> int girl[125],used[125]; int line[125][125]; int n,m; int find(int x) { int j; for(j=1;j<=n;++j) { if(!used[j]&&line[x][j]) { used[j]=1; if(!girl[j]||find(girl[j])) { girl[j]=x; return 1; } } } return 0; } int main() { int t; scanf("%d",&t); while(t--) { int a,b; memset(used,0,sizeof(used)); memset(line,0,sizeof(line)); memset(girl,0,sizeof(girl)); scanf("%d%d",&n,&m); for(int i=1;i<=m;++i) { scanf("%d%d",&a,&b); line[a][b]=1; } int count=0; for(int i=1;i<=n;++i) { memset(used,0,sizeof(used)); if(find(i)) count++; } printf("%d\n",n-count); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。