답안 #257079

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257079 2020-08-03T15:16:34 Z eohomegrownapps 도시들 (IOI15_towns) C++14
25 / 100
25 ms 384 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> distv;
int n;

int dist(int a, int b){
    if (a==b){return 0;}
    if (a>b){
        swap(a,b);
    }
    if (distv[a][b]!=-1){
        return distv[a][b];
    } else {
        return distv[a][b]=getDistance(a,b);
    }
}

int hubDistance(int N, int sub) {
    n=N;
    distv.assign(n,vector<int>(n,-1));
	
    int adist = -1;
    int aind = -1;
    for (int i = 0; i<n; i++){
        int ds = dist(0,i);
        if (adist<ds){
            adist=ds;
            aind=i;
        }
    }


    int bdist = -1;
    int bind = -1;
    for (int i = 0; i<n; i++){
        int ds = dist(aind,i);
        if (bdist<ds){
            bdist=ds;
            bind=i;
        }
    }

    //from adist to aind
    //vector<int> diameter;
    int hubdist = 1e9;
    int hubind = -1;
    for (int i = 0; i<n; i++){
        if (i==aind||i==bind){continue;}
        int distfroma = dist(aind,i)-(dist(aind,i)+dist(bind,i)-dist(aind,bind))/2;
        int rv = max(distfroma,dist(aind,bind)-distfroma);
        if (rv<hubdist){
            hubdist=rv;
            hubind=i;
        }
    }
    if (sub<=2){return hubdist;}
	int hubfroma = dist(aind,hubind);
    
    vector<int> nodesinhub;
    int befcnt=0;
    int aftcnt=0;
    for (int i = 0; i<n; i++){
        int distfroma = dist(aind,i)-(dist(aind,i)+dist(bind,i)-dist(aind,bind))/2;
        if (distfroma<hubfroma){
            befcnt++;
        } else if (distfroma==hubfroma){
            nodesinhub.push_back(i);
        } else {
            aftcnt++;
        }
    }
    if (befcnt>n/2||aftcnt>n/2){return -hubdist;}
    vector<bool> visited(n,false);
    int nodesleft = nodesinhub.size();
    for (int i : nodesinhub){
        if (visited[i]){continue;}
        if (nodesleft<=n/2){break;}
        int nodesingrp = 1;
        visited[i]=true;
        for (int j : nodesinhub){
            if (!visited[j]){
                if (dist(i,j)<dist(aind,i)-dist(aind,hubind)+dist(aind,j)-dist(aind,hubind)){
                    nodesingrp++;
                }
            }
        }
        if (nodesingrp>n/2){return -hubdist;}
        nodesleft-=nodesingrp;
    }
    return hubdist;
}

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:76:36: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
     int nodesleft = nodesinhub.size();
                     ~~~~~~~~~~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 384 KB Output is correct
2 Correct 17 ms 384 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 23 ms 384 KB Output is correct
5 Correct 23 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 384 KB Output is correct
2 Correct 18 ms 384 KB Output is correct
3 Correct 23 ms 384 KB Output is correct
4 Correct 23 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 384 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -