#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=2000;
int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int n,m,p;
void dj(int u) {
int start =u;
memset(vis,false,sizeof(vis));
memset(dis,inf,sizeof(dis));
dis[start]=0;
vis[start]=1;
for(int i=1; i<=n; i++) {
dis[i]=mp[start][i];
}
for(int time=1; time<=n-1; time++) {
int minn=inf;
p=1;
for(int i=1; i<=n; i++) {
if(dis[i]<minn&&vis[i]==0) {
p=i;
minn=dis[i];
}
}
vis[p]=1;
for(int i=1; i<=n; i++) {
dis[i]=min(dis[i],dis[p]+mp[p][i]);
}
}
}
int main() {
int a,b,g;
int x,y;
while( cin>>n>>m) {
memset(mp,inf,sizeof(mp));
for(int i=1; i<=n; i++) {
mp[i][i]=0;
}
while(m--) {
cin>>a>>b>>g;
mp[a][b]=g;
mp[b][a]=g;
}
cin>>x>>y;
dj(x);
if(dis[y]!=inf)
printf("%d\n",dis[y]);
else
printf("No path\n");
}
}
算法分析与设计(实验2Dijkstra算法)
最新推荐文章于 2022-07-09 16:42:30 发布