HDU-2544单源最短路

    没什么感想写的,一套便过,似乎。。

#include <iostream>
#include <cstdio>

#include <cstring>

#include <algorithm>

#include <map>

#include <vector>

#include <queue>

#include <stack>

#define maxn 100005

#define INF 999999999

using namespace std;                          //套模板。。

struct edge

{

   int to;

   int cost;

   int from;

};
//typedef pair<int, int> P;

int V,E;

edge es[maxn];

int d[maxn];

void shortest_path(int s)

{

  for(int i = 1;i <=V; i++)

    d[i] = INF;

  

  d[s] = 0;

  while(true)

  {

    bool update = false;

    for(int i = 1; i <= 2*E; i ++)

    {

      edge e = es[i];

      //printf("%d %d %d\n",e.from, e.to, e.cost);

      //printf("%d %d \n",d[e.to],d[e.from]);

      if(d[e.from] != INF && d[e.to] > d[e.from]+e.cost)

      {

        d[e.to] = d[e.from]+e.cost;

        //printf("%d\n",e.cost);

        update = true;

      }

    }

    if(!update)break;

  }

}

int main()

{

    int i, a;

    edge b;

    while(~scanf("%d%d",&V,&E))

    {

       if(V==0&&E==0)break;

       for(i= 1;i <= 2*E; i++)

       {

         scanf("%d%d%d",&es[i].from, &es[i].to, &es[i].cost);    //本题为无向图

         i++;

         es[i].from = es[i-1].to;

         es[i].to = es[i-1].from;

         es[i].cost = es[i-1].cost;

         

         //G[a].push_back(b);

         //printf("%d %d",b.to,b.cost);  

       }

       

       shortest_path(1);

       //for(i = 0; i <= V; i ++)

        //printf("%d\n",d[i]);

       

       printf("%d\n",d[V]);

      

    }

    return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值