#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=105;
int n,m,head[maxn],cnt,vis[maxn],dis[maxn];
struct node{
int id,d;
friend bool operator < (node a,node b) {
return a.d<b.d;
}
};
struct edge{
int to,w,nex;
}e[maxn*maxn];
//因为是建边,所以结构体e[]是对边的
void adde(int u,int v,int w){
e[++cnt].to=v;
e[cnt].w=w;
e[cnt].nex=head[u];
head[u]=cnt;
}
int main()
{
int a,b,c;
while(cin>>n>>m){
if(n==0&&m==0) break;
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(head,0,sizeof(head));
cnt=0;
for(int i=0;i<m;i++){
cin>>a>>b>>c;
adde(a,b,c);
adde(b,a,c);
}
priority_queue <node> q;
node cur;
cur.id=1,cur.d=0;
q.push(cur);
dis[1]=0;
while(!q.empty()){
cur=q.top(); q.pop();
int u=cur.id;
if(vis[u])continue;
vis[u]=1;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
node tem;tem.id=v,tem.d=dis[v];
q.push(tem);
}
}
}
cout<<dis[n]<<endl;
}
}
hdu2544基于堆优化(优先队列)的最短路径。变简单了
最新推荐文章于 2024-08-08 15:12:59 发布