poj 1034 The dog task

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2753 Accepted: 1103 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


将人的路线缩边为点,与可达景点连边建图,求最大匹配即可。



#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
#define maxn 111
int xm[maxn],ym[maxn],vis[maxn];
int per[maxn][2],dog[maxn][2];
int G[maxn][maxn];
int n,m;

bool findpath(int x){
        for(int y=1;y<=m;y++)
                if(G[x][y] && !vis[y])
                {
                        vis[y] = 1;
                        if(ym[y] == -1 || findpath(ym[y]))
                        {
                                xm[x] = y;
                                ym[y] = x;
                                return true;
                        }
                }
        return false;
}
int maxmatch()
{
        memset(xm,-1,sizeof(xm));
        memset(ym,-1,sizeof(ym));
        int ret = 0;
        for(int i=1;i<=n;i++){
                memset(vis,0,sizeof(vis));
                if(findpath(i)) ret++;
        }
        return ret;
}
double dis(int a, int b, int c, int d)
{
        return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
bool judge(int i,int j, int k)
{
        double d1 = dis(per[i][0],per[i][1],dog[k][0],dog[k][1]);
        double d2 = dis(per[j][0],per[j][1],dog[k][0],dog[k][1]);
        double d3 = dis(per[i][0],per[i][1],per[j][0],per[j][1]);
        return d1+d2<=d3*2;
}
int main()
{
        int i,j,k;
        while(scanf("%d %d",&n,&m)==2){
                memset(G,0,sizeof(G));
                for(i=1;i<=n;i++) scanf("%d%d",per[i]+0,per[i]+1);
                for(i=1;i<=m;i++) scanf("%d%d",dog[i]+0,dog[i]+1);
                n--;
                for(i=1;i<=n;i++)
                        for(j=1;j<=m;j++)
                        if(judge(i,i+1,j))
                        G[i][j] = 1;
                printf("%d\n",n+1+maxmatch());
                for(i=1;i<=n;i++)
                {
                        printf("%d %d ",per[i][0],per[i][1]);
                        if(xm[i]!=-1)
                                printf("%d %d ",dog[xm[i]][0],dog[xm[i]][1]);
                }
                printf("%d %d\n",per[n+1][0],per[n+1][1]);
        }
        return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值