| The dog task | 
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 (X'j,Y'j). However, after leaving his 
master at the point (Xi , Yi) (where 
)
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 (
X'j, Y'jX). 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 is an integer L, then a blank line followed by L datasets. There is a blank line between datasets.
The first line of each dataset contains two integers N and M, separated
by a space (
). The second
line contains N pairs of integers 
,
separated by
spaces, that represent Bob's route. The third line contains M pairs of
integers 
,
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 each dataset 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 
,
separated by spaces, that 
represent this route. If there are
several such routes, then you may write any of them.
Print a blank line between datasets.
Sample Input
1 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
Miguel Revilla
2000-05-22
#include <iostream> #include <vector> #include <cmath> #define EPS 1e-10 using namespace std; struct Coordinate{ int x; int y; Coordinate(int a = 0, int b = 0){ x = a; y = b; } double distance(Coordinate c){return sqrt((x - c.x) * (x - c.x) + (y - c.y) * (y - c.y));} }; ostream &operator <<(ostream &os, const Coordinate &c){os << c.x << ' ' << c.y;} class TheDogTask{ private: vector<Coordinate> bob; vector<Coordinate> place; vector<vector<int> > dog; vector<int> link; vector<bool> visit; int answer; bool findPath(int); public: void initialize(); void readCase(); void computing(); void outResult(int); }; void TheDogTask::initialize(){ bob.clear(); place.clear(); dog.clear(); link.clear(); } void TheDogTask::readCase(){ int n, m, a, b; cin >> n >> m; answer = n; while(n--){ cin >> a >> b; bob.push_back(Coordinate(a, b)); } while(m--){ cin >> a >> b; place.push_back(Coordinate(a, b)); link.push_back(-1); } } bool TheDogTask::findPath(int x){ for(int i = 0; i < dog[x].size(); i++){ if(!visit[dog[x][i]]){ visit[dog[x][i]] = true; if(link[dog[x][i]] == -1 || findPath(link[dog[x][i]])){ link[dog[x][i]] = x; return true; } } } return false; } void TheDogTask::computing(){ vector<int> route; for(int i = 0; i < bob.size() - 1; i++){ route.clear(); for(int j = 0; j < place.size(); j++){ if(bob[i].distance(place[j]) + bob[i + 1].distance(place[j]) - 2 * bob[i].distance(bob[i + 1]) <= EPS){ route.push_back(j); } } dog.push_back(route); } for(int i = 0; i < bob.size() - 1; i++){ visit.clear(); visit.resize(place.size()); if(findPath(i)){answer++;} } } void TheDogTask::outResult(int l){ cout << answer << endl; for(int i = 0; i < bob.size() - 1; i++){ cout << bob[i] << ' '; for(int j = 0; j < link.size(); j++){ if(link[j] == i){cout << place[j] << ' ';} } } cout << bob[bob.size() - 1] << endl; if(l){cout << endl;} } int main(){ TheDogTask tdt; int l; cin >> l; while(l--){ tdt.initialize(); tdt.readCase(); tdt.computing(); tdt.outResult(l); } return 0; }
                    
                
                
            
        
浙公网安备 33010602011771号