//================code===================//
//#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#include <random>
#include <chrono>
//#include <stdint.h>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<LL,LL>
#define micro 0.000000001
using namespace std;
ld PI = acos(-1);
LL gcd(LL a, LL b)
{
if (!(a && b)) return max(a, b);
return a % b ? gcd(b, a % b) : b;
}
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#define LLL LL
#else
#define AT
#define AE
#define ce(t)
#define LLL __int128
#endif
pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{-1,0},{-1,-1}, {0,-1} , {1,-1},{-1,-1} }; //RDLU EWSN
mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };
const int max_v = 2e2 + 7;
const int max_k = 2e2 + 7;
const int bsz = (1ll << 22) + 7;
const int INF = 0x3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f3f3f;
const LL mod = 1e9 + 7; //998244353;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a > mod) a -= mod; };
LL dist[max_v][max_v];
vector<int> adj[max_v];
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; ci(n >> m);
struct edge
{
LL u, v, c, d;
};
memset(dist, 0x3f, sizeof(dist));
vector<edge> ve(m);
int cd = 0;
fa(i, 0, n) dist[i][i] = 0;
for (auto& x : ve) ci(x.u >> x.v >> x.c >> x.d), --x.u, --x.v,dist[x.u][x.v]= x.c,adj[x.u].push_back(cd++);
fa(k, 0, n) fa(i, 0, n) fa(j, 0, n) ckmin(dist[i][j], dist[i][k] + dist[k][j]);
auto dijk = [&](int s,int e,int id)
{
vector<LL> dist(n, LNF);
priority_queue<setl, vector<setl>, lcmp> pq;
dist[s] = 0;
pq.push({ s,0 });
while (pq.size())
{
auto [u, c] = pq.top(); pq.pop();
if (dist[u] ^ c) continue;
if (ve[id].v == u)
{
if (dist[ve[id].u] > dist[u] + ve[id].c)
{
dist[ve[id].u] = dist[u] + ve[id].c;
pq.push({ ve[id].u,dist[ve[id].u] });
}
}
for (auto& x : adj[u]) if(x != id)
{
if (dist[ve[x].v] > dist[u] + ve[x].c)
{
dist[ve[x].v] = dist[u] + ve[x].c;
pq.push({ ve[x].v, dist[ve[x].v] });
}
}
}
return dist[e];
};
LL ans = dist[0][n - 1] + dist[n - 1][0];
int c = 0;
for (auto& x : ve)
{
LL l = min(dist[0][n - 1], dist[0][x.v] + dist[x.u][n - 1] + x.c);
LL r = min(dist[n - 1][0], dist[n - 1][x.v] + dist[x.u][0] + x.c);
if (l + r + x.d < ans)
{
LL rl = dijk(0,n-1,c);
LL rr = dijk(n-1,0,c);
ckmin(ans, rl + rr + x.d);
}
++c;
}
ans>=LNF ? co(-1): co(ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
11 ms |
676 KB |
Output is correct |
3 |
Correct |
11 ms |
716 KB |
Output is correct |
4 |
Correct |
12 ms |
728 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
660 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Incorrect |
11 ms |
720 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
3532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
724 KB |
Output is correct |
2 |
Correct |
15 ms |
680 KB |
Output is correct |
3 |
Correct |
28 ms |
2760 KB |
Output is correct |
4 |
Correct |
11 ms |
588 KB |
Output is correct |
5 |
Correct |
33 ms |
3484 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
584 KB |
Output is correct |
8 |
Correct |
37 ms |
3404 KB |
Output is correct |
9 |
Correct |
39 ms |
3512 KB |
Output is correct |
10 |
Correct |
36 ms |
3456 KB |
Output is correct |
11 |
Correct |
33 ms |
3500 KB |
Output is correct |
12 |
Correct |
33 ms |
3480 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
34 ms |
3488 KB |
Output is correct |
20 |
Correct |
33 ms |
3404 KB |
Output is correct |
21 |
Correct |
32 ms |
3404 KB |
Output is correct |
22 |
Correct |
35 ms |
3620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
716 KB |
Output is correct |
2 |
Correct |
11 ms |
676 KB |
Output is correct |
3 |
Correct |
11 ms |
716 KB |
Output is correct |
4 |
Correct |
12 ms |
728 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
660 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Incorrect |
11 ms |
720 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |