【最短路】hdu2544 dijskstra+堆优化

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF 9999999
#define maxn 101

int i,j,n,m,map[maxn][maxn],mark[maxn],a,b,c,dis[maxn];

typedef pair<int,int> ele;

void clear(){
     memset(mark,0,sizeof(mark));
     for (i=1;i<=n;i++) 
       for (j=1;j<=n;j++) map[i][j]=INF;
 }


void Dijkstra( ){
     for (i=2;i<=n;i++) dis[i]=INF;
     dis[1]=0;
     priority_queue<ele,vector<ele>,greater<ele> >q;
     q.push(make_pair(dis[1],1));
     while (!q.empty()){
       ele t=q.top();
       q.pop();
       int now=t.second;
       if (mark[now]) continue;
       mark[now]=1;
       for (j=2;j<=n;j++)
         if (!mark[j] && map[now][j]<INF && dis[j]>dis[now]+map[now][j])
         {
             dis[j]=dis[now]+map[now][j];
             q.push(make_pair(dis[j],j));
         }
       }
}
int main(){
    while(scanf("%d%d",&n,&m)&& n && m){
    clear();
    for (i=1;i<=m;i++){
          scanf("%d%d%d",&a,&b,&c);
          map[a][b]=c;
          map[b][a]=c;
          }
    Dijkstra( );
    printf("%d\n",dis[n]);
    }
}

你可能感兴趣的:(【最短路】hdu2544 dijskstra+堆优化)