hdu2544

#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=105;
int n,m;
struct edge
{
    int from,to,w;
};

struct node
{
    int id,dis;
    bool operator<(const node &a) const
    {
        return dis>a.dis;
    }
};

vector<edge>e[maxn];
void dijkstra()
{
    int s=1;
    node t,u;
    priority_queue<node>q;
    int dis[maxn];
    bool done[maxn];
    for(int i=1;i<=n;i++)
    {
        dis[i]=9999999;
        done[i]=false;
    }

    dis[s]=0;
    t.id=s;
    t.dis=dis[s];
    q.push(t);

    while(!q.empty())
    {
        u=q.top();
        q.pop();
        if(done[u.id]) continue;
        done[u.id]=true;
        for(int i=0;i<e[u.id].size();i++)
        {
            edge y=e[u.id][i];
            if(done[y.to]) continue;
            if(dis[y.to]>y.w+u.dis)
            {
                dis[y.to]=y.w+u.dis;
                t.id=y.to;
                t.dis=dis[y.to];
                q.push(t);
            }
        }
    }

    cout<<dis[n]<<endl;
}

int main()
{
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break;
        int i,j;
        int a,b,c;
        edge t;
        for(i=1;i<=n;i++)
        {
            e[i].clear();
        }

        for(i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            t.from=a;
            t.to=b;
            t.w=c;
            e[a].push_back(t);

            t.from=b;
            t.to=a;
            e[b].push_back(t);
        }

        dijkstra();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值