#include <bits/stdc++.h>
using namespace std;
void setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, m, a[50000], b[50000], c[50001], d[50000];
bool check[200], onpath[50000];
long long to_0[200], from_0[200], to_n[200], from_n[200], head, tail;
vector<pair<int, int>> g[200], h[200];
long long res = 1e18;
priority_queue<pair<long long, int>> pq;
inline void Dijkstra(int source,
vector<pair<int, int>> (&graph)[200],
long long (&dist)[200])
{
int node;
long long cur;
fill_n(dist, 200, 1e18);
dist[source] = 0;
pq.push({0, source});
while (!pq.empty())
{
cur = -pq.top().first;
node = pq.top().second;
pq.pop();
if (dist[node] != cur)
{
continue;
}
for (auto &i : graph[node])
{
if (dist[i.first] > dist[node] + c[i.second])
{
dist[i.first] = dist[node] + c[i.second];
pq.push({-dist[i.first], i.first});
}
}
}
}
inline void DFS(int node,
vector<pair<int, int>> (&graph)[200],
long long (&dist)[200])
{
check[node] = true;
for (auto &i : graph[node])
{
if (!check[i.first] && dist[node] + c[i.second] == dist[i.first])
{
onpath[i.second] = true;
DFS(i.first, graph, dist);
}
}
}
inline long long Check(int block)
{
int node;
long long ret = 0, cur, dist[200];
fill_n(dist, n, 1e18);
dist[0] = 0;
pq.push({0, 0});
while (!pq.empty())
{
node = pq.top().second;
cur = -pq.top().first;
pq.pop();
if (cur != dist[node])
{
continue;
}
for (auto &i : g[node])
{
if (i.second != block && dist[i.first] > dist[node] + c[i.second])
{
dist[i.first] = dist[node] + c[i.second];
pq.push({-dist[i.first], i.first});
}
}
}
ret += dist[n - 1];
fill_n(dist, n, 1e18);
dist[n - 1] = 0;
pq.push({0, n - 1});
while (!pq.empty())
{
node = pq.top().second;
cur = -pq.top().first;
pq.pop();
if (cur != dist[node])
{
continue;
}
for (auto &i : g[node])
{
if (i.second != block && dist[i.first] > dist[node] + c[i.second])
{
dist[i.first] = dist[node] + c[i.second];
pq.push({-dist[i.first], i.first});
}
}
}
ret += dist[0];
return ret;
}
int main()
{
setup();
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
cin >> a[i] >> b[i] >> c[i] >> d[i];
a[i]--;
b[i]--;
g[a[i]].push_back({b[i], i});
h[b[i]].push_back({a[i], i});
}
Dijkstra(0, g, from_0);
Dijkstra(0, h, to_0);
Dijkstra(n - 1, g, from_n);
Dijkstra(n - 1, h, to_n);
res = min(res, from_0[n - 1] + from_n[0]);
DFS(0, g, from_0);
fill_n(check, 200, 0);
DFS(n - 1, g, from_n);
for (int i = 0; i < m; ++i)
{
if (onpath[i])
{
c[m] = c[i];
g[b[i]].push_back({a[i], m});
res = min(res, Check(i) + d[i]);
g[b[i]].pop_back();
}
else
{
head = min(from_0[n - 1], from_0[b[i]] + to_n[a[i]] + c[i]);
tail = min(from_n[0], from_n[b[i]] + to_0[a[i]] + c[i]);
res = min(res, head + tail + d[i]);
}
}
cout << (res == 1e18 ? -1 : res);
return 0;
}
# | 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... |