ZOJ 3156 Taxi (二分匹配+二分查找)
Posted slgkaifa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 3156 Taxi (二分匹配+二分查找)相关的知识,希望对你有一定的参考价值。
题目链接:Taxi
As we all know, it often rains suddenly in Hangzhou during summer time.I suffered a heavy rain when I was walking on the street yesterday, so I decided to take a taxi back school. I found that there weren people on the street trying to take taxis, and m taxicabs on the street then. Supposing that the cars waited still and each person walked at a speed ofv, now given the positions of then persons and them taxicabs, you should find the minimum time needed for all the persons to get on the taxicabs. Assume that no two people got on the same taxicab.
Input
For each case, you are given two integers 0 <= n <= 100 and n< =m <= 100 on the first line, thenn lines, each has two integers 0 <=Xi,Yi <= 1000000 describing the position of the ith person, thenm lines, each has two integers 0 <=xi,yi< = 1000000 describing the position the ith taxicab, then a line has a float 0.00001 <v <= 10000000 which is the speed of the people.
Output
You shuold figue out one float rounded to two decimal digits for each case.
Sample Input
2 3 0 0 0 1 1 0 1 1 2 1 1
Sample Output
1.00
题意:n个人,m辆车,不论什么是n个人的坐标。和m个车的坐标,计算全部人上车所用的最少时间
错了5遍,dis数组开小了。。。
思路:和POJ2536几乎相同。仅仅是本题是求最小时间,所以在二分查找最小时间的同一时候,进行推断当前点是否存在增广路
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define init(a) memset(a,0,sizeof(a)) #define PI acos(-1,0) using namespace std; const int maxn = 110; const int maxm = 10010; #define lson left, m, id<<1 #define rson m+1, right, id<<1|1 #define min(a,b) (a>b)?b:a #define max(a,b) (a>b)?a:b int n,m; bool vis[maxn]; int line[maxn]; double ma[maxn][maxn]; struct node { double x,y; }; node g[maxn],h[maxn]; double dis[maxm],st; int cmp(const void *a,const void *b) { return (*(double *)a > *(double *)b) ? 1 : -1; } int DFS(int u) { for(int v = 1; v <= m; v++) { if (ma[u][v]<=st && !vis[v]) { vis[v] = 1; if (line[v]== -1 || DFS(line[v])) { line[v] = u; return 1; } } } return 0; } int K_M() { memset(line,-1,sizeof line); for (int i=1; i<=n; i++) { init(vis); if (DFS(i)==0) return 0; } return 1; } int ans; void B_search(int num) { int low = 0; int high = num - 1; while (low <= high) { //printf("low = %d high = %d\n",low,high); //printf("mid = %d",mid); int mid = (low + high)/2; st = dis[mid]; //printf("st = %.2lf\n",st); //printf("ans = %lf\n",ans); if ( K_M()==1 ) { ans = mid; high = mid - 1; } else low = mid + 1; } } int main() { double v; int num = 0; while(scanf("%d%d",&n,&m)!=EOF) { init(ma); num = 0; for (int i=1; i<=n; i++) scanf("%lf%lf",&g[i].x,&g[i].y); /*for(int i = 1;i<=n;i++) printf("g.x = %lf g.y = %lf\n",g[i].x,g[i].y);*/ for (int i=1; i<=m; i++) scanf("%lf%lf",&h[i].x,&h[i].y); /*for(int i = 1;i<=n;i++) printf("h.x = %lf h.y = %lf\n",h[i].x,h[i].y);*/ scanf("%lf",&v); for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) { ma[i][j] = (sqrt((g[i].x-h[j].x)*(g[i].x-h[j].x)+(g[i].y-h[j].y)*(g[i].y-h[j].y))/v); dis[num++] = ma[i][j]; } } qsort(dis,num,sizeof(dis[0]),cmp); /*for(int i = 0;i<num;i++) printf("%.2lf ",dis[i]); printf("\n");*/ B_search(num); printf("%.2lf\n",dis[ans]); } return 0; }
以上是关于ZOJ 3156 Taxi (二分匹配+二分查找)的主要内容,如果未能解决你的问题,请参考以下文章
ZOJ - 3265: Strange Game (优化 二分图匹配)
ZOJ 1654--Place the Robots二分匹配 && 经典建图
ZOJ 1654 Place the Robots建图思维(分块思想)+二分匹配