hdu 2680Choose the best route 最短路(加入一个0点) BFS + priority——queue

/*
 题目很简单就是求最短路,其实跟hdu 2066差不多的
 加入一个0点,把kiki能到的s点与0点相连,map[0][p] = 0;
 接下来就是很简单的求最短路,注意这个是有向图
*/
#include <iostream>//2566318 2010-07-02 15:15:37 Accepted 2680 234MS 4252K 1410 B C++ 悔惜晟
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const int M = 1005;
const int MAX = 0x3fffffff;
int dis[M];
int map[M][M];
int n, s;
bool hash[M];

struct node
{
 int num;
 int dis;
 friend bool operator < (node a, node b)
 {
  return a.dis > b.dis;
 }
};

void BFS()
{
 priority_queue<node> Q;
 node N, P;
 for(int i = 1; i <= n; i++)
 {
  if(map[0][i] != MAX)
  {
   N.num = i;
   N.dis = 0;
   Q.push(N);
  }
 }
 memset(hash, false, sizeof(hash));
 hash[0] = true;
 dis[0] = 0;
 while(!Q.empty())
 {
  P = Q.top();
  Q.pop();
  hash[P.num] = true;
  for(int j = 1; j <= n; j++)
  {
   if(!hash[j] && map[P.num][j] + P.dis < dis[j])
   {
    dis[j] = map[P.num][j] + P.dis;
    N.dis = dis[j];
    N.num = j;
    Q.push(N);

   }
  }

 }
 if(dis[s] == MAX)
  printf("-1/n");
 else
  printf("%d/n", dis[s]);

}

int main()
{
 int m, w;
 while(scanf("%d %d %d", &n, &m, &s) != EOF)
 {
  int a, b, t;
  for(int i = 0; i < M; i++)
  for(int j = 0; j < M; j++)
  {
   map[i][j] = MAX;
  }
  while(m--)
  {
   scanf("%d %d %d", &a, &b, &t);
   if(t < map[a][b])
   {
    map[a][b] = t ;//map[b][a] = t;
   }//means from station p to station q there is a way and it will costs t minutes .
  }        //一开始就错在这句话上。
  scanf("%d", &w);
  while(w--)
  {
   scanf("%d", &a);
   map[0][a] = map[a][0] = 0;
  }
  for(int i = 0; i <= n; i++)
   dis[i] = map[0][i];
  BFS();

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值