Programming Tutors

Programming Tutors (二分+匈牙利算法)

题目链接
You are the founder of the Bruce Arden Programming
Collective, which is a tutoring programme that matches
experienced programmers with newbies to teach them.
You have N students and N tutors, but now you have
to match them up. Since the students will have to travel
to their tutors’ houses from their own (or vice versa) you
decide to do your matching based on travel distance.
Minimising overall distance doesn’t seem fair; it might happen that one student has to travel
a huge distance while all the other students get a tutor very close by, even though the tutors
could have been split up so that each gets a tutor that is at least somewhat close.
Thus, you opt to minimise the distance travelled by the student who is worst off; one pairing
of students to tutors is better than another if the student who has to travel farthest in the
first pairing has to travel less far than the student who has to travel farthest in the second
pairing.
Because the students live in a city, the distance that a student needs to travel is not the
literal distance between them and their tutor. Instead, the distance between points (X, Y )
and (X0, Y 0) in the city is|X − X0| + |Y − Y0|.

Input
The first line of the input contains an integer N, with 1 ≤ N ≤ 100, the number of students
and the number of tutors to pair up.
Then, there are N lines, each with two space separated integers with absolute value at most
108, which give the locations of the N students.
These are followed by N lines, each with two space separated integers with absolute value at
most 108, which give the locations of the N tutors.
Note that it is possible for students and/or tutors to have identical locations (they may share
a house).

Output
Output a single line containing a single integer K, where K is the least integer such that there
exists a pairing of students to tutors so that no pair has distance greater than K between
them.

Sample Input 1
2
0 0
0 3
0 2
0 5
Sample Output 1
2

Sample Input 2
4
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
Sample Output 2
2

题目大意:
给你n对学生和老师点的坐标,求n个学生与n个老师匹配情况中,学生与老师距离最大值中的最小值。

思路:
利用二分的思想,通过匈牙利算法求出最大匹配数量,根据最大匹配数量来对结果的区间进行缩小。
比如我们设立初始left值为0,right值为100,我们就计算最大距离不超过50的匹配情况,如果我们能让每个老师都有各自的学生,就说明这个标准太高,我们就让right值变小。如果在最大距离是50的情况下,有的学生匹配不到老师,我们就让left值变大,进而提高标准。
稍微修改一下匈牙利算法即可。


#include
using namespace std;
struct node
{
    int x,y;
};
struct node S[110],T[110];
int dis[110][110];
int pre[110];
int vis[110];
int m,n;
int DFS(int t,int mid)
{
    for(int i=1;i<=n;i++)
    {
        if(dis[t][i]<=mid&&!vis[i])			///如果二者之间的距离小于我们设定的标准值,就找这个点对应的老师
        {
            vis[i]=1;
            if(pre[i]==-1||DFS(pre[i],mid))
            {
                pre[i]=t;
                return 1;
            }
        }
    }
    return 0;
}
int find(int mid)
{
    int ans=0;
    memset(pre,-1,sizeof(pre));
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
       if(DFS(i,mid))
       {
           ans++;
       }
    }
    if(ans==n)
        return 1;
    return 0;
}
int main()
{
    cin>>n;
    int maxx=0;
    for(int i=1;i<=n;i++)
    {
        cin>>S[i].x>>S[i].y;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>T[i].x>>T[i].y;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dis[i][j]=abs(S[i].x-T[j].x)+abs(S[i].y-T[j].y);
            maxx=max(maxx,dis[i][j]);
        }
    }
    int left=0;
    int right=maxx;
    while(left<=right)
    {
        int mid=(left+right)/2;
        if(find(mid))
        {
            right=mid-1;
        }
        else
        {
            left=mid+1;
        }
    }
    cout<

你可能感兴趣的:(图论)