This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 4;
const int INF = 1e10;
const int N = 205;
using T = pair<int,int>;
struct Edge {
int u, v, w, c;
tuple<int,int,int,int> init() {
return {u, v, w, c};
}
};
int n, m, d[N][N], dis[N];
Edge edges[maxn];
set<int> adj[maxn];
priority_queue<T, vector<T>, greater<T>> pq;
int dijkstra(int s, int t) {
for (int i = 1; i <= n; i++)
dis[i] = INF;
dis[s] = 0; pq.push({0, s});
while (!pq.empty()) {
int u, d; tie(d, u) = pq.top(); pq.pop();
if (dis[u] != d) continue;
for (int i : adj[u]) {
int v = edges[i].v, w = edges[i].w;
if (dis[u] + w < dis[v]) {
dis[v] = dis[u] + w;
pq.push({dis[v], v});
}
}
}
return dis[t];
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
d[i][j] = INF;
for (int i = 1; i <= m; i++) {
int u, v, w, c; cin >> u >> v >> w >> c;
d[u][v] = min(d[u][v], w);
edges[i] = {u, v, w, c};
edges[i + m] = {v, u, w, c};
adj[u].insert(i);
}
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++)
for (int k = 1; k <= n; k++)
d[u][v] = min(d[u][v], d[u][k] + d[k][v]);
int res = INF;
int s = 1, t = n;
res = min(res, d[s][t] + d[t][s]);
for (int i = 1; i <= m; i++) {
int u, v, w, c; tie(u, v, w, c) = edges[i].init();
// int from = min(d[s][t], d[s][v] + w + d[u][t]);
// int to = min(d[t][s], d[t][v] + w + d[u][s]);
// if (from + to + c < res) {
adj[u].erase(i); adj[v].insert(i + m);
res = min(res, c + dijkstra(s, t) + dijkstra(t, s));
adj[v].erase(i + m); adj[u].insert(i);
// }
}
cout << (res == INF ? -1 : res);
}
# | 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... |