大数据量处理dijkstra

针对项目交通网络的大数据量写了个算法,用到了不少数据结构,最近有些迷恋于结构体的使用,希望能尽量减少空间的浪费,所以本来简单的程序在这里看起来有些啰嗦复杂。

#include <iostream>

#include <fstream>

#include <hash_map>

#include <string>

#include <stdlib.h>

#include <queue>

using namespace std;

using namespace stdext;

struct DisNode{

  int node;

  double cost;

  DisNode *next;

};

struct OriNode{

  int node;

  int edgeNum;

  DisNode *first;

};

double INFINIT = 99999999;

int NODE_NUM;

int main()

{

  cout<<"test begin"<<endl;

  const char* AListFile = "AdjacencyList.txt";

  const char* LCostFile = "LinkCost.txt";

  const int MIN = 800000000;

  NODE_NUM = 0;

  hash_map<int,double> LCostHash;

  hash_map<int,OriNode*> NodeHash;

  ifstream if_AList;

  ifstream if_LCost;

  if_AList.open(AListFile);

  if_LCost.open(LCostFile);

  if(if_AList.is_open()&&if_LCost.is_open())

  {

 cout<<"file is opened"<<endl;

 

 int Lid = 0;

 double cost = 0.000;

 char line[30];

//save cost

 while(!if_LCost.eof())

 {

 if_LCost.getline(line,30);

 char* str = strstr(line,",");

// cout<<str<<endl;

          int size = str - line;

          char LidC[10];

 strncpy(LidC,line,size);

 LidC[size] = '/0';

          char*CostC = str +1;

          Lid = atoi(LidC);

 cost = atof(CostC);

 cout<<Lid<<"  "<<cost<<endl;

 LCostHash[Lid] = cost;

 }

//save cost

 cout<<"hashtable is saved"<<endl;

//save node info

 

   while(!if_AList.eof())

   {

     NODE_NUM ++;

int node;

int edgeNum;

OriNode* orinode = (OriNode *)malloc(sizeof(OriNode));

     if_AList>>node;

     if_AList>>edgeNum;

orinode->node = node;

orinode->edgeNum = edgeNum;

DisNode *disnodeTemp = (DisNode *)malloc(sizeof(DisNode)) ;

int dis_node;

if_AList>>dis_node;

disnodeTemp->node = dis_node;

     int cid;

if_AList>>cid;

disnodeTemp->cost = LCostHash[cid];

orinode->first = disnodeTemp;

cout <<orinode->node<<"  "<<orinode->edgeNum<<"  ";

     for (int j = 1; j < orinode->edgeNum; j++)

       {

          DisNode *disnode = (DisNode *)malloc(sizeof(DisNode));

 if_AList>>disnode->node;

 cout <<disnode->node<<"  ";

          int cid;

     if_AList>>cid;

     disnode->cost = LCostHash[cid];

 disnodeTemp->next = disnode;

 disnodeTemp = disnode;

       }

NodeHash[orinode->node] = orinode;

cout<<endl;

   }

//save node info

//测试保存结果

cout<<LCostHash[2]<<endl;

cout<<((OriNode*)NodeHash[3])->node<<endl;  

//测试保存结果

if_AList.close();

if_LCost.close();

}

//dijkstra

  int oriID;

  int desID;

  cout<<"Please enter the original node id: ";

  cin>>oriID;

  cout<<endl;

  cout<<"Please enter the destination node id: ";

  cin>>desID;

  cout<<endl;

  cout<<"wait..."<<endl;

  queue<int> QNode;

  hash_map<int,double> QCostHash;

  for (int n = 0; n < 1000; n++)

  {

  QCostHash[n] = INFINIT;

  }

  QCostHash[oriID] = 0;

  QNode.push(oriID);

  while(!QNode.empty())

  {

 int uNode = QNode.front();

 QNode.pop();

 if(uNode == desID)

 {

 break;

 }

 OriNode * uOriNode = (OriNode*)NodeHash[uNode];

 DisNode* uDisNode = uOriNode->first;

 for (int m = 0; m < uOriNode->edgeNum; m++)

 {

 int alt = QCostHash[uNode] + uDisNode->cost;

 if(alt < QCostHash[uDisNode->node])

 {

 QCostHash[uDisNode->node] = alt;

 }

 QNode.push(uDisNode->node);

 uDisNode = uDisNode->next;

 }       

  }

  cout<<"The result is :"<<QCostHash[desID]<<endl;

 

//dijkstra

int test;

cin>>test;

  return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值