[Usaco2017 Dec] A Pie for a Pie
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Usaco2017 Dec] A Pie for a Pie相关的知识,希望对你有一定的参考价值。
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=5140
[算法]
最短路
时间复杂度 : O(N^2)
[代码]
#include<bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; struct info { int x , y , id; } a[MAXN << 1] , b[MAXN << 1]; int n , d; int dist[MAXN << 1]; vector< int > G[MAXN << 1]; queue< int > q; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } inline bool cmpA(info a,info b) { return a.x < b.x; } inline bool cmpB(info a,info b) { return a.y < b.y; } int main() { read(n); read(d); for (int i = 1; i <= n; i++) { read(a[i].x); read(a[i].y); } for (int i = 1; i <= n; i++) { read(b[i].x); read(b[i].y); } memset(dist,255,sizeof(dist)); for (int i = 1; i <= n; i++) { if (a[i].y == 0) { q.push(i); dist[i] = 1; } a[i].id = i; } for (int i = 1; i <= n; i++) { if (b[i].x == 0) { q.push(i + n); dist[i + n] = 1; } b[i].id = i + n; } sort(a + 1,a + n + 1,cmpA); sort(b + 1,b + n + 1,cmpB); for (int i = 1; i <= n; i++) { int l = 1 , r = n , pos = -1; while (l <= r) { int mid = (l + r) >> 1; if (b[mid].y >= a[i].y) { pos = mid; r = mid - 1; } else l = mid + 1; } if (pos == -1) continue; for (int j = pos; j <= n; j++) { if (b[j].y > a[i].y + d) break; G[b[j].id].push_back(a[i].id); } } for (int i = 1; i <= n; i++) { int l = 1 , r = n , pos = -1; while (l <= r) { int mid = (l + r) >> 1; if (a[mid].x >= b[i].x) { pos = mid; r = mid - 1; } else l = mid + 1; } if (pos == -1) continue; for (int j = pos; j <= n; j++) { if (a[j].x > b[i].x + d) break; G[a[j].id].push_back(b[i].id); } } while (!q.empty()) { int cur = q.front(); q.pop(); for (unsigned i = 0; i < G[cur].size(); i++) { int v = G[cur][i]; if (dist[v] != -1) continue; dist[v] = dist[cur] + 1; q.push(v); } } for (int i = 1; i <= n; i++) printf("%d ",dist[i]); return 0; }
以上是关于[Usaco2017 Dec] A Pie for a Pie的主要内容,如果未能解决你的问题,请参考以下文章
USACO 2017 December GoldA Pie for a Pie 题解
USACO 2017 December Contest Gold T1: A Pie for a Pie
bzoj 1606 [Usaco2008 Dec]Hay For Sale 购买干草 01背包
[BZOJ1606] [Usaco2008 Dec] Hay For Sale 购买干草 (dp)