1.連接地址node
https://vjudge.net/problem/POJ-1034ios
2.問題描述算法
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:ui
輸入樣例this
4 5 1 4 5 7 5 2 -2 4 -4 -2 3 9 1 2 -1 3 8 -3
輸出樣例spa
6 1 4 3 9 5 7 5 2 1 2 -2 4
3.解題思路.net
我以爲如今最難的就是讀懂題意。。。查了題解才明白題意rest
這道題能夠轉化爲匈牙利算法實現,將獵人能夠走的路弄成一個點集,將狗狗以爲有趣的地方視做另外一個點集,在兩個點集間尋找增廣路徑code
根據狗狗的速度大概是獵人的兩倍,能夠判斷狗狗的點和獵人的路能不能連通blog
4.算法實現源代碼
#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cmath> using namespace std; struct node { int x, y; }; node Bob[105], Dog[105]; int g[105][105]; int vis[105]; int match[105]; int path[105]; int n, m; double dis(int x1, int y1, int x2, int y2) { return sqrt((double)(x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); } int dfs(int x) { for (int i = 0; i < m; i++) { if (g[x][i] && !vis[i]) { vis[i] = 1; if (match[i]==-1 || dfs(match[i])) { match[i] = x; return 1; } } } return 0; } int main() { //freopen("D:\\txt.txt", "r", stdin); while (cin >> n >> m) { for (int i = 0; i < n; i++) cin >> Bob[i].x >> Bob[i].y; for (int i = 0; i < m; i++) cin >> Dog[i].x >> Dog[i].y; memset(g, 0, sizeof(g)); memset(match, -1, sizeof(match)); for (int i = 0; i < n - 1; i++) for (int j = 0; j < m; j++) { double d1 = dis(Bob[i].x, Bob[i].y, Bob[i + 1].x, Bob[i + 1].y); double d2 = dis(Bob[i].x, Bob[i].y, Dog[j].x, Dog[j].y); double d3 = dis(Bob[i + 1].x, Bob[i + 1].y, Dog[j].x, Dog[j].y); if (d2 + d3 - 2 * d1 <= 0) g[i][j] = 1; } int ans = 0; for (int i = 0; i < n; i++) { memset(vis, 0, sizeof(vis)); if(dfs(i)) ans++; } cout << ans + n << endl; memset(path, -1, sizeof(path)); for (int i = 0; i < m; i++) { if (match[i] != -1) path[match[i]] = i; } for (int i = 0; i < n - 1; i++) { cout << Bob[i].x << " " << Bob[i].y << " "; if (path[i] != -1) cout << Dog[path[i]].x << " " << Dog[path[i]].y << " "; } cout << Bob[n - 1].x << " " << Bob[n - 1].y << endl; } return 0; }