#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
struct Edge{
int v, w, i;
Edge(){}
Edge(int _v, int _w, int _i): v(_v), w(_w), i(_i) {}
};
vector<Edge> adj[500500];
int on[2002000], W[2002000], dist[500500];
vector<Edge> E;
vector<int> P;
bool dfs0(int s, int e, int pa = -1){
if (s==e) return 1;
for (auto &v:adj[s]) if (v.v!=pa && on[v.i]){
P.push_back(v.i);
if (dfs0(v.v, e, s)) return 1;
P.pop_back();
}
return 0;
}
int visited[500500], tm;
void dfs(int s, int pa = -1, int w = 0){
visited[s] = tm;
if (pa==-1) dist[s] = 0;
else dist[s] = dist[pa] + w;
for (auto &v:adj[s]) if (v.v!=pa && on[v.i]) dfs(v.v, s, v.w);
}
int cur = 0, ans = 1e9, m;
int calc(int s, int n){
fill(dist, dist+n, 0);
dfs(s);
if (*max_element(dist, dist+n)==0) return 0;
dfs(max_element(dist, dist+n)-dist);
return *max_element(dist, dist+n);
}
int main(){
int n;
scanf("%d %d", &n, &m);
for (int i=1;i<=m;i++){
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
adj[x].emplace_back(y, z, i);
adj[y].emplace_back(x, z, i);
W[i] = z;
if (z==1) on[i] = 1, cur++;
else E.emplace_back(x, y, i);
}
ans = min(ans, cur*2 - calc(0, n));
if (n>500) {printf("%d\n", ans); return 0;}
for (auto &e:E){
//printf(" %d %d\n", e.v, e.w);
P.clear();
assert(dfs0(e.v, e.w));
on[e.i] = 1, cur += W[e.i];
int tval = 1e9;
for (auto &x:P){
on[x] = 0, cur -= W[x];
ans = min(ans, cur*2 - calc(0, n));
tval = min(tval, cur*2 - calc(0, n));
on[x] = 1, cur += W[x];
}
///cycle
on[e.i] = 0;
vector<int> deep;
int len = W[e.i] + P.size();
for (auto &x:P) on[x] = 0;
tm++;
for (int i=0;i<n;i++) if (visited[i]!=tm){
ans = min(ans, cur*2-len-calc(i, n));
}
///
for (auto &x:P) on[x] = 1;
on[e.i] = 0, cur -= W[e.i];
}
printf("%d\n", ans);
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(int, int, int)':
Main.cpp:27:18: error: reference to 'tm' is ambiguous
27 | visited[s] = tm;
| ^~
In file included from /usr/include/time.h:39,
from /usr/include/c++/10/ctime:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:49,
from Main.cpp:1:
/usr/include/x86_64-linux-gnu/bits/types/struct_tm.h:7:8: note: candidates are: 'struct tm'
7 | struct tm
| ^~
Main.cpp:25:22: note: 'int tm'
25 | int visited[500500], tm;
| ^~
Main.cpp: In function 'int main()':
Main.cpp:85:9: error: reference to 'tm' is ambiguous
85 | tm++;
| ^~
In file included from /usr/include/time.h:39,
from /usr/include/c++/10/ctime:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:49,
from Main.cpp:1:
/usr/include/x86_64-linux-gnu/bits/types/struct_tm.h:7:8: note: candidates are: 'struct tm'
7 | struct tm
| ^~
Main.cpp:25:22: note: 'int tm'
25 | int visited[500500], tm;
| ^~
Main.cpp:86:47: error: reference to 'tm' is ambiguous
86 | for (int i=0;i<n;i++) if (visited[i]!=tm){
| ^~
In file included from /usr/include/time.h:39,
from /usr/include/c++/10/ctime:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:49,
from Main.cpp:1:
/usr/include/x86_64-linux-gnu/bits/types/struct_tm.h:7:8: note: candidates are: 'struct tm'
7 | struct tm
| ^~
Main.cpp:25:22: note: 'int tm'
25 | int visited[500500], tm;
| ^~
Main.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d %d %d", &x, &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~