제출 #758070

#제출 시각아이디문제언어결과실행 시간메모리
758070sysiaOlympic Bus (JOI20_ho_t4)C++17
37 / 100
1066 ms4588 KiB
//Sylwia Sapkowska #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; void __print(int x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << "'" << x << "'";} void __print(const char *x) {cerr << '"' << x << '"';} void __print(const string &x) {cerr << '"' << x << '"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef LOCAL #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define ll long long typedef pair<int, int> T; typedef tuple<int, int, int> F; const int oo2 = 2e9+7, mx = 202; int n; T pre[mx]; int dist[mx]; priority_queue<T>pq; set<int>cycle; long long dijkstra(vector<vector<F>>&g, int from, int to = -1){ for (int i = 1; i<=n; i++) { pre[i] = {0, -1}; dist[i] = oo2; } dist[from] = 0; pq.push({0, from}); while ((int)pq.size()){ auto [d, v] = pq.top(); pq.pop(); if (dist[v] < d) continue; for (auto [x, c, i]: g[v]){ if (dist[x] > dist[v] + c){ pre[x] = {v, i}; dist[x] = dist[v] + c; pq.push({dist[x], x}); } } } if (to >= 1){ int now = to; while (now){ cycle.insert(pre[now].second); now = pre[now].first; } return oo2; } return from == 1 ? dist[n] : dist[1]; } void solve(){ int m; cin >> n >> m; vector<vector<F>>g(n+1), gt(n+1); vector<tuple<int, int, int, int>>edges; vector d(n+1, vector<int>(n+1, oo2)); for (int i = 0; i<m; i++){ int a, b, c, dd; cin >> a >> b >> c >> dd; g[a].emplace_back(b, c, i); gt[b].emplace_back(a, c, i); d[a][b] = min(d[a][b], c); edges.emplace_back(a, b, c, dd); } for (int i = 1; i<=n; i++) d[i][i] = 0; for (int k = 1; k<=n; k++){ for (int i = 1; i<=n; i++){ for (int j = 1; j<=n; j++){ if (d[i][k] == oo2 || d[k][j] == oo2) continue; d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } dijkstra(g, 1, n); dijkstra(g, n, 1); long long ans = (long long)d[1][n] + d[n][1]; debug(ans); int k = 0; debug(cycle); for (auto &[a, b, c, dd]: edges){ if (cycle.find(k) != cycle.end()){ debug(a, b); auto [A, B, C, D] = edges[k]; for (int i = 0; i<(int)g[A].size(); i++){ if (g[A][i] == tie(B, C, k)){ swap(g[A][i], g[A].back()); break; } } g[A].pop_back(); g[B].emplace_back(A, C, k); long long tmp = (long long)dd + dijkstra(g, 1, 0) + dijkstra(g, n, 0); g[B].pop_back(); g[A].emplace_back(B, C, k); ans = min(ans, tmp); } else { ans = min(ans, (ll)dd + min((ll)d[1][n], (ll)d[1][b]+c+d[a][n]) + min((ll)d[n][1], (ll)d[n][b] + c + d[a][1])); } k++; } if (ans >= oo2) cout << "-1\n"; else cout << ans << "\n"; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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...