제출 #757201

#제출 시각아이디문제언어결과실행 시간메모리
757201sysiaOlympic Bus (JOI20_ho_t4)C++17
37 / 100
1067 ms3128 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 typedef pair<int, int> T; set<T>cycle; const long long oo = 1e18; const int oo2 = 1e9+7; vector<int> dijkstra(int n, vector<vector<T>>&g, int from, int to = -1){ vector<int>dist(n+1, oo2); dist[from] = 0; set<T>s; vector<int>pre(n+1); s.insert({0, from}); while ((int)s.size()){ auto [d, v] = *s.begin(); s.erase(s.begin()); if (dist[v] < d) continue; for (auto [x, c]: g[v]){ if (dist[x] > dist[v] + c){ pre[x] = v; dist[x] = dist[v] + c; s.insert({dist[x], x}); } } } if (to != -1){ int now = to; vector<int>path; while (now){ path.emplace_back(now); now = pre[now]; } reverse(path.begin(), path.end()); for (int i = 1; i<(int)path.size(); i++) cycle.insert({path[i-1], path[i]}); } return dist; } int dijkstra2(int n, vector<vector<T>>&g, int from, int to = -1){ vector<int>dist(n+1, oo2); dist[from] = 0; set<T>s; s.insert({0, from}); while ((int)s.size()){ auto [d, v] = *s.begin(); s.erase(s.begin()); if (dist[v] < d) continue; for (auto [x, c]: g[v]){ if (dist[x] > dist[v] + c){ dist[x] = dist[v] + c; s.insert({dist[x], x}); } } } return dist[to]; } void solve(){ int n, m; cin >> n >> m; vector<vector<T>>g(n+1), gt(n+1); vector<tuple<int, int, int, int>>edges; for (int i = 0; i<m; i++){ int a, b, c, d; cin >> a >> b >> c >> d; g[a].emplace_back(b, c); gt[b].emplace_back(a, c); edges.emplace_back(a, b, c, d); } auto d1 = dijkstra(n, g, 1, n); //1 do x auto dn = dijkstra(n, g, n, 1); //n do x auto d1rev = dijkstra(n, gt, 1); //x do 1 auto dnrev = dijkstra(n, gt, n); //x do n long long ans = d1[n] + dn[1]; int k = 0; debug(cycle); for (auto &[a, b, c, d]: edges){ if (cycle.find({a, b}) != cycle.end()){ debug(a, b); long long tmp = d; vector<vector<T>>G(n+1); for (int i = 0; i<m; i++){ auto [A, B, C, D] = edges[i]; if (i == k) { G[B].emplace_back(A, C); continue; } G[A].emplace_back(B, C); } tmp += dijkstra2(n, G, 1, n) + dijkstra2(n, G, n, 1); ans = min(ans, tmp); } else { ans = min(ans, (long long)d + (long long)min(d1[n], d1[b]+c+dnrev[a]) + (long long)min(dn[1], dn[b] + c + d1rev[a])); } 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); int t = 1; //cin >> t; while (t--) 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...