이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = ll(4e18) + ll(2e15);
const double EPS = 1e-9;
static int LamyIsCute = []() {
EmiliaMyWife
return 48763;
}();
const int N = 207;
vector<pair<int, int>> edge[N];
void dijk(int s, vector<int> &dis) {
dis[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if(d > dis[u])
continue;
for(const auto &[v, c] : edge[u]) {
if(dis[v] > dis[u] + c) {
dis[v] = dis[u] + c;
pq.push({dis[v], v});
}
}
}
}
signed main() {
int n, m;
cin >> n >> m;
vector<tuple<int, int, int, int>> edges;
for(int i = 0; i < m; i++) {
int u, v, c, d;
cin >> u >> v >> c >> d;
if(i % 2 == 0) {
edge[u].push_back({v, c});
edges.push_back({u, v, c, d});
}
}
vector<vector<int>> dis(n + 1, vector<int>(n + 1, INF));
for(int i = 1; i <= n; i++)
dijk(i, dis[i]);
int ans = INT_MAX;
if(dis[1][n] < INF && dis[n][1] < INF)
ans = min(ans, dis[1][n] + dis[n][1]);
for(const auto &[u, v, c, d] : edges) {
ans = min<ll>({
ans,
(dis[1][v] < INF && dis[u][n] < INF && dis[n][1] < INF ? dis[1][v] + dis[u][n] + c + dis[n][1] + d : INT_MAX),
(dis[1][v] < INF && dis[u][n] < INF && dis[n][v] < INF && dis[u][1] < INF ? dis[1][v] + dis[u][n] + c + dis[n][v] + dis[u][1] + c + d : INT_MAX),
(dis[1][n] < INF && dis[n][v] < INF && dis[u][1] < INF ? dis[1][n] + dis[n][v] + dis[u][1] + c + d : INT_MAX),
});
}
if(ans == INT_MAX)
cout << "-1\n";
else
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |