您的位置:首页 > 其它

Poj-2060 Taxi Cab Scheme 二分图最小路径覆盖

2014-07-18 13:16 302 查看
题目链接

出租车公司有n个预约, 每个预约有时间和地点, 地点分布在二维整数坐标系上, 地点之间的行驶时间为两点间的曼哈顿距离(|x1 - x2| + |y1 - y2|)。一辆车可以在运完一个乘客后运另一个乘客, 条件是此车要在预约开始前一分钟之前到达出发地, 问最少需要几辆车搞定所有预约。





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 505;
const int Mod = 1000000007;
const double inf = 1<<30;
int n;
int map[maxn][maxn];
struct node
{
int sta,t;
int sx,sy,ex,ey;
}book[maxn];
int cx[maxn],cy[maxn];
bool vis[maxn];
bool FindPath( int u )
{
for( int i = u+1; i <= n; i ++ )
{
if( !vis[i] && map[u][i] )
{
vis[i] = true;
if( cy[i] == -1 || FindPath( cy[i] ) )
{
cy[i] = u;
cx[u] = i;
return true;
}
}
}
return false;
}
int MaxMatch()
{
int ans = 0;
memset( cx,-1,sizeof(cx) );
memset( cy,-1,sizeof(cy) );
for( int i = 1; i <= n; i++ )
{
if( cx[i] == -1 )
{
memset( vis,0,sizeof(vis) );
ans += FindPath( i );
}
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas,h,m;
scanf("%d",&cas);
while( cas -- )
{
scanf("%d",&n);
memset( map,0,sizeof(map) );
for( int i = 1; i <= n; i ++ )
{
scanf("%d:%d%d%d%d%d",&h,&m,&book[i].sx,&book[i].sy,&book[i].ex,&book[i].ey);
book[i].sta = h*60 + m;
book[i].t = abs( book[i].sx - book[i].ex ) + abs( book[i].sy - book[i].ey );
}
for( int i = 1; i <= n; i ++ )
{
for( int j = i+1; j <= n; j ++ )
{
int dis = abs( book[j].sx - book[i].ex ) + abs( book[j].sy - book[i].ey );
if( book[i].t + dis + 1 <= book[j].sta - book[i].sta )
map[i][j] = 1;
}
}
printf("%d\n",n - MaxMatch());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: