// https://oj.uz/problem/view/JOI20_ho_t4
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 210;
const int inf = 1e18;
const int comp = 1e17;
int N, M;
vector<pair<int, int>> g[maxn];
tuple<int, int, int, int> ar[maxn];
int dist[maxn];
bool proc[maxn];
int ans = inf;
void dijkstra(int A)
{
for(int i = 1; i <= N; i++)
{
dist[i] = 0;
proc[i] = 0;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({1, A});
dist[A] = 1;
while(!pq.empty())
{
int x = pq.top().second;
pq.pop();
if(proc[x]) continue;
proc[x] = 1;
for(auto[v, w] : g[x])
{
if(dist[v] == 0 || dist[x]+w < dist[v])
{
dist[v] = dist[x]+w;
pq.push({dist[v], v});
}
}
}
}
int32_t main()
{
cin >> N >> M;
for(int i = 1; i <= M; i++)
{
int u, v, c, d;
cin >> u >> v >> c >> d;
ar[i] = make_tuple(u, v, c, d);
g[u].push_back({v, c});
}
for(int i = 0; i <= M; i++)
{
int D = 0;
if(i != 0)
{
auto[u, v, c, d] = ar[i];
for(auto it = g[u].begin(); it != g[u].end(); it++)
{
if(it->first == v && it->second == c)
{
g[u].erase(it);
break;
}
}
g[v].push_back({u, c});
D = d;
}
dijkstra(1);
int p1 = dist[N]-1;
dijkstra(N);
int p2 = dist[1]-1;
if(p1 != -1 && p2 != -1) ans = min(ans, p1+p2+D);
if(i != 0)
{
auto[u, v, c, d] = ar[i];
for(auto it = g[v].begin(); it != g[v].end(); it++)
{
if(it->first == u && it->second == c)
{
g[v].erase(it);
break;
}
}
g[u].push_back({v, c});
}
}
// cout << (ans > comp ? -1 : ans);
cout << ans;
return 0;
}
// sub 1 -> ok brute the one you'll invert
// sub 2 -> ok after inverting you can still use the other way
// for each guy, have P(1, i) P(i, N) P(N, i) P(i, 1). For each V ans = +1, min(+2, P(U, N)+d+c), +3, min(+4, P(U,1)+d+c)
// sub 3 -> choose one and just get to the end
# | 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... |