提交 74168179 编写于 作者: 道标 · 年's avatar 道标 · 年 🙂

删除 迪杰斯特拉.cpp

上级 35ace137
#include <iostream>
#include <vector>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e3+10;
int d[N],vs[N];
struct Edge{
int to;
int w;
};
vector<Edge>edge[N];
int n,m;
void add(int u,int v,int w){
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
int main(){
while(cin>>n>>m&&n!=0&&m!=0){
int u,v,w;
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
add(u,v,w);
}
for(int i=1;i<=n;i++){
d[i]=INF;
vs[i]=0;
}
d[1]=0;
for(int i=1;i<=n;i++){
int x=0,m=INF;
for(int j=1;j<=n;j++){
if(!vs[j]&&d[j]<=m){
x=j;
m=d[j];
}
}
vs[x]=1;
for(int j=0;j<edge[x].size();j++){
d[edge[x][j].to]=min(d[edge[x][j].to],d[x]+edge[x][j].w);
}
}
cout<<d[n]<<endl;
memset(edge,0,sizeof(edge));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册