【最小路径覆盖】POJ 2060

题意是求最小用几辆出租车完成n个任务,方法:最小路径覆盖 = 点数 - 最大匹配数,这里有个注意的细节http://www.cnblogs.com/ka200812/archive/2011/07/31/2122641.html

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define LL long long
#define MIN -99999999
#define MAX 99999999
#define pii pair<int ,int>

#define bug cout<<"here!!"<<endl
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define eps 1e-8
#define N 510
struct node{
    int beg,end;
    int x1,y1,x2,y2;
}p[N];
int match[N];
vector<int>  v[N];

bool vis[N];
bool sear(int s){
    int i,j;
    for(i=0;i<v[s].size();i++){
        int x = v[s][i];
        if(!vis[x]){
            vis[x] = 1;
            if(match[x] == -1 || sear(match[x])){
                match[x] = s;
                return true;
            }
        }
    }
    return false;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++){
            int aa,bb;
            scanf("%d:%d %d %d %d %d",&aa,&bb,&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
            p[i].beg = aa*60+bb;
            p[i].end = p[i].beg+abs(p[i].x1-p[i].x2)+abs(p[i].y1-p[i].y2);
            v[i].clear();
        }
        memset(match,-1,sizeof(match));
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                if(i==j)continue;
                int dis = abs(p[i].x2 - p[j].x1)+abs(p[i].y2 - p[j].y1);
                if(p[i].end+dis<p[j].beg){
                    v[i].push_back(j);
                }
            }
        }
        int cnt = 0;
        for(i=0;i<n;i++){
            memset(vis,0,sizeof(vis));
            if(sear(i))cnt++;
        }
        printf("%d\n",n-cnt);
    }
    return 0;
}


你可能感兴趣的:(【最小路径覆盖】POJ 2060)