We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
>**#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>longlong*array;intcmp(constvoid*a,constvoid*b){longlongia=*(longlong*)a;longlongib=*(longlong*)b;returnarray[ia]<array[ib]?-1:array[ia]>array[ib];}intisValid(intmbikes,intnmen,intk,intz,longlongindex[]);intmain(){//find the k shortes edges "in the bipartite graph" between men & bikes//performance metric is the max distance among the k pairs//this is a min max problem, minimizing the max distanceintnmen,mbikes,kspots;scanf("%d %d %d",&nmen,&mbikes,&kspots);longmen[nmen][2],bikes[mbikes][2];for(inti=0;i<nmen;i++){scanf("%ld %ld",&men[i][0],&men[i][1]);}for(inti=0;i<mbikes;i++){scanf("%ld %ld",&bikes[i][0],&bikes[i][1]);}longlongd,dists[mbikes*nmen];for(inti=0;i<mbikes;i++){for(intj=0;j<nmen;j++){d=(bikes[i][0]-men[j][0]);dists[i*nmen+j]=d*d;d=(bikes[i][1]-men[j][1]);dists[i*nmen+j]+=d*d;}}//sort distances, only really need k smallest from each bike//discard those that are larger (but not those that are equal)longlongindex[mbikes*nmen];//use malloc to large size arrayfor(longi=0;i<mbikes*nmen;i++){index[i]=i;}array=dists;qsort(index,mbikes*nmen,sizeof(*index),cmp);//for(long i=0;i<mbikes*nmen;i++){printf("%lld ",dists[index[i]]);} printf("\n");intlast=kspots;//do binary search to find out minimum dist that allows a valid assignmentintleft=0,right=mbikes*nmen,width=mbikes*nmen,mid;while(width>4){width/=2;mid=(left+right)/2;//printf("Check %d\n",mid);if(!isValid(mbikes,nmen,kspots,mid,index)){left=mid;}else{right=mid;}}last=right;for(intj=left;j<right;j++){//printf("Check %d\n",j);if(isValid(mbikes,nmen,kspots,j,index)){last=j;break;}}printf("%lld",dists[index[last-1]]);return0;}#define WHITE 0#define GRAY 1#define BLACK 2#define MAX_NODES 1000#define oo 1000000000intn;// number of nodesinte;// number of edgesintcapacity[MAX_NODES][MAX_NODES];// capacity matrixintflow[MAX_NODES][MAX_NODES];// flow matrixintcolor[MAX_NODES];// needed for breadth-first search intpred[MAX_NODES];// array to store augmenting pathintmax_flow(intsource,intsink);intisValid(intmbikes,intnmen,intk,intz,longlongindex[]){//check if we can pick k unique row/col pairs among the first z//this is a matching of cardinality k in the bipartite ii-jj graphif(z<k)return0;//capacity rows 0-249, cols 250-499, source as 500, sink as 501for(inti=0;i<500;i++){for(intj=0;j<500;j++){capacity[i][j]=0;}}for(inti=0;i<250;i++){capacity[500][i]=1;}for(inti=0;i<250;i++){capacity[250+i][501]=1;}for(inti=0;i<z;i++){intii=index[i]/nmen;intjj=index[i]%nmen;capacity[ii][250+jj]=1;}n=502;e=z+2;intmaxflow=max_flow(500,501);//printf("Max flow for z= %d\n",maxflow);if(maxflow>=k)return1;elsereturn0;}// below follows Ford-Fulkerson algorithm for max matching via max flowintmin(intx,inty){returnx<y?x:y;// returns minimum of x and y}inthead,tail;intq[MAX_NODES+2];voidenqueue(intx){q[tail]=x;tail++;color[x]=GRAY;}intdequeue(){intx=q[head];head++;color[x]=BLACK;returnx;}intbfs(intstart,inttarget){intu,v;for(u=0;u<n;u++){color[u]=WHITE;}head=tail=0;enqueue(start);pred[start]=-1;while(head!=tail){u=dequeue();// Search all adjacent white nodes v. If the capacity// from u to v in the residual network is positive, enqueue v.for(v=0;v<n;v++){if(color[v]==WHITE&&capacity[u][v]-flow[u][v]>0){enqueue(v);pred[v]=u;}}}// If the color of the target node is black now, it means that we reached it.returncolor[target]==BLACK;}intmax_flow(intsource,intsink){inti,j,u;// Initialize empty flow.intmax_flow=0;for(i=0;i<n;i++){for(j=0;j<n;j++){flow[i][j]=0;}}// While there exists an augmenting path, increment the flow along this path.while(bfs(source,sink)){// Determine the amount by which we can increment the flow.intincrement=oo;for(u=n-1;pred[u]>=0;u=pred[u]){increment=min(increment,capacity[pred[u]][u]-flow[pred[u]][u]);}// Now increment the flow.for(u=n-1;pred[u]>=0;u=pred[u]){flow[pred[u]][u]+=increment;flow[u][pred[u]]-=increment;}max_flow+=increment;}// No augmenting path anymore. We are done.returnmax_flow;}``**
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Bike Racers
You are viewing a single comment's thread. Return to all comments →