floyd找最小环
很好理解
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define LLin(x) scanf("%lld",&x)
#define op operator
#define CSC main
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
cint inf=;
void inin(int &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret*=,ret+=ch-'',ch=getchar();
ret=f?-ret:ret;
}
int n,m,t,dis[][],mp[][];
int floyd()
{
int ret=inf;
re(k,,n)
{
re(i,,n)if(i!=k)
re(j,,n)if(j!=k&&j!=i)
if(dis[i][j]<inf&&mp[i][k]!=inf&&mp[k][j]!=inf)
ret=min(ret,mp[i][k]+mp[k][j]+dis[i][j]);
re(i,,n)
re(j,,n)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
return ret;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
re(i,,n)re(j,,m)dis[i][j]=mp[i][j]=inf;
re(i,,m)
{
int q,w,e;inin(q),inin(w),inin(e);
dis[q][w]=dis[w][q]=mp[q][w]=mp[w][q]=min(dis[q][w],e);
}
int ans=floyd();
if(ans==inf)printf("It's impossible.\n");
else printf("%d\n",ans);
}
return ;
}