#include <bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)
using ll = long long;
const int mxn = 9 + 1e6;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
void _27augain(void) {
ll n, m;
cin >> n >> m;
vector<vector<array<ll, 3>>> adj(n + 1);
vector<ll> roads(m);
vector<pair<ll, ll>> edges;
for (ll i = 0; i < m; ++i) {
ll u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w, i});
adj[v].push_back({u, w, i});
roads[i] = w;
edges.push_back({u, v});
}
if (m == n - 1) {
set<ll> E;
ll sumW = 0;
function<bool(ll, ll)> dfs = [&](ll u, ll p) {
if (u == n) {
return true;
}
for (auto &[v, w, i] : adj[u]) {
if (v ^ p) {
if (dfs(v, u)) {
E.insert(i);
sumW += w;
return true;
}
}
}
return false;
};
dfs(1, -1);
ll res = sumW, wst = roads[m - 1];
for (ll i = m - 1; i >= 0; --i) {
if (E.count(i)) {
res = max(res, sumW + wst);
}
wst = max(wst, roads[i]);
}
cout << res << "\n";
return;
}
if (*max_element(begin(roads), end(roads)) == 1 && *min_element(begin(roads), end(roads)) == 1) {
auto dij = [&](ll u) {
vector<ll> dis(n + 1, lnf);
priority_queue<pair<ll, ll>> pq;
dis[u] = 0;
pq.push({0, u});
while (!pq.empty()) {
auto [W, u] = pq.top();
pq.pop();
if (-W > dis[u]) {
continue;
}
for (auto [v, w, i] : adj[u]) {
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pq.push({-dis[v], v});
}
}
}
return dis;
};
auto _1 = dij(1), _n = dij(n);
set<ll> s;
map<pair<ll, ll>, ll> es;
for (ll i = 0; i < (int) edges.size(); ++i) {
auto [u, v] = edges[i];
if (min(_1[u] + 1 + _n[v], _1[v] + 1 + _n[u]) == _1[n]) {
s.insert(i);
if (_1[u] + 1 + _n[v] == _1[n]) {
++es[{_1[u], _n[v]}];
} else {
++es[{_1[v], _n[u]}];
}
}
}
ll res = _1[n];
for (ll i = 0; i <= m - 2; ++i) {
auto [u, v] = edges[i];
if (s.count(i) && ((es[{_1[u], _n[v]}] == 1 && _1[u] + 1 + _n[v] == _1[n]) || (es[{_1[v], _1[u]}] == 1 && _1[v] + 1 + _n[u]))) {
res = max(res, _1[n] + 1);
}
}
cout << res << "\n";
return;
}
auto dij = [&](ll i, ll add) {
vector<ll> dis(n + 1, lnf);
priority_queue<pair<ll, ll>> pq;
pq.push({0, 1});
dis[1] = 0;
while (!pq.empty()) {
auto [W, u] = pq.top();
pq.pop();
if (-W > dis[u]) {
continue;
}
for (auto [v, w, ci] : adj[u]) {
if (ci == i) {
w += add;
}
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pq.push({-dis[v], v});
}
}
}
return dis;
};
ll res = dij(-1, 0)[n], wst = roads[m - 1];
for (ll i = m - 1; i >= 0; --i) {
res = max(res, dij(i, wst)[n]);
wst = max(wst, roads[i]);
}
cout << res << "\n";
}
int32_t main(void) {
#define task "INCGRAPH"
cin.tie(0)->sync_with_stdio(0);
for (string iext : {"in", "inp"}) {
if (fopen((task "." + iext).c_str(), "r")) {
freopen((task "." + iext).c_str(), "r", stdin);
freopen(task ".out", "w", stdout);
}
}
int testcase = 1;
// cin >> testcase;
while (testcase--) _27augain();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Aesthetic.cpp: In function 'int32_t main()':
Aesthetic.cpp:137:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen((task "." + iext).c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:138:14: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(task ".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... |