hdu 1874

//2577833 2010-07-08 14:29:13 Accepted 1874 15MS 284K 1445 B C++ T&T
#include<iostream>
#include<cstdio>
#include<queue>
#define MAX 0x7fffffff
#define asize 210
using namespace std;
struct node{
 node(int vv=0,int dd=0):vexn(vv),dist(dd){}
 int vexn;
  int dist;
 friend bool operator<(node a,node b)
 {
  return a.dist > b.dist;
 }    
};
bool hash[asize];
int dis[asize],cost[asize][asize];
int count1;
int djkstra(int v0,int n,int t)
{  
 priority_queue<node> q;
 int i;
 for(i = 0; i < n; i++)
 {  
  dis[i] = MAX;
  //path[i] = -1;
 }
 memset(hash,true, sizeof(hash));
 dis[v0] = 0;
 //path[v0] = 0;
 q.push(node(v0,dis[v0]));
 node N;
 while(!q.empty())
 {
  N = q.top();
  q.pop();
  if(!hash[N.vexn])continue;
  hash[N.vexn] = false;
  for(i = 0; i < n; i++)
  {
   if(hash[i] && N.dist + cost[N.vexn][i] < dis[i]&& cost[N.vexn][i] < MAX)
   {
    dis[i] = N.dist + cost[N.vexn][i];
    //path[i] = N.vexn;
    q.push(node(i,dis[i]));
   }
  }
 }
 if(dis[t] == MAX)
 return -1;
 return dis[t];
}
int main()
{
 int n,i,w,m,b,j,a;
 int s,t;
 while(scanf("%d%d",&n,&m) != EOF)
 {
  count1 = 0;
  for(i = 0; i < n; i++)
  for(j = 0; j < n; j++)
  {
   if(i == j)
   cost[i][j] = 0;
   else
   cost[i][j] = MAX;
  }
  for(i = 0; i < m; i++)
  {
   scanf("%d%d%d",&a,&b,&w);
   if(w < cost[a][b])
   {
    cost[a][b] = w;
    cost[b][a] = w;
   }
  }
  scanf("%d%d",&s,&t);
  int res = djkstra(s,n,t);
  printf("%d/n",res);
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值