poj1034

1.链接地址

https://vjudge.net/problem/POJ-1034

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:

输入样例

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

输出样例

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

3.解题思路

我觉得现在最难的就是读懂题意。。。查了题解才明白题意

这道题可以转化为匈牙利算法实现,将猎人可以走的路弄成一个点集,将狗狗觉得有趣的地方视作另一个点集,在两个点集间寻找增广路径

根据狗狗的速度大概是猎人的两倍,可以判断狗狗的点和猎人的路能不能连通

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;
}

 

转载于:https://www.cnblogs.com/KasenBob/p/11481582.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值