1. 程式人生 > >最短路徑的Dijkstra演算法(鄰接表)

最短路徑的Dijkstra演算法(鄰接表)

描述     以鄰接表作為儲存結構實現,求解從給定源點到給定結束點的最短路徑。 輸入
從1開始表示第一個節點。
第一行輸入:頂點數n(2<=n<=100),邊數m(2<=m<=100)
第二行輸入有向邊:起始點s1,結束點 s2,邊權值 w
第三行輸入:源點start,終點end
輸出 若存在路徑,輸出路徑長度;
若不存在,輸出-1。 輸入樣例 6 8
1 6 100
1 5 30
1 3 10
2 3 5
3 4 50
4 6 10
5 4 20
5 6 60
1 6 輸出樣例

60

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define maxnum 120
#define INF 10000000

using namespace std;

typedef char VertexType;
//邊
typedef struct ArcNode
{
    int adjvex;
    int weight;
    struct ArcNode *nextarc;
}ArcNode;
//頂點
typedef struct VNode
{
    VertexType data;
    ArcNode *firstarc;
}VNode, AdjList[maxnum];

typedef struct
{
    AdjList vertices;//陣列
    int vexnum, arcnum;
}ALGraph;

//頂點節點,儲存id和到源頂點的估算距離,優先佇列需要的型別
struct Node
{
    int id;//源頂點id
    int w;//估算距離

    //因要實現最小堆,按升序排列,因而需要過載運算子,重定義優先順序,以小為先
    friend bool operator < (struct Node a, struct Node b)
    {
        return a.w > b.w;
    }
};

int path[maxnum];
int visited[maxnum] = {0};
Node dist[maxnum];
priority_queue<Node>q;

void Dijkstra(ALGraph g, int v0, int n)
{
    //初始化

    for(int i = 1; i <= n; i++)
    {
        dist[i].id = i;
        dist[i].w = INF;
        path[i] = -1;       //每個頂點都無父親節點
        visited[i] = 0;     //都未找到最短路
    }
    dist[v0].w = 0;
    q.push(dist[v0]);
    while(!q.empty())
    {
        Node cd = q.top();
        q.pop();
        int u = cd.id;

        if(visited[u])
            continue;
        visited[u] = 1;
        ArcNode *p = g.vertices[u].firstarc;

        while(p)
        {
            int tempv = p->adjvex;
            int tempw = p->weight;

            if(!visited[tempv] && dist[tempv].w > dist[u].w+tempw)
            {
                dist[tempv].w = dist[u].w+tempw;
                path[tempv] = u;
                q.push(dist[tempv]);
            }
            p = p->nextarc;
        }
    }
}

void CreateALGraph(ALGraph &g, int arc, int vex)
{
    g.arcnum = arc;
    g.vexnum = vex;
    int v1, v2, i, w;

	for(i = 1; i <= vex; i++)
	{
		g.vertices[i].firstarc = NULL;
	}
    for(i = 1; i <= arc; i++)
    {
        cin >> v1 >> v2 >> w;
        ArcNode *q = (ArcNode*)malloc(sizeof(ArcNode));
        q->adjvex = v2;
        q->weight = w;

		q->nextarc = g.vertices[v1].firstarc;
		g.vertices[v1].firstarc = q;
    }
}
int DFS(ALGraph g, int i, int j)
{
    visited[i] = 1;
	ArcNode *p = g.vertices[i].firstarc;
	while(p)
	{
		if(p->adjvex == j)
			return 1;
        //cout <<(visited[p->adjvex])<< endl;
		if(!(visited[p->adjvex]) && DFS(g, p->adjvex, j))
			return 1;
		p = p->nextarc;
	}
	return 0;
}

int BFS(ALGraph g, int i, int j)
{
    queue<int>q;//
    q.push(i);
    visited[i] = 1;
    ArcNode *p;
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        p = g.vertices[temp].firstarc;

        while(p)
        {
            //cout << p->adjvex;
            if(p->adjvex == j)
                return 1;
            if(!(visited[p->adjvex]))
            {
                visited[p->adjvex] = 1;
                q.push(p->adjvex);
            }
            p = p->nextarc;
        }
    }
    return 0;//返回不可少
}
int main()
{
    int m, n;
    //頂點,邊
    cin >> n >> m;
    ALGraph g;
    CreateALGraph(g, m, n);

//    for(int i = 1; i <= n; i++)
//    {
//        ArcNode *p = g.vertices[i].firstarc;
//        cout << "i = "  << i << ": ";
//        while(p)
//        {
//            cout << p->adjvex;
//            p = p->nextarc;
//        }
//		cout << endl;
//    }
    int v0, ve;
    cin >> v0 >> ve;
    Dijkstra(g, v0, n);
    if(dist[ve].w != INF)
        cout << dist[ve].w << endl;
    else
        cout << -1 <<endl;

    return 0;
}