热身赛补题 Gym 101490J Programming Tutors

题目

J Programming Tutors
Picture by Damien Pollet via Flickr 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 10^8, 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 10^8, 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.
22 Problem J: Programming Tutors
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
Sample Input 3
3
0 5
5 2
4 5
3 3
5 2
5 2
Sample Output 3
5
Sample Input 4
2
0 0
0 5
-1 4
8 3
Sample Output 4
10

解题思路

n<=100,同时同学与老师恰好一一对应,让我们可以想到二分图匹配,进而得到O(n^3)的匈牙利算法可以使用,又因为求最大值最小二分的典型问法,所以是二分法,把匈牙利做完check函数,思路不复杂,

但是我太菜了,先是RE而后WA,,角落十几发才过,好菜啊,心态崩了

AC代码

#include
using namespace std;
typedef long long LL;
const int maxn = 2e2+5;
struct Node{
    LL x,y;
}s[maxn],t[maxn];
int N;
int a[maxn][maxn];
int linker[maxn];
bool used[maxn];
bool dfs(int u,LL limit){
   for(int i=1;i<=N;i++){
   		if(a[u][i]>limit){
   			continue;
		   }
		if(used[i]) continue;
		used[i]=1;
		if(linker[i]==-1||dfs(linker[i],limit)){
			linker[i]=u;
			return 1;
		}
   }
   return 0;
}

bool hungary(LL limit){
    
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(int u=1;u<=N;u++){
        memset(used,0,sizeof(used));
        if(dfs(u,limit)) res++;
    }
    if(res==N) return 1;
    else return 0;
}


int main()
{
   	cin>>N;
    for(int i=1;i<=N;++i)
       scanf("%lld%lld",&s[i].x,&s[i].y);
    for(int i=1;i<=N;++i)
        scanf("%lld%lld",&t[i].x,&t[i].y);
        LL mx=-1;
        for(int i=1;i<=N;++i){
            for(int j=1;j<=N;++j){
                LL dist = abs(s[i].x-t[j].x)+abs(s[i].y-t[j].y);
              	a[i][j]=dist;
            }
        }
        LL L=0,R=1e9,mid;
        while(L<=R){
            mid = (L+R)/2;
            if(hungary(mid)) R = mid-1;
            else L = mid+1;
        }
        cout<<L<<endl;
    return 0;
}

你可能感兴趣的:(热身赛补题 Gym 101490J Programming Tutors)