静态邻接表dijkstra
http://www.cnblogs.com/wuying/archive/2008/08/04/1259841.html
//使用静态连接表实现dijkstra
//路径无穷远,则返回-1;否则返回最短路径,
#include <iostream>
#include <queue>
using namespace std;
typedef struct
{
long v;
long next;
long cost;
}Edge;
typedef struct
{
long v;
long cost;
}node;
bool operator <(const node &a,const node &b)
{
return a.cost>b.cost;
}
const long MAXN=100010;
priority_queue<node> q;
long p[MAXN];//MAIN函数里元素初始化为-1;
Edge e[2*MAXN];//静态邻接表
bool vist[MAXN];
long Dijkstra(long Start,long End)//开始点,结束点
{
memset(vist,0,sizeof(vist));
while (!q.empty())
{
q.pop();
}
long cost=0;
node t;
t.cost=0;
t.v=Start;
q.push(t);
while (!q.empty())
{
t=q.top();
q.pop();
if (t.v==End)
{
break;
}
if (vist[t.v])
{
continue;
}
vist[t.v]=true;
long j;
for (j=p[t.v];j!=-1;j=e[j].next)
{
if (!vist[e[j].v])
{
node temp;
temp.v=e[j].v;
temp.cost=e[j].cost+t.cost;
q.push(temp);
}
}
}
if (t.v==End)
{
cost=t.cost;
}
else
{
cost=-1;
}
return cost;
}