Bar to sample item: hdu1874
AC code is as follows:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=205;
int n,m;
int mp[maxn][maxn];
int main(){
while(cin>>n>>m){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j) mp[i][j]=0;
else mp[i][j]=INF;
}
}
for(int i=1;i<=m;i++){
int x,y,z;cin>>x>>y>>z;
mp[x][y]=min(z,mp[x][y]);
mp[y][x]=min(z,mp[y][x]);
}
int s,t;cin>>s>>t;
//Floyd的写法:
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
//止
if(mp[s][t]==INF) cout<<"-1"<<endl;
else cout<<mp[s][t]<<endl;
}
}
Here I explain Floyd algorithm, he is seeking the shortest path of two points, the use of the thinking here of dp.
Provided the starting point for the s, the end of t, a point passing (i.e., bypass point k)
the core key is dp [s] [t] = min (dp [s] [t], dp [s] [k] + dp [k] [t])
this is the state transition, and finally to deposit the minimum