poj 1034 The dog task (二分匹配)

The dog task
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2559   Accepted: 1038   Special Judge

Description

Hunter Bob often walks with his dog Ralph. Bob walks with a constant speed and his route is a polygonal line (possibly self-intersecting) whose vertices are specified by N pairs of integers (Xi, Yi) ? their Cartesian coordinates. 
Ralph walks on his own way but always meets his master at the specified N points. The dog starts his journey simultaneously with Bob at the point (X1, Y1) and finishes it also simultaneously with Bob at the point (XN, YN). 
Ralph can travel at a speed that is up to two times greater than his master's speed. While Bob travels in a straight line from one point to another the cheerful dog seeks trees, bushes, hummocks and all other kinds of interesting places of the local landscape which are specified by M pairs of integers (Xj',Yj'). However, after leaving his master at the point (Xi, Yi) (where 1 <= i < N) the dog visits at most one interesting place before meeting his master again at the point (Xi+1, Yi+1). 
Your task is to find the dog's route, which meets the above requirements and allows him to visit the maximal possible number of interesting places. The answer should be presented as a polygonal line that represents Ralph's route. The vertices of this route should be all points (Xi, Yi) and the maximal number of interesting places (Xj',Yj'). The latter should be visited (i.e. listed in the route description) at most once. 
An example of Bob's route (solid line), a set of interesting places (dots) and one of the best Ralph's routes (dotted line) are presented in the following picture: 

Input

The first line of the input contains two integers N and M, separated by a space ( 2 <= N <= 100 ,0 <= M <=100 ). The second line contains N pairs of integers X1, Y1, ..., XN, YN, separated by spaces, that represent Bob's route. The third line contains M pairs of integers X1',Y1',...,XM',YM', separated by spaces, that represent interesting places. 
All points in the input file are different and their coordinates are integers not greater than 1000 by the absolute value. 

Output

The first line of the output should contain the single integer K ? the number of vertices of the best dog's route. The second line should contain K pairs of coordinates X1'',Y1'' , ...,Xk'',Yk'', separated by spaces, that represent this route. If there are several such routes, then you may write any of them.

Sample Input

4 5
1 4 5 7 5 2 -2 4
-4 -2 3 9 1 2 -1 3 8 -3

Sample Output

6
1 4 3 9 5 7 5 2 1 2 -2 4

Source

 1 //204K    32MS    C++    1612B    2013-11-11 09:07:25 
 2 /*
 3     
 4     题意:
 5         有n个点,Bob要按序逐个走过,它的狗的速度最大时他的两倍,每次在两点间他的狗都可以
 6     去浏览一个有趣的点并回到Bob身边,问他的狗最多可以浏览多少个有趣的点,并输出狗经过的点 
 7     
 8     二分匹配:
 9         Bob要走的点看成是二分中的一个集合,有趣的点看成是二分的另一集合,很明显构图时要符合
10     有趣点到一条Bob必经路径两点的距离和 小于等于 2*该路径长度时,路径和该有趣的点联通,构好
11     图后直接匈牙利模板 
12      
13 */
14 #include<stdio.h>
15 #include<string.h>
16 #include<math.h>
17 struct node{
18     double x,y;
19 }p[105],q[105];
20 int g[105][105];
21 int match[105];
22 int vis[105];
23 int n,m;
24 double L(node a,node b) //求两点距离 
25 {
26     return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x));
27 }
28 void build()
29 {
30     memset(g,0,sizeof(g));
31     for(int i=0;i<n-1;i++){
32         double l=L(p[i],p[i+1]); 
33         for(int j=0;j<m;j++)
34             if(L(p[i],q[j])+L(p[i+1],q[j])<=2*l) //条件 
35                 g[i][j]=1;
36     }
37 }
38 int dfs(int x)
39 {
40     for(int i=0;i<m;i++){
41         if(!vis[i] && g[x][i]){
42             vis[i]=1;
43             if(match[i]==-1 || dfs(match[i])){
44                 match[i]=x;
45                 return 1;
46             }
47         }
48     }
49     return 0;
50 }
51 void hungary()
52 {
53     memset(match,-1,sizeof(match));
54     int ret=0;
55     for(int i=0;i<n-1;i++){
56         memset(vis,0,sizeof(vis));
57         if(dfs(i)) ret++;
58     }
59     printf("%d
",ret+n);
60     int flag[105][2]={0}; //第二维保存的是必经点中哪个点后有有趣的点和该有趣的点的下标 
61     for(int i=0;i<m;i++)
62         if(match[i]!=-1){
63             flag[match[i]][0]=1;
64             flag[match[i]][1]=i;
65         }
66     for(int i=0;i<n-1;i++){ //输出 
67         printf("%.0lf %.0lf ",p[i].x,p[i].y);
68         if(flag[i][0]){
69             printf("%.0lf %.0lf ",q[flag[i][1]].x,q[flag[i][1]].y);
70         }
71     }
72     printf("%.0lf %.0lf
",p[n-1].x,p[n-1].y);
73 }
74 int main(void)
75 {
76     while(scanf("%d%d",&n,&m)!=EOF)
77     {
78         for(int i=0;i<n;i++)
79             scanf("%lf%lf",&p[i].x,&p[i].y);
80         for(int i=0;i<m;i++)
81             scanf("%lf%lf",&q[i].x,&q[i].y);
82         build();
83         hungary();
84     }
85     return 0;
86 }
原文地址:https://www.cnblogs.com/GO-NO-1/p/3417417.html