The dog task

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3272   Accepted: 1313   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、注意最重要的一个条件:狗每次最多去一个interesting place;
2、一处英文错误,狗的速度是人的两倍;
 
解题:典型的二分图最大匹配问题,用匈牙利算法。
 
  1 Source Code
  2 Problem: 1034
  3 Memory: 416K        Time: 16MS
  4 Language: GCC        Result: Accepted
  5 
  6   7 
  8     #include <stdio.h>
  9     #include <math.h>
 10     #include <string.h>
 11 
 12     #define DOG_SPEED 2
 13 
 14     #define MAX_POINT_NUM 101
 15 
 16     #define TRUE  (int)1
 17     #define FALSE (int)0
 18 
 19     typedef int BOOL;
 20 
 21     typedef struct
 22     {
 23         int x;
 24         int y;
 25     }Point;
 26 
 27     typedef struct
 28     {
 29         int num;
 30         Point pos[MAX_POINT_NUM];
 31     }Points;
 32 
 33     Points g_Bob;
 34     Points g_interests;
 35     BOOL g_isOccupied[MAX_POINT_NUM];
 36     int g_len[MAX_POINT_NUM][MAX_POINT_NUM];
 37     int g_selectNum;
 38     int g_selectIdx[MAX_POINT_NUM];
 39     int g_BobToInterest[MAX_POINT_NUM];
 40 
 41     void Input()
 42     {
 43         int i;
 44 
 45         scanf("%d %d", &g_Bob.num, &g_interests.num);
 46 
 47         for(i = 0; i < g_Bob.num; i++)
 48         {
 49             scanf("%d %d", &g_Bob.pos[i].x, &g_Bob.pos[i].y);
 50         }
 51 
 52         for(i = 0; i < g_interests.num; i++)
 53         {
 54             scanf("%d %d", &g_interests.pos[i].x, &g_interests.pos[i].y);
 55         }
 56 
 57         g_selectNum = 0;
 58         memset(g_len, -1, sizeof(g_len));
 59         memset(g_selectIdx, -1, sizeof(g_selectIdx));
 60         memset(g_BobToInterest, -1, sizeof(g_BobToInterest));
 61     }
 62 
 63     void Output()
 64     {
 65         int bobIdx, interestIdx;
 66 
 67         printf("%d\n", g_Bob.num+g_selectNum);
 68 
 69         for(bobIdx = 0; bobIdx < g_Bob.num; bobIdx++)
 70         {
 71             printf("%d %d ", g_Bob.pos[bobIdx].x, g_Bob.pos[bobIdx].y);
 72             interestIdx = g_BobToInterest[bobIdx];
 73             if(interestIdx != -1) printf("%d %d ", g_interests.pos[interestIdx].x, g_interests.pos[interestIdx].y);
 74         }
 75     }
 76 
 77     static double CalcLen(Point* m, Point* n)
 78     {
 79         double x = m->x - n->x;
 80         double y = m->y - n->y;
 81 
 82         return sqrt(x*x+y*y);
 83     }
 84 
 85     int IsLenSatisfied(int bobIdx, int interestIdx)
 86     {
 87         double bobLen, dogLen1, dogLen2;
 88 
 89         if(g_len[bobIdx][interestIdx] == -1)
 90         {
 91             bobLen = CalcLen(&g_Bob.pos[bobIdx], &g_Bob.pos[bobIdx+1]);
 92             dogLen1 = CalcLen(&g_Bob.pos[bobIdx], &g_interests.pos[interestIdx]);
 93             dogLen2 = CalcLen(&g_Bob.pos[bobIdx+1], &g_interests.pos[interestIdx]);
 94             g_len[bobIdx][interestIdx] = ((bobLen*DOG_SPEED) >= (dogLen1+dogLen2)) ? 1 : 0;
 95         }
 96         return g_len[bobIdx][interestIdx];
 97     }
 98 
 99     BOOL DogFinding(int bobIdx)
100     {
101         int interestIdx;
102 
103         for(interestIdx = 0; interestIdx < g_interests.num; interestIdx++)
104         {
105             if(!g_isOccupied[interestIdx] && IsLenSatisfied(bobIdx, interestIdx))
106             {
107                 g_isOccupied[interestIdx] = TRUE;
108                 if(g_selectIdx[interestIdx] == -1 || DogFinding(g_selectIdx[interestIdx]))
109                 {
110                     g_selectIdx[interestIdx] = bobIdx;
111                     g_BobToInterest[bobIdx] = interestIdx;
112                     return TRUE;
113                 }
114             }
115         }
116 
117         return FALSE;
118     }
119 
120     void Proc()
121     {
122         int bobIdx;
123         for(bobIdx = 0; bobIdx < g_Bob.num-1; bobIdx++)
124         {
125             memset(g_isOccupied, 0, sizeof(g_isOccupied));
126             if(DogFinding(bobIdx)) g_selectNum++;
127         }
128     }
129 
130     int main()
131     {
132         Input();
133         Proc();
134         Output();
135         return 0;
136     }