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;
//types
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define pw2(x) (1LL << x)
#define getBit(x, y) (x & (1LL << y))
template <class X, class Y>
bool cmax(X &a, const Y &b) {
return a < b ? a = b, 1 : 0;
}
template <class X, class Y>
bool cmin(X &a, const Y &b) {
return a > b ? a = b, 1 : 0;
}
//lowercase 31, all 53
//(P/Q) % M = (P % M) * (Q^(M-2) % M)
//-------------------------------------------------------
const ld PI = 3.14159265359;
const ll mx = 3e5+5, mod = 1e9+7;
bool vs[mx];
ll n, m, f[mx];
vector<pll> adj[mx];
void dfs(int u) {
vs[u] = true;
vector<ll> ed;
for (auto e : adj[u]) {
ll v, w;
tie(w, v) = e;
if (vs[v]) {
ed.push_back(f[v]);
continue;
}
dfs(v);
ed.push_back(f[v]);
}
sort(ed.begin(), ed.end(), greater<ll>());
for (int i = 0; i < ed.size(); i++) {
cmin(f[u], adj[u][i].fi + ed[i]);
}
}
/*
f[i] : kcnn tu dinh i den n
dfs den u
trong cac canh di tu u -> v
*/
main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
//freopen("Ferry.Inp", "r", stdin);
//freopen("Ferry.Out", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[v].push_back({w, u});
}
for (int i = 1; i <= n; i++) sort(adj[i].begin(), adj[i].end());
memset(f, inf, sizeof(f));
f[n] = 0;
dfs(1);
cout << f[1];
}
Compilation message (stderr)
ferries.cpp: In function 'void dfs(int)':
ferries.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < ed.size(); i++) {
| ~~^~~~~~~~~~~
ferries.cpp: At global scope:
ferries.cpp:66:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
66 | main() {
| ^~~~
# | 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... |