码迷,mamicode.com
首页 > 其他好文 > 详细

UVaLive 3126 Taxi Cab Scheme (最小路径覆盖)

时间:2017-10-04 00:17:20      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:include   mon   const   最小路径覆盖   题意   i+1   back   opera   stack   

题意:有 n 个客人,要从 si 到 ti,每个人有一个出发时间,现在让你安排最少和出租车去接,在接客人时至少要提前一分钟到达客人的出发地点。

析:把每个客人看成一个结点,然后如果用同一个出租车接的话,那么肯定是先接 u 然后再去接 v,也就是有一条边 u->v,画图看的就成知道,这是一个最小路径覆盖的问题。把每个结点拆成 X和 Y 然后如果 u 能连 v,那么就 Xu -> Yv,然后跑一次二分最大匹配,那么答案就是 n - 最大匹配数。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 100 + 10;
const int mod = 1000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
  int to, next;
};
Edge edge[maxn*maxn];
int head[maxn], cnt;

void addEdge(int u, int v){
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

bool used[maxn];
int match[maxn];

bool dfs(int u){
  used[u] = 1;
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to, w = match[v];
    if(w < 0 || !used[w] && dfs(w)){
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

struct Pessonger{
  int time, sx, sy, tx, ty, dist;
  bool operator < (const Pessonger &p) const{
    return time < p.time;
  }
};
Pessonger pess[maxn];

bool judge(int i, int j){
  int t = pess[i].dist + pess[i].time + abs(pess[i].tx-pess[j].sx) + abs(pess[i].ty-pess[j].sy);
  return t < pess[j].time;
}

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
      int h, s;
      scanf("%d:%d %d %d %d %d", &h, &s, &pess[i].sx, &pess[i].sy, &pess[i].tx, &pess[i].ty);
      pess[i].time = h * 60 + s;
      pess[i].dist = abs(pess[i].sx - pess[i].tx) + abs(pess[i].sy - pess[i].ty);
    }
    ms(head, -1);  cnt = 0;
    FOR(i, 0, n)  for(int j = i+1; j < n; ++j){
      if(judge(i, j))  addEdge(i<<1, j<<1|1);
    }
    ms(match, -1);
    int ans = 0;
    for(int i = 0; i < (n<<1); ++i)  if(match[i] < 0){
      ms(used, 0);  if(dfs(i)) ++ans;
    }
    printf("%d\n", n - ans);
  }
  return 0;
}

  

UVaLive 3126 Taxi Cab Scheme (最小路径覆盖)

标签:include   mon   const   最小路径覆盖   题意   i+1   back   opera   stack   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7624791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!