This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifdef DEBUG
#define debug(args...) _debug(args)
void _debug(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
#else
#define debug(args...)
#endif
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 500005
int n, m;
unordered_map<int, vii> tadj[MAXN];
unordered_map<int, ll> sum[MAXN];
vector<pll> adj[MAXN];
priority_queue<pll, vector<pll>, greater<pll>> pq;
ll d[MAXN];
map<ii, int> id;
int ptr;
inline int getid(ii a) {
if (id.find(a) == id.end()) {
id[a] = ptr++;
}
return id[a];
}
int main() {
scanf("%d%d", &n, &m);
REP (i, 0, m) {
int a, b, c, p; scanf("%d%d%d%d", &a, &b, &c, &p);
tadj[a][c].pb(MP(b, p));
tadj[b][c].pb(MP(a, p));
sum[a][c] += p;
sum[b][c] += p;
}
ptr = n + 1;
REP (u, 1, n + 1) {
for (auto [c, vt] : tadj[u]) {
for (auto [v, w] : vt) {
adj[u].pb(MP(v, w));
adj[u].pb(MP(v, sum[u][c] - w));
adj[u].pb(MP(getid(MP(v, c)), 0));
adj[getid(MP(u, c))].pb(MP(v, sum[u][c] - w));
}
}
}
REP (i, 1, ptr) {
d[i] = LINF;
debug("%d:", i);
for (auto [v, w] : adj[i]) {
debug(" (%d %d)", v, w);
}
debug("\n");
}
d[1] = 0;
pq.push(MP(d[1], 1));
while (!pq.empty()) {
auto [ud, u] = pq.top(); pq.pop();
if (ud != d[u]) continue;
for (auto [v, w] : adj[u]) {
if (mnto(d[v], d[u] + w)) {
pq.push(MP(d[v], v));
}
}
}
if (d[n] == LINF) {
printf("-1\n");
} else {
printf("%lld\n", d[n]);
}
return 0;
}
/*
4 6
1 4 4 4
3 4 1 3
1 3 4 4
2 4 3 1
2 3 3 2
1 2 4 2
*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:81:13: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
81 | for (auto [v, w] : adj[i]) {
| ^~~~~~
Main.cpp:59:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:61:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | int a, b, c, p; scanf("%d%d%d%d", &a, &b, &c, &p);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |