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;
/*#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif*/
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define endl "\n"
typedef pair<int, int> Pii;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(x) begin(x), end(x)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(s) (s).begin(),(s).end()
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define FOR_(n) for (ll _ = 0; (_) < (ll)(n); ++(_))
#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define se second
#define pb push_back
#define P pair<ll,ll>
#define PQminll priority_queue<ll, vector<ll>, greater<ll>>
#define PQmaxll priority_queue<ll,vector<ll>,less<ll>>
#define PQminP priority_queue<P, vector<P>, greater<P>>
#define PQmaxP priority_queue<P,vector<P>,less<P>>
#define NP next_permutation
//const ll mod = 1000000009;
const ll mod = 998244353;
//const ll mod = 1000000007;
const ll inf = 4100000000000000000ll;
const ld eps = ld(0.00000000001);
static const long double pi = 3.141592653589793;
template<class T>void vcin(vector<T> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
template<class T,class K>void vcin(vector<T> &n,vector<K> &m){for(int i=0;i<int(n.size());i++) cin>>n[i]>>m[i];}
template<class T>void vcout(vector<T> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
template<class T>void vcin(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cin>>n[i][j];}}}
template<class T>void vcout(vector<vector<T>> &n){for(int i=0;i<int(n.size());i++){for(int j=0;j<int(n[i].size());j++){cout<<n[i][j]<<" ";}cout<<endl;}cout<<endl;}
void yes(bool a){cout<<(a?"yes":"no")<<endl;}
void YES(bool a){cout<<(a?"YES":"NO")<<endl;}
void Yes(bool a){cout<<(a?"Yes":"No")<<endl;}
void possible(bool a){ cout<<(a?"possible":"impossible")<<endl; }
void Possible(bool a){ cout<<(a?"Possible":"Impossible")<<endl; }
void POSSIBLE(bool a){ cout<<(a?"POSSIBLE":"IMPOSSIBLE")<<endl; }
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
template<class T>auto min(const T& a){ return *min_element(all(a)); }
template<class T>auto max(const T& a){ return *max_element(all(a)); }
template<class T,class F>void print(pair<T,F> a){cout<<a.fi<<" "<<a.se<<endl;}
template<class T>bool chmax(T &a,const T b) { if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a,const T b) { if (b<a) { a=b; return 1; } return 0;}
template<class T> void ifmin(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
template<class T> void ifmax(T t,T u){if(t>u){cout<<-1<<endl;}else{cout<<t<<endl;}}
ll fastgcd(ll u,ll v){ll shl=0;while(u&&v&&u!=v){bool eu=!(u&1);bool ev=!(v&1);if(eu&&ev){++shl;u>>=1;v>>=1;}else if(eu&&!ev){u>>=1;}else if(!eu&&ev){v>>=1;}else if(u>=v){u=(u-v)>>1;}else{ll tmp=u;u=(v-u)>>1;v=tmp;}}return !u?v<<shl:u<<shl;}
ll modPow(ll a, ll n, ll mod) { if(mod==1) return 0;ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
vector<ll> divisor(ll x){ vector<ll> ans; for(ll i = 1; i * i <= x; i++){ if(x % i == 0) {ans.push_back(i); if(i*i!=x){ ans.push_back(x / ans[i]);}}}sor(ans); return ans; }
ll pop(ll x){return __builtin_popcountll(x);}
ll poplong(ll x){ll y=-1;while(x){x/=2;y++;}return y;}
P hyou(P a){ll x=fastgcd(abs(a.fi),abs(a.se));a.fi/=x;a.se/=x;if(a.se<0){a.fi*=-1;a.se*=-1;}return a;}
P Pplus(P a,P b){ return hyou({a.fi*b.se+b.fi*a.se,a.se*b.se});}
P Ptimes(P a,ll b){ return hyou({a.fi*b,a.se});}
P Ptimes(P a,P b){ return hyou({a.fi*b.fi,a.se*b.se});}
P Pminus(P a,P b){ return hyou({a.fi*b.se-b.fi*a.se,a.se*b.se});}
P Pgyaku(P a){ return hyou({a.se,a.fi});}
template<class T>
struct Sum{
vector<T> data;
Sum(const vector<T>& v):data(v.size()+1){
for(ll i=0;i<v.size();i++) data[i+1]=data[i]+v[i];
}
T get(ll l,ll r) const {
return data[r]-data[l];
}
};
template<class T>
struct Sum2{
vector<vector<T>> data;
Sum2(const vector<vector<T>> &v):data(v.size()+1,vector<T>(v[0].size()+1)){
for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]=data[i][j+1]+v[i][j];
for(int i=0;i<v.size();i++) for(int j=0;j<v[i].size();j++) data[i+1][j+1]+=data[i+1][j];
}
T get(ll x1,ll y1,ll x2,ll y2) const {
return data[x2][y2]+data[x1][y1]-data[x1][y2]-data[x2][y1];
}
};
void cincout(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout<< fixed << setprecision(15);
}
vector<vector<int>> sh[11];
void solve(int n){
if(n==0){
sh[0].pb({});
return;
}
for(int i=0;i*2<=n;i++){
for(int z=0;z<=1;z++){
if(i*2+z>n) continue;
vector<int> c(n);
for(int j=0;j<i;j++){
c[2*j]=c[2*j+1]=j+1;
}
if(z) c[n-1]=-1;
sor(c);
do{
vector<int> r(i,-1);
for(int j=0;j<n;j++){
if(c[j]>=1&&r[c[j]-1]==-1) r[c[j]-1]=j;
}
vector<int> u=r;
sor(u);
if(u!=r) continue;
sh[n].pb(c);
}while(NP(all(c)));
}
}
}
ll dp[1000][5010];
vector<int> g[1000];
int d[1000],D[1000],par[1000];
vector<int> nz[1000];
vector<int> vs;
void dfs(int p,int q){
vs.pb(p);
for(auto e:g[p]){
if(e==q) continue;
d[e]=d[p]^1;
D[e]=D[p]+1;
par[e]=p;
dfs(e,p);
}
}
vector<int> a,b,c,A,B,C;
ll T=5000;
int f[1000];
vector<vector<int>> G[1000];
vector<vector<int>> GG[1000];
void dfs3(int p,int q,int x){
f[p]=x;
for(auto e:g[p]){
if(e==q) continue;
dfs3(e,p,x);
}
}
void dfs2(int p,int q){
vector<int> ch;
int x=0;
for(int i=0;i<1000;i++) f[i]=-1;
for(auto e:g[p]){
if(e==q) continue;
dfs3(e,p,x);
ch.pb(e);
x++;
}
int m=ch.size();
vector<vector<ll>> cs(m,vector<ll>(m,-inf/1000));
vector<vector<int>> V;
for(auto e:G[p]){
if(e[0]==p||e[1]==p){
V.pb(e);
}
else{
chmax(cs[f[e[0]]][f[e[1]]],dp[ch[f[e[0]]]][e[3]]+dp[ch[f[e[1]]]][e[3]]+e[2]);
chmax(cs[f[e[1]]][f[e[0]]],dp[ch[f[e[0]]]][e[3]]+dp[ch[f[e[1]]]][e[3]]+e[2]);
}
}
vector<ll> M(m);
for(int i=0;i<m;i++){
M[i]=dp[ch[i]][T];
for(auto e:nz[ch[i]]){
if(a[e]==p||b[e]==p) chmax(M[i],dp[ch[i]][e]+c[e]);
}
}
for(auto v:sh[m]){
ll sum=0;
ll idx=T;
vector<int> memo(6,-1);
for(int i=0;i<m;i++){
if(v[i]==0){
sum+=M[i];
}
else if(v[i]==-1){
idx=i;
}
else{
if(memo[v[i]]>=0){
sum+=cs[memo[v[i]]][i];
}
else{
memo[v[i]]=i;
}
}
}
// cout<<"PQ"<<" "<<p<<" "<<idx<<" "<<sum<<endl;
if(idx==T){
chmax(dp[p][T],sum);
}
else{
for(auto e:nz[ch[idx]]){
if(a[e]==p||b[e]==p){
chmax(dp[p][T],sum+dp[ch[idx]][e]+c[e]);
}
else{
chmax(dp[p][e],sum+dp[ch[idx]][e]);
}
}
}
}
// cout<<"K"<<" "<<p<<" "<<m<<" "<<sh[m].size()<<endl;
for(auto e:GG[p]){
// cout<<"P"<<" "<<p<<" "<<e[0]<<" "<<e[1]<<" "<<e[2]<<" "<<e[3]<<endl;
int OT=e[0]^e[1]^p;
if(f[OT]==-1){
chmax(dp[p][e[3]],dp[p][T]);
}
}
for(int i=0;i<5000;i++){
if(dp[p][i]>=0) nz[p].pb(i);
}
}
int main() {
cincout();
int n,m;
cin>>n>>m;
ll ans=0;
for(int i=0;i<m;i++){
int p,q,r;
cin>>p>>q>>r;
p--;
q--;
if(r){
a.pb(p);
b.pb(q);
c.pb(r);
}
else{
g[p].pb(q);
g[q].pb(p);
}
ans+=r;
}
dfs(0,-1);
for(int i=0;i<a.size();i++){
if(d[a[i]]==d[b[i]]){
A.pb(a[i]);
B.pb(b[i]);
C.pb(c[i]);
}
}
a=A,b=B,c=C;
for(int i=0;i<a.size();i++){
int X=a[i],Y=b[i];
GG[X].pb({a[i],b[i],c[i],i});
GG[Y].pb({a[i],b[i],c[i],i});
while(X!=Y){
if(D[X]<D[Y]) Y=par[Y];
else X=par[X];
}
G[X].pb({a[i],b[i],c[i],i});
}
for(int i=0;i<1000;i++){
for(int j=0;j<=5005;j++) dp[i][j]=-inf;
}
rever(vs);
for(int i=0;i<=10;i++) solve(i);
for(auto e:vs){
dfs2(e,par[e]);
}
// cout<<dp[4][1]<<endl;
//cout<<dp[2][1]<<endl;
cout<<ans-dp[0][T]<<endl;
}
Compilation message (stderr)
training.cpp: In function 'int main()':
training.cpp:250:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
250 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
training.cpp:258:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
258 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
# | 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... |
# | 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... |