时间限制: 1 Sec 内存限制: 64 MB
题目描述
All one knows Jesse live in the city , but he must come to Xiasha twice in a week. The road is too long for him to go . But he is so much resolute that nothing can prevent him from coming . More, Jesse must take many bus to come , the length of road is different. So, Jesse think : can I take the shortest road to save time ? Now , the problem is puting on you , can you help him to calculate the shortest length? Is it easy? Just do it!
输入
The input has several test cases. The first line of each case has two number n,m (1 <= n,m <= 200 ). n means have n bus stations 1th,2th,....nth . m means have m roads. Then following next m lines ,each line have 3 integer a,b,c which means the length between a and b bus station is c(0 < c < 2000). Then a line with two integer s,t,means the start and end bus station.
输出
For the given start and end bus station ,output the shortest lenth between s and t in one line. If the road not exists,output -1.
样例输入
3 3
1 2 3
1 3 10
2 3 11
1 3
3 1
2 3 1
1 2
样例输出
10-1
提示
来源
经典Floyd
#include <bits/stdc++.h>
#define cl(x) memset(x,0,sizeof(x))
#define _cl(a,b) memset(a,0,(b+1)<<2)
#define At(x,y) memset(x,y,sizeof x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define vct vector
#define pq priority_queue
#define all(x) x.begin(),x.end()
#define int long long
#define fi first
#define se second
//#pragma GCC optimize(2)
using namespace std;
inline int min(int a,int b) {return a<b?a:b;}
inline int max(int a,int b) {return a>b?a:b;}
//map<string,map<string,int> > mp;
//pq <ll,vct<ll>,less<ll> > now,last;
//vct<vct<int> > vt(n+1,vct<int>(m+1,0))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const int MOD=998244353;
const int N=2e2+10;
int n,m;
int d[N][N];
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
void solve()
{
while(cin>>n>>m){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i==j) d[i][j]=0;
else d[i][j]=inf;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
d[a][b]=min(d[a][b],c);
d[b][a]=min(d[b][a],c);
}
floyd();
int a,b;
cin>>a>>b;
int t=d[a][b];
if(t>(inf>>1)) cout<<"-1\n";
else cout<<t<<"\n";
}
return ;
}
signed main()
{
// srand(time(NULL));
// freopen("2022_XinShengSai_1.in", "r", stdin);
// freopen("2022_XinShengSai_1.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
int t;
// cin>>t;
t=1;
while(t--)
solve();
return 0;
}
/*
*/