제출 #1131455

#제출 시각아이디문제언어결과실행 시간메모리
1131455steveonalexOlympic Bus (JOI20_ho_t4)C++20
0 / 100
77 ms3004 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1ULL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll lcm(ll a, ll b){return a / gcd(a, b) * b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ull mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} double rngesus_d(double l, double r){ double cur = rngesus(0, MASK(60) - 1); cur /= MASK(60) - 1; return l + cur * (r - l); } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int N = 220; const ll INF = 1e12 + 69; int n, m; vector<array<ll, 4>> edges; ll g[N][N]; ll calc_dis(int u, int v){ ll cost[n+1][n+1], dis[n+1]; for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) if (i != j) cost[i][j] = INF; else cost[i][j] = 0; for(int i = 1; i <= n; ++i) dis[i] = INF; for(auto i: edges){ minimize(cost[i[0]][i[1]], i[2]); } bool visited[n+1]; memset(visited, 0, sizeof visited); dis[u] = 0; for(int it = 0; it < n; ++it){ pair<ll, int> mi = {INF, 0}; for(int i = 1; i <= n; ++i) if (!visited[i]){ minimize(mi, make_pair(dis[i], i)); } if (mi.second == 0) break; visited[mi.second] = true; for(int j = 1; j <= n; ++j) minimize(dis[j], mi.first + cost[mi.second][j]); } return dis[v]; } vector<int> calc_used_edge(int u, int v){ pair<ll, int> cost[n+1][n+1], dis[n+1]; for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) if (i != j) cost[i][j] = {INF, -1}; else cost[i][j] = {0, -1}; for(int i = 1; i <= n; ++i) dis[i] = {INF, -1}; for(int it = 0; it < m; ++it){ auto i = edges[it]; minimize(cost[i[0]][i[1]], make_pair(i[2], it)); } bool visited[n+1]; memset(visited, 0, sizeof visited); dis[u] = {0, -1}; for(int it = 0; it < n; ++it){ pair<ll, int> mi = {INF, 0}; for(int i = 1; i <= n; ++i) if (!visited[i]){ minimize(mi, make_pair(dis[i].first, i)); } if (mi.second == 0) break; visited[mi.second] = true; for(int j = 1; j <= n; ++j) if (!visited[j]) minimize(dis[j], make_pair(mi.first + cost[mi.second][j].first, cost[mi.second][j].second)); } vector<int> ans; int cur = v; while(cur != u){ if (dis[cur].second == -1) assert(false); ans.push_back(dis[cur].second); cur = edges[dis[cur].second][0]; } return ans; } ll solve_case(bool is_og){ for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) if (i != j){ g[i][j] = INF; } for(auto i: edges){ minimize(g[i[0]][i[1]], i[2]); } for(int k = 1; k <= n; ++k) for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) minimize(g[i][j], g[i][k] + g[k][j]); ll ans = INF; if (is_og){ for(int j = 0; j < m; ++j) { int u = edges[j][0], v = edges[j][1]; ll w = edges[j][2], d = edges[j][3]; if (u == 1 || u == n || v == 1 || v == n) continue; minimize(ans, (d + w) + g[1][v] + g[n][v] + g[u][1] + g[u][n]); } } if (g[1][n] == INF) return ans; minimize(ans, g[1][n] + g[n][1]); vector<bool> required_edge(m); vector<int> sigma = calc_used_edge(1, n); for(int j: sigma) required_edge[j] = true; for(int j = 0; j < m; ++j) { int u = edges[j][0], v = edges[j][1]; ll w = edges[j][2], d = edges[j][3]; if (!required_edge[j]) minimize(ans, g[1][n] + g[n][v] + (w + d) + g[u][1]); else{ swap(edges[j][0], edges[j][1]); minimize(ans, calc_dis(1, n) + g[n][v] + (w + d) + g[u][1]); swap(edges[j][0], edges[j][1]); } } return ans; } ll solve(){ cin >> n >> m; for(int i = 0; i < m; ++i){ array<ll, 4> arr; for(int j = 0; j < 4; ++j) cin >> arr[j]; edges.push_back(arr); // if (arr[0] != arr[1]) // cout << arr[0] << " " << arr[1] << "\n"; } ll ans = INF; minimize(ans, solve_case(true)); for(auto &i: edges) swap(i[0], i[1]); minimize(ans, solve_case(false)); if (ans == INF) ans = -1; return ans; } int main(void){ ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); clock_t start = clock(); cout << solve() << "\n"; cerr << "Time elapsed: " << clock() - start << " ms\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...