hdu2544基于堆优化(优先队列)的最短路径。变简单了

#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;
	}
	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值