#include<random>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<climits>
//#define NDEBUG
#include<cassert>
#include<cstring>
#include<complex>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<bitset>
//#define LL __int128
#define LL long long
#define ULL unsigned LL
#define uint unsigned int
//#define int LL
//#define double long double
#define mkp make_pair
#define par pair<int,int>
#define psb push_back
#define epb emplace_back
#define f(x) ((x).first)
#define s(x) ((x).second)
using namespace std;
#define Lbt(x) ((x)&(-(x)))
#define Swap(x,y) (x^=y^=x^=y)
const int Mxxx=1e5;
inline char gc()
{
// return getchar();
static char buf[Mxxx],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxxx,stdin),p1==p2)?EOF:*p1++;
}
inline char pc(char ch,bool fl=false)
{
// return fl?0:putchar(ch),0;
static char buf[Mxxx],*p1=buf,*p2=buf+Mxxx;
return (fl||((*p1++=ch)&&p1==p2))&&(fwrite(buf,1,p1-buf,stdout),p1=buf),0;
}
#define output pc('!',true)
inline int read()
{
char ch=gc();
int gans=0,gflag=0;
for(;ch<'0'||'9'<ch;gflag|=ch=='-',ch=gc());
for(;'0'<=ch&&ch<='9';gans=(gans<<1)+(gans<<3)+(ch^48),ch=gc());
return gflag?-gans:gans;
}
template<typename T>
inline char read(T&gans)
{
char ch=gc();
int gflag=0;gans=0;
for(;ch<'0'||'9'<ch;gflag|=ch=='-',ch=gc());
for(;'0'<=ch&&ch<='9';gans=(gans<<1)+(gans<<3)+(ch^48),ch=gc());
return gans=gflag?-gans:gans,ch;
}
template<typename T>
inline void write(T x)
{
if(x>9)write(x/10);
pc(x%10^48);
}
template<typename T>
inline void writenum(T x,char ch)
{
if(x<0)pc('-'),x=-x;
write(x);pc(ch);
}
inline void writechar(char x,char ch)
{
pc(x);pc(ch);
}
template<typename T>
inline T Max(T x,T y)
{
return x>y?x:y;
}
template<typename T>
inline T Min(T x,T y)
{
return x<y?x:y;
}
template<typename T>
inline T Abs(T x)
{
return x<0?-x:x;
}
template<typename T>
inline void ckmx(T&x,T y)
{
x=Max(x,y);
}
template<typename T>
inline void ckmn(T&x,T y)
{
x=Min(x,y);
}
const int M=15e2,Mx=3e5+M;
int n,m,cnt,h[Mx+5],nxt[Mx+5],tto[Mx+5],val[Mx+5];
int rd[Mx+5],cd[Mx+5];
inline void ade(int x,int y,int v)
{
rd[y]++;cd[x]++;
nxt[++cnt]=h[x];
tto[h[x]=cnt]=y;
val[cnt]=v;
}
int pr[Mx+5];LL vl[Mx+5];
inline int Gf(int x)
{
if(pr[x]==x)return x;
int p;
pr[x]=Gf(p=pr[x]);
vl[x]+=vl[p];
return pr[x];
}
int tot,H[Mx+5],Nx[Mx+5],To[Mx+4];
LL Vl[Mx+5];
inline void adeg(int x,int y,LL v)
{
Nx[++cnt]=H[x];
To[H[x]=cnt]=y;
Vl[cnt]=v;
}
int vst[Mx+5];LL ds[Mx+5];
priority_queue<pair<LL,int> >q;
inline void Dij(int s)
{
int i,x,to;
for(i=1;i<=tot;i++)ds[i]=LLONG_MAX,vst[i]=0;
ds[s]=0;
q.push(mkp(-ds[s],s));
for(;!q.empty();)
{
x=s(q.top());q.pop();
if(vst[x])continue;
vst[x]=1;
for(i=H[x];i;i=Nx[i])
{
if(ds[to=To[i]]>ds[x]+Vl[i])q.push(mkp(-(ds[to]=ds[x]+Vl[i]),to));
}
}
}
int fl[Mx+5],trs[Mx+5];
LL ans;
signed main()
{
#ifndef ONLINE_JUDGE
// freopen("_.in","r",stdin);
freopen("__.in","r",stdin);
// freopen("_.out","w",stdout);
#endif
int i,j,x,y,to;ans=LLONG_MAX;
n=read();m=read();
for(i=1;i<=n;i++)pr[i]=i,vl[i]=0;
for(i=1;i<=m;i++)
{
x=read();y=read();
if(x==y)ckmn(ans,(LL)read());
else ade(x,y,read());
}
for(x=1;x<=n;x++)
{
if(rd[x]==1&&cd[x]==1)
{
y=tto[h[x]];
if(Gf(y)==x)
{
ckmn(ans,vl[y]+val[h[x]]);
fl[x]=1;
}
else
{
pr[x]=y;vl[x]=val[h[x]];
}
}
}
for(x=1;x<=n;x++)
{
if(!rd[x]||!cd[x])continue;
if(!fl[x]&&Gf(x)==x)
{
trs[x]=++tot;
}
}cnt=0;
for(i=1;i<=n;i++)
{
if((x=trs[i]))
{
for(j=h[i];j;j=nxt[j])
{
if((y=trs[Gf(to=tto[j])]))
adeg(x,y,val[j]+vl[to]);
}
}
}
for(i=1;i<=tot;i++)
{
Dij(i);
for(x=1;x<=tot;x++)if(ds[x]^LLONG_MAX)for(j=H[x];j;j=Nx[j])if(To[j]==i)ckmn(ans,ds[x]+Vl[j]);
}
writenum(ans==LLONG_MAX?-1:ans,10);
return output;
}
/*
4 6
1 2 1
4 3 3
4 1 9
2 4 1
3 1 2
3 2 6
*/
/*
1 0
*/
/*
1 1
1 1 1
*/剪贴板
剪贴板 68acfbcl
剪贴板信息
剪贴板信息
- 收录
- 更新