#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const bool typetest = 0;
const int N = 2e2 + 5;
const int M = 5e4 + 5;
const int Inf = 1e9 + 7;
const ll Inf64 = 1e17;
struct Edge
{
int u, v;
ll c, d;
bool operator<(const Edge &a) const
{
return v < a.v || (v == a.v && c < a.c) || (v == a.v && c == a.c && d < a.d);
}
} e[M];
vector<int> adj[N], nadj[N];
bool chosen[N];
int n, m;
int d[7][N];
bitset<M> ck[7];
ll ans(Inf64);
void Read()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d;
adj[e[i].u].push_back(i);
}
for (int i = 1; i <= n; ++i)
{
sort(adj[i].begin(), adj[i].end(), [&](const int &x, const int &y) {
return e[x] < e[y];
});
int p(0);
for (int j = 0; j < (int)adj[i].size(); ++j)
{
int t = j;
while (t < (int)adj[i].size() && e[adj[i][t]].v == e[adj[i][j]].v)
++t;
if (t - j > 2)
{
adj[i][p++] = adj[i][j];
adj[i][p++] = adj[i][j + 1];
j = t - 1;
}
else
{
while (j < t)
adj[i][p++] = adj[i][j++];
--j;
}
}
adj[i].resize(p);
for (auto j : adj[i])
chosen[j] = 1;
for (auto j : adj[i])
nadj[e[j].v].push_back(j);
}
}
void Dijkstra(int x, vector<int> adj[N], int d[N], bitset<M> &ck, int except)
{
fill_n(&d[0], N, Inf);
vector<bool> ok(n + 1, 0);
vector<int> par(n + 1);
d[x] = 0;
while (1)
{
int v(0);
for (int i = 1; i <= n; ++i)
if (!ok[i] && d[v] > d[i])
v = i;
if (v == 0)
break;
ok[v] = 1;
for (auto i : adj[v])
if (i != except && d[e[i].v ^ e[i].u ^ v] > d[v] + e[i].c)
{
d[e[i].v ^ e[i].u ^ v] = d[v] + e[i].c;
par[e[i].v ^ e[i].u ^ v] = i;
}
}
if (except == 0)
{
for (int i = 1; i <= n; ++i)
if (i != x && d[i] != Inf)
{
ck[par[i]] = 1;
}
}
}
ll Proc(int i, int x, int y, const vector<int> &s)
{
ll res(0);
int tmp1(s[0]), tmp2(s[3]);
if (ck[s[0]][i])
{
Dijkstra(x, adj, d[5], ck[5], i);
tmp1 = 5;
}
if (ck[s[3]][i])
{
Dijkstra(y, nadj, d[6], ck[6], i);
tmp2 = 6;
}
if (d[tmp1][e[i].v] == Inf || d[tmp2][e[i].u] == Inf)
return Inf64;
res = d[tmp1][e[i].v] + e[i].c + e[i].d + d[tmp2][e[i].u];
tmp1 = s[2];
if (ck[s[2]][i])
{
Dijkstra(y, adj, d[5], ck[5], i);
tmp1 = 5;
}
if (d[tmp1][x] == Inf)
return Inf64;
res += d[tmp1][x];
return res;
}
void Solve()
{
Dijkstra(1, adj, d[1], ck[1], 0);
Dijkstra(1, nadj, d[2], ck[2], 0);
Dijkstra(n, adj, d[3], ck[3], 0);
Dijkstra(n, nadj, d[4], ck[4], 0);
if (d[1][n] != Inf && d[3][1] != Inf)
ans = d[1][n] + d[3][1];
for (int i = 1; i <= m; ++i)
if (chosen[i])
{
ans = min(ans, Proc(i, 1, n, {1, 2, 3, 4}));
ans = min(ans, Proc(i, n, 1, {3, 4, 1, 2}));
}
cout << (ans == Inf64 ? -1 : ans);
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
Solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
22 ms |
4588 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
620 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |