/*
Ben Watson
Quang Minh MasterDDDDD
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const string name = "test";
void solve();
signed main()
{
if (fopen((name + ".inp").c_str(), "r"))
{
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".ans").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
return 0;
}
// main program
const ll oo = 0x3f3f3f3f3f3f3f3f;
const int maxm = 5001;
int t[maxm], l[maxm], r[maxm], c[maxm];
vector<int> S, T, adj[maxm];
int n, m;
void build(int i, int j)
{
if (l[i] > l[j]) swap(i, j);
if (r[i] < l[j] - 1) return;
int diff = abs(t[i] - t[j]);
int l1 = l[i], r1 = r[i], l2 = l[j], r2 = r[j];
if (t[i] < t[j])
r1 -= diff;
else
{
l2 += diff;
if (l2 > r2)
return;
}
// if (i == 5 && j == 3)
// cout << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << '\n';
if (r1 < l2 - 1) return;
// cout << i << ' ' << j << '\n';
adj[i].push_back(j);
// cout << i << ' ' << j << '\n';
}
vector<ll> Dijsktra(int s)
{
vector<ll> dist(m + 1, oo);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.push({dist[s] = c[s], s});
while (!pq.empty())
{
int u = pq.top().second;
ll w = pq.top().first;
pq.pop();
if (dist[u] != w)
continue;
for (int v : adj[u])
if (dist[v] > dist[u] + c[v])
pq.push({dist[v] = dist[u] + c[v], v});
}
return dist;
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> t[i] >> l[i] >> r[i] >> c[i];
if (l[i] == 1)
S.push_back(i);
if (r[i] == n)
T.push_back(i);
}
for (int i = 1; i <= m; i++)
for (int j = i + 1; j <= m; j++)
{
build(i, j);
build(j, i);
}
ll res = oo;
for (int s : S)
{
vector<ll> dist = Dijsktra(s);
for (int t : T)
res = min(res, dist[t]);
// if (s == 5)
// for (int t : T)
// cout << t << ' ' << dist[t] << '\n';
}
cout << (res == oo ? -1 : res) << '\n';
}
Compilation message (stderr)
treatment.cpp: In function 'int main()':
treatment.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((name + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((name + ".ans").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |