#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
const ll mod = 1e9 + 7;
const ll INF = 1e18;
//--------------------------------------------------------------------
const ll N = 3e5 + 10;
struct ED {
ll u, v, w;
} ed[N];
vector<pa> g[N];
ll n, m;
namespace sub3 {
bool check() {
return m == n - 1;
}
ll f[N];
bool dx[N];
pa par[N];
void dfs(ll u, ll p) {
for(auto e : g[u]) {
ll v = e.fi, id = e.se;
if(v == p) continue;
par[v] = {u, id};
f[v] = f[u] + ed[id].w;
dfs(v, u);
}
}
void slove() {
FOR(i, 1, m) {
ll u = ed[i].u, v = ed[i].v, w = ed[i].w;
g[u].push_back({v, i});
g[v].push_back({u, i});
}
dfs(1, 0);
vector<ll> vt;
ll u = n;
while(u != 1) {
ll v = par[u].fi, id = par[u].se;
dx[id] = 1;
u = v;
}
ll ans = f[n];
ll mx = -INF;
FORD(i, m, 1) {
if(dx[i]) {
ans = max(ans, f[n] + mx);
}
mx = max(ed[i].w, mx);
}
cout << ans;
}
}
void hbmt() {
cin >> n >> m;
FOR(i, 1, m) {
ll u, v, w;
cin >> u >> v >> w;
ed[i] = {u, v, w};
}
if(sub3::check()) return sub3::slove();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
#define NAME "hbmt"
if(fopen(NAME".inp", "r")) {
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
//int t;cin>>t;while(t--)
hbmt();
return 0;
}
Compilation message (stderr)
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:78:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:79:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(NAME".out", "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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |