Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 1483 | Accepted: 644 | Special Judge |
Description
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
All points in the input file are different and their coordinates are integers not greater than 1000 by the absolute value.
Output
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
#include<stdio.h>
#include<string.h>
#include<math.h>
int route[101][2],place[101][2];
int usedif[101];//usedif[i]记录Y顶点子集中编号为i的顶点是否使用,注意Y子集中的最多顶点数为(7-1)*12+12=84
int link[101];//link[i]记录与Y顶点子集中编号为i的顶点相连的X顶点子集中x的编号
int gou[101];
int mat[101][101];//mat[i][j]表示顶点i与j之间是否有边
int gx,gy;//gx为X顶点子集中的顶点数目,gy为Y顶点子集中的顶点数目
bool can(int t) //判断X中的顶点t在Y中是否有顶点与之匹配
{
for(int i=1; i<=gy; i++) //注意范围
{
if(usedif[i]==0&&mat[t][i]==1) //Y中的顶点i未匹配且t与i之间有边
{
usedif[i]=1;
if(link[i]==-1||can(link[i])) //link[i]=-1表示顶点i还未匹配
//can(link[i])表示顶点i已经匹配而现在唯一的路径,就是走到与i节点匹配的顶点link[i]处继续进行匹配
{
link[i]=t;
gou[t]=i;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int num=0;
memset(link,-1,sizeof(link));
memset(gou,-1,sizeof(gou));
for(int i=1; i<=gx; i++) //对X中的每个顶点在Y中寻找与其匹配的顶点,注意范围
{
memset(usedif,0,sizeof(usedif));
if(can(i)) num++;
}
return num;//返回最大匹配数
}
int main()
{
int n,m,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
gx=n;
gy=m;
memset(mat,0,sizeof(mat));
for(int i=1; i<=n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
route[i][0]=x;
route[i][1]=y;
}
for(int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
place[i][0]=x;
place[i][1]=y;
}
for(int i=1;i<=n-1;i++)
for(int j=1;j<=m;j++)
{
float len1=sqrt((float)(route[i][0]-place[j][0])*(route[i][0]-place[j][0])+(route[i][1]-place[j][1])*(route[i][1]-place[j][1]));
float len2=sqrt((float)(route[i+1][0]-place[j][0])*(route[i+1][0]-place[j][0])+(route[i+1][1]-place[j][1])*(route[i+1][1]-place[j][1]));
float len3=sqrt((float)(route[i][0]-route[i+1][0])*(route[i][0]-route[i+1][0])+(route[i][1]-route[i+1][1])*(route[i][1]-route[i+1][1]));
if((len1+len2)<=2*len3)
mat[i][j]=1;
}
ans=n+MaxMatch();
printf("%d/n",ans);
for(int i=1;i<=n;i++)
{
if(i-1) printf(" ");
printf("%d %d",route[i][0],route[i][1]);
if(gou[i]!=-1)
{
printf(" %d %d",place[gou[i]][0],place[gou[i]][1]);
}
}
printf("/n");
}
return 0;
}