Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <unordered_map>
- #include <numeric>
- #include <iomanip>
- using namespace std;
- #define pii pair<long long, long long>
- #define ll long long
- #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
- const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
- const long long dl[2] = {1, -1};
- const long long MOD = 1000000007;
- const long long MAXN = 1005;
- int r, c;
- int carn = 0, parkn = 0;
- int park[55][55]; // . = 0, X = -1, C = 1, P = 2 + 0, 2 + 1, ...
- bool visited[55][55];
- int dist[105][105];
- vector<pii> car;
- vector<bool> cars;
- vector<int> parks;
- vector<pii> v[105];
- void printdist(){
- for(int i = 0; i < 10; i++){
- cout << i << ": ";
- for(int j = 0; j < 10; j++){
- cout << dist[i][j] << " ";
- }
- cout << "\n";
- }
- }
- void init(){
- cin >> r >> c;
- int tmp = 0;
- for(int i = 0; i < r; i++){
- for(int j = 0; j < c; j++){
- char cc;
- cin >> cc;
- if(cc == '.'){
- park[i][j] = 0;
- }
- else if(cc == 'X'){
- park[i][j] = -1;
- }
- else if(cc == 'C'){
- park[i][j] = -2;
- car.push_back(make_pair(i, j));
- carn++;
- }
- else{
- park[i][j] = 2 + tmp;
- tmp++;
- parkn++;
- }
- }
- }
- queue<pair<pii, int>> q;
- for(int k = 0; k < car.size(); k++){
- pii coord = car[k];
- for(int i = 0; i < 55; i++){
- for(int j = 0; j < 55; j++){
- visited[i][j] = false;
- }
- }
- q.push(make_pair(coord, 0));
- while(!q.empty()){
- int cx = q.front().first.first;
- int cy = q.front().first.second;
- int cd = q.front().second;
- q.pop();
- if(visited[cx][cy]){
- continue;
- }
- visited[cx][cy] = true;
- if(park[cx][cy] >= 2){
- dist[k][park[cx][cy] - 2] = cd;
- }
- for(int i = 0; i < 4; i++){
- int nx = cx + dx[i];
- int ny = cy + dy[i];
- if(nx < 0 || nx >= r || ny < 0 or ny >= c || visited[nx][ny] || park[nx][ny] == -1){
- continue;
- }
- q.push({{nx, ny}, cd + 1});
- }
- }
- }
- }
- bool dfs(int cur, int x){
- if(cars[cur]){
- return false;
- }
- cars[cur] = true;
- for(auto nxt : v[cur]){
- if(nxt.second > x){
- continue;
- }
- if(parks[nxt.first] == -1 || dfs(parks[nxt.first], x)){
- parks[nxt.first] = cur;
- return true;
- }
- }
- return false;
- }
- bool match(int x){
- int parked = 0;
- parks.assign(parkn, -1);
- for(int i = 0; i < carn; i++){
- cars.assign(carn, false);
- if(dfs(i, x)){
- parked++;
- }
- }
- if(parked == carn){
- return true;
- }
- else{
- return false;
- }
- }
- void solve(){
- if(carn == 0){
- cout << "0\n";
- return;
- }
- if(carn > parkn){
- cout << "-1\n";
- return;
- }
- int maxt = 0;
- for(int i = 0; i < carn; i++){
- for(int j = 0; j < parkn; j++){
- if(dist[i][j] > 0){
- maxt = max(maxt, dist[i][j]);
- v[i].push_back(make_pair(j, dist[i][j]));
- }
- }
- }
- int lo = 0, hi = maxt;
- if(!match(maxt)){
- cout << "-1\n";
- return;
- }
- while(lo < hi){
- int mid = (lo + hi) / 2;
- bool ck = match(mid);
- if(!ck){
- lo = mid + 1;
- }
- else{
- hi = mid;
- }
- }
- cout << lo << "\n";
- }
- int main() {
- FAST;
- init();
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement