首页 > 专题系列 > Java解POJ > POJ 2060 Taxi Cab Scheme [解题报告] Java
2013
11-10

POJ 2060 Taxi Cab Scheme [解题报告] Java

Taxi Cab Scheme

问题描述 :

Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordination of the cabs in order to pick up the customers calling to get a cab as soon as possible,there is also a need to schedule all the taxi rides which have been booked in advance.Given a list of all booked taxi rides for the next day, you want to minimise the number of cabs needed to carry out all of the rides.

For the sake of simplicity, we model a city as a rectangular grid. An address in the city is denoted by two integers: the street and avenue number. The time needed to get from the address a, b to c, d by taxi is |a – c| + |b – d| minutes. A cab may carry out a booked ride if it is its first ride of the day, or if it can get to the source address of the new ride from its latest,at least one minute before the new ride’s scheduled departure. Note that some rides may end after midnight.

输入:

On the first line of the input is a single positive integer N, telling the number of test scenarios to follow. Each scenario begins with a line containing an integer M, 0 < M < 500, being the number of booked taxi rides. The following M lines contain the rides. Each ride is described by a departure time on the format hh:mm (ranging from 00:00 to 23:59), two integers a b that are the coordinates of the source address and two integers c d that are the coordinates of the destination address. All coordinates are at least 0 and strictly smaller than 200. The booked rides in each scenario are sorted in order of increasing departure time.

输出:

For each scenario, output one line containing the minimum number of cabs required to carry out all the booked taxi rides.

样例输入:

2
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11

样例输出:

1
2

解题代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main{
	static int n = 0;//提前预定的taxi总数
	static final int MAX = 500;//最大容量
	static Booked[] booked = new Booked[MAX];//taxi对象数组
	static boolean[][] graph = new boolean[MAX * 2][MAX * 2];//建图
	static boolean[] visit = new boolean[MAX * 2];//访问标记
	static int[] link = new int[MAX * 2];//记录前节点
 public static void main(String[] args) {
   Scanner cin = new Scanner(System.in);
   int t = cin.nextInt();
   while (t-- != 0) {
    n = cin.nextInt();
    for (int i = 0; i < n; i++) {
     String[] ss = cin.next().split(":");
     int time = Integer.parseInt(ss[0]) * 60 + Integer.parseInt(ss[1]);//计算出发时间
     booked[i] = new Booked(time, cin.nextInt(), cin.nextInt(), cin.nextInt(), cin.nextInt());//读入起始坐标
    }
    init();
    System.out.println(n - hungray());//节点数 - 最大二分匹配
   }
  }
	
  static void init() {//构建图
   for (int i = 0; i < MAX * 2; i++)
    Arrays.fill(graph[i], false);
    for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
      if (booked[i].reachable(booked[j]))
       graph[i][j + n] = true;
	 }
    }
  }
	
  static boolean find(int x) {//寻找可增广路
   for (int i = 0; i < n * 2; i++) {
    if (graph[x][i] && !visit[i]) {
      visit[i] = true;
      if (link[i] == -1 || find(link[i])) {
        link[i] = x;
        return true;
      }
     }
   }
   return false;
  }
	
  static int hungray() {//匈牙利求最大匹配
    int ans = 0;
		Arrays.fill(link, -1);
		for (int i = 0; i < n; i++) {
			Arrays.fill(visit, false);
			if (find(i))
				ans++;
		}
		return ans;
	}
}
class Booked {//预计的taxi类
	int time, a, b, c, d;
	public Booked(int time, int a, int b, int c, int d) {
		this.time = time;//出发时间
		this.a = a;//起点坐标
		this.b = b;
		this.c = c;//终点坐标
		this.d = d;
	}
	boolean reachable(Booked bo) {//两条路线能否用一辆taxi来代替
		return this.time + Math.abs(this.a - this.c) + Math.abs(this.b - this.d) 
		+ Math.abs(bo.a - this.c) + Math.abs(bo.b - this.d) < bo.time;
	}
}

  1. 有限自动机在ACM中是必须掌握的算法,实际上在面试当中几乎不可能让你单独的去实现这个算法,如果有题目要用到有限自动机来降低时间复杂度,那么这种面试题应该属于很难的级别了。