답안 #1079588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079588 2024-08-28T18:07:34 Z mindiyak 꿈 (IOI13_dreaming) C++14
0 / 100
1000 ms 13412 KB
#include "dreaming.h"
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <cmath>
#include <numeric>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, int> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<ld> vd;
typedef vector<long> vl;
typedef vector<ull> vull;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpi> vvpi;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define nl endl
#define S second
#define lb lower_bound
#define ub upper_bound
#define aint(x) x.begin(), x.end()
#define raint(x) x.rbegin(), x.rend()
#define ins insert
const int MOD = 1000000007;
 
vi visited;
vvpi paths;
vi prev_arr;
set<int> backtrack;
int node = 0;
int maxdist = 0;
int ans = 0;
int n;
 
void dij(int pos){
    vi dist(n,-1);
    prev_arr = vi(n,-1);
 
    queue<int> p;
    p.push(pos);
    dist[pos] = 0;
 
    while(!p.empty()){
        int a = p.front();p.pop();
        visited[a] = 1;
 
        for(auto b:paths[a]){
            if(a!= pos && prev_arr[a] == b.F)continue;
            dist[b.F] = dist[a] + b.S;
            p.push(b.F);
            prev_arr[b.F] = a;
        }
    }
 
    FOR(i,0,n){
        if(dist[i] > maxdist){
            maxdist = dist[i];
            node = i;
        }
    }
}
 
void dij2(int pos,int mxdst){
    vi dist(n,-1);
    vi dist2(n,-1);
    prev_arr = vi(n,-1);
 
    queue<int> p;
    p.push(pos);
    dist[pos] = 0;
    dist2[pos] = mxdst;
 
    while(!p.empty()){
        int a = p.front();p.pop();
 
        for(auto b:paths[a]){
            if(backtrack.count(b.F) == 0)continue;
            if(a!= pos && prev_arr[a] == b.F)continue;
            
            dist[b.F] = dist[a] + b.S;
            dist2[b.F] = dist2[a] - b.S;
            p.push(b.F);
            prev_arr[b.F] = a;
            if(max(dist[b.F],dist2[b.F]) < maxdist){
                maxdist = max(dist[b.F],dist2[b.F]);
                node = b.F;
            }
        }
    }
}
 
void go_back(int pos){
    backtrack.insert(pos);
    while(prev_arr[pos] != -1){
        backtrack.insert(prev_arr[pos]);
        pos = prev_arr[pos];
    }
}
 
int travelTime(int N, int m, int L, int A[], int B[], int T[]) {
    if(n==1)return 0;
    n = N;
    paths = vvpi(n);
    visited = vi(n,0);
    vi distances(n,0);
    ans = 0;
 
    FOR(i,0,m){
        paths[A[i]].pb({B[i],T[i]});
        paths[B[i]].pb({A[i],T[i]});
    }
 
    for(int i=0;i<n;i++){
        if(visited[i])continue;
        maxdist = 0;
        node = i;
        dij(i);
        cerr << i << " " << node << " " << maxdist << endl;
 
        maxdist = 0;
        int head = node;
        node = i;
        dij(head);
        cerr << "width " << head << " " << node << " " << maxdist << endl;
 
        ans = max(ans,maxdist);
 
        backtrack.clear();
        go_back(node);
 
        cerr << "path - ";
        for(int a:backtrack)cerr << a << " ";
        cerr << endl;
 
        dij2(node,maxdist);       
 
        head = node;
        maxdist = 0;
        dij(head);
 
        cerr << "center " << head << " " << maxdist << endl;
        cerr << endl;
 
        distances[i] = maxdist;
 
    }
 
    sort(distances.rbegin(),distances.rend());
 
    if(m == n-1)return ans;
 
    ans = max(ans,distances[0]+distances[1]+L);

    ans = max(ans,distances[2]+distances[1]+L+L);
 
    return ans;
    
}
# 결과 실행 시간 메모리 Grader output
1 Correct 260 ms 13412 KB Output is correct
2 Correct 266 ms 13188 KB Output is correct
3 Correct 173 ms 8876 KB Output is correct
4 Correct 36 ms 2140 KB Output is correct
5 Correct 27 ms 1372 KB Output is correct
6 Correct 58 ms 3232 KB Output is correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 260 ms 13412 KB Output is correct
2 Correct 266 ms 13188 KB Output is correct
3 Correct 173 ms 8876 KB Output is correct
4 Correct 36 ms 2140 KB Output is correct
5 Correct 27 ms 1372 KB Output is correct
6 Correct 58 ms 3232 KB Output is correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1070 ms 7656 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 260 ms 13412 KB Output is correct
2 Correct 266 ms 13188 KB Output is correct
3 Correct 173 ms 8876 KB Output is correct
4 Correct 36 ms 2140 KB Output is correct
5 Correct 27 ms 1372 KB Output is correct
6 Correct 58 ms 3232 KB Output is correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Halted 0 ms 0 KB -