最短路径的Dijkstra算法(邻接表)

原文:http://blog.csdn.net/axiqia/article/details/50984464
描述
    以邻接表作为存储结构实现,求解从给定源点到给定结束点的最短路径。
 
输入

从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


[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <cstring>  
  4. #include <queue>  
  5. #define maxnum 120  
  6. #define INF 10000000  
  7.   
  8. using namespace std;  
  9.   
  10. typedef char VertexType;  
  11. //边  
  12. typedef struct ArcNode  
  13. {  
  14.     int adjvex;  
  15.     int weight;  
  16.     struct ArcNode *nextarc;  
  17. }ArcNode;  
  18. //顶点  
  19. typedef struct VNode  
  20. {  
  21.     VertexType data;  
  22.     ArcNode *firstarc;  
  23. }VNode, AdjList[maxnum];  
  24.   
  25. typedef struct  
  26. {  
  27.     AdjList vertices;//数组  
  28.     int vexnum, arcnum;  
  29. }ALGraph;  
  30.   
  31. //顶点节点,保存id和到源顶点的估算距离,优先队列需要的类型  
  32. struct Node  
  33. {  
  34.     int id;//源顶点id  
  35.     int w;//估算距离  
  36.   
  37.     //因要实现最小堆,按升序排列,因而需要重载运算符,重定义优先级,以小为先  
  38.     friend bool operator < (struct Node a, struct Node b)  
  39.     {  
  40.         return a.w > b.w;  
  41.     }  
  42. };  
  43.   
  44. int path[maxnum];  
  45. int visited[maxnum] = {0};  
  46. Node dist[maxnum];  
  47. priority_queue<Node>q;  
  48.   
  49. void Dijkstra(ALGraph g, int v0, int n)  
  50. {  
  51.     //初始化  
  52.   
  53.     for(int i = 1; i <= n; i++)  
  54.     {  
  55.         dist[i].id = i;  
  56.         dist[i].w = INF;  
  57.         path[i] = -1;       //每个顶点都无父亲节点  
  58.         visited[i] = 0;     //都未找到最短路  
  59.     }  
  60.     dist[v0].w = 0;  
  61.     q.push(dist[v0]);  
  62.     while(!q.empty())  
  63.     {  
  64.         Node cd = q.top();  
  65.         q.pop();  
  66.         int u = cd.id;  
  67.   
  68.         if(visited[u])  
  69.             continue;  
  70.         visited[u] = 1;  
  71.         ArcNode *p = g.vertices[u].firstarc;  
  72.   
  73.         while(p)  
  74.         {  
  75.             int tempv = p->adjvex;  
  76.             int tempw = p->weight;  
  77.   
  78.             if(!visited[tempv] && dist[tempv].w > dist[u].w+tempw)  
  79.             {  
  80.                 dist[tempv].w = dist[u].w+tempw;  
  81.                 path[tempv] = u;  
  82.                 q.push(dist[tempv]);  
  83.             }  
  84.             p = p->nextarc;  
  85.         }  
  86.     }  
  87. }  
  88.   
  89. void CreateALGraph(ALGraph &g, int arc, int vex)  
  90. {  
  91.     g.arcnum = arc;  
  92.     g.vexnum = vex;  
  93.     int v1, v2, i, w;  
  94.   
  95.     for(i = 1; i <= vex; i++)  
  96.     {  
  97.         g.vertices[i].firstarc = NULL;  
  98.     }  
  99.     for(i = 1; i <= arc; i++)  
  100.     {  
  101.         cin >> v1 >> v2 >> w;  
  102.         ArcNode *q = (ArcNode*)malloc(sizeof(ArcNode));  
  103.         q->adjvex = v2;  
  104.         q->weight = w;  
  105.   
  106.         q->nextarc = g.vertices[v1].firstarc;  
  107.         g.vertices[v1].firstarc = q;  
  108.     }  
  109. }  
  110. int DFS(ALGraph g, int i, int j)  
  111. {  
  112.     visited[i] = 1;  
  113.     ArcNode *p = g.vertices[i].firstarc;  
  114.     while(p)  
  115.     {  
  116.         if(p->adjvex == j)  
  117.             return 1;  
  118.         //cout <<(visited[p->adjvex])<< endl;  
  119.         if(!(visited[p->adjvex]) && DFS(g, p->adjvex, j))  
  120.             return 1;  
  121.         p = p->nextarc;  
  122.     }  
  123.     return 0;  
  124. }  
  125.   
  126. int BFS(ALGraph g, int i, int j)  
  127. {  
  128.     queue<int>q;//  
  129.     q.push(i);  
  130.     visited[i] = 1;  
  131.     ArcNode *p;  
  132.     while(!q.empty())  
  133.     {  
  134.         int temp = q.front();  
  135.         q.pop();  
  136.         p = g.vertices[temp].firstarc;  
  137.   
  138.         while(p)  
  139.         {  
  140.             //cout << p->adjvex;  
  141.             if(p->adjvex == j)  
  142.                 return 1;  
  143.             if(!(visited[p->adjvex]))  
  144.             {  
  145.                 visited[p->adjvex] = 1;  
  146.                 q.push(p->adjvex);  
  147.             }  
  148.             p = p->nextarc;  
  149.         }  
  150.     }  
  151.     return 0;//返回不可少  
  152. }  
  153. int main()  
  154. {  
  155.     int m, n;  
  156.     //顶点,边  
  157.     cin >> n >> m;  
  158.     ALGraph g;  
  159.     CreateALGraph(g, m, n);  
  160.   
  161. //    for(int i = 1; i <= n; i++)  
  162. //    {  
  163. //        ArcNode *p = g.vertices[i].firstarc;  
  164. //        cout << "i = "  << i << ": ";  
  165. //        while(p)  
  166. //        {  
  167. //            cout << p->adjvex;  
  168. //            p = p->nextarc;  
  169. //        }  
  170. //      cout << endl;  
  171. //    }  
  172.     int v0, ve;  
  173.     cin >> v0 >> ve;  
  174.     Dijkstra(g, v0, n);  
  175.     if(dist[ve].w != INF)  
  176.         cout << dist[ve].w << endl;  
  177.     else  
  178.         cout << -1 <<endl;  
  179.   
  180.     return 0;  
  181. }  

最短路径的Dijkstra算法(邻接表)

  6078人阅读  评论(0)  收藏  举报
  分类:
数据结构与算法(75) 
描述
    以邻接表作为存储结构实现,求解从给定源点到给定结束点的最短路径。
 
输入

从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


[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <cstring>  
  4. #include <queue>  
  5. #define maxnum 120  
  6. #define INF 10000000  
  7.   
  8. using namespace std;  
  9.   
  10. typedef char VertexType;  
  11. //边  
  12. typedef struct ArcNode  
  13. {  
  14.     int adjvex;  
  15.     int weight;  
  16.     struct ArcNode *nextarc;  
  17. }ArcNode;  
  18. //顶点  
  19. typedef struct VNode  
  20. {  
  21.     VertexType data;  
  22.     ArcNode *firstarc;  
  23. }VNode, AdjList[maxnum];  
  24.   
  25. typedef struct  
  26. {  
  27.     AdjList vertices;//数组  
  28.     int vexnum, arcnum;  
  29. }ALGraph;  
  30.   
  31. //顶点节点,保存id和到源顶点的估算距离,优先队列需要的类型  
  32. struct Node  
  33. {  
  34.     int id;//源顶点id  
  35.     int w;//估算距离  
  36.   
  37.     //因要实现最小堆,按升序排列,因而需要重载运算符,重定义优先级,以小为先  
  38.     friend bool operator < (struct Node a, struct Node b)  
  39.     {  
  40.         return a.w > b.w;  
  41.     }  
  42. };  
  43.   
  44. int path[maxnum];  
  45. int visited[maxnum] = {0};  
  46. Node dist[maxnum];  
  47. priority_queue<Node>q;  
  48.   
  49. void Dijkstra(ALGraph g, int v0, int n)  
  50. {  
  51.     //初始化  
  52.   
  53.     for(int i = 1; i <= n; i++)  
  54.     {  
  55.         dist[i].id = i;  
  56.         dist[i].w = INF;  
  57.         path[i] = -1;       //每个顶点都无父亲节点  
  58.         visited[i] = 0;     //都未找到最短路  
  59.     }  
  60.     dist[v0].w = 0;  
  61.     q.push(dist[v0]);  
  62.     while(!q.empty())  
  63.     {  
  64.         Node cd = q.top();  
  65.         q.pop();  
  66.         int u = cd.id;  
  67.   
  68.         if(visited[u])  
  69.             continue;  
  70.         visited[u] = 1;  
  71.         ArcNode *p = g.vertices[u].firstarc;  
  72.   
  73.         while(p)  
  74.         {  
  75.             int tempv = p->adjvex;  
  76.             int tempw = p->weight;  
  77.   
  78.             if(!visited[tempv] && dist[tempv].w > dist[u].w+tempw)  
  79.             {  
  80.                 dist[tempv].w = dist[u].w+tempw;  
  81.                 path[tempv] = u;  
  82.                 q.push(dist[tempv]);  
  83.             }  
  84.             p = p->nextarc;  
  85.         }  
  86.     }  
  87. }  
  88.   
  89. void CreateALGraph(ALGraph &g, int arc, int vex)  
  90. {  
  91.     g.arcnum = arc;  
  92.     g.vexnum = vex;  
  93.     int v1, v2, i, w;  
  94.   
  95.     for(i = 1; i <= vex; i++)  
  96.     {  
  97.         g.vertices[i].firstarc = NULL;  
  98.     }  
  99.     for(i = 1; i <= arc; i++)  
  100.     {  
  101.         cin >> v1 >> v2 >> w;  
  102.         ArcNode *q = (ArcNode*)malloc(sizeof(ArcNode));  
  103.         q->adjvex = v2;  
  104.         q->weight = w;  
  105.   
  106.         q->nextarc = g.vertices[v1].firstarc;  
  107.         g.vertices[v1].firstarc = q;  
  108.     }  
  109. }  
  110. int DFS(ALGraph g, int i, int j)  
  111. {  
  112.     visited[i] = 1;  
  113.     ArcNode *p = g.vertices[i].firstarc;  
  114.     while(p)  
  115.     {  
  116.         if(p->adjvex == j)  
  117.             return 1;  
  118.         //cout <<(visited[p->adjvex])<< endl;  
  119.         if(!(visited[p->adjvex]) && DFS(g, p->adjvex, j))  
  120.             return 1;  
  121.         p = p->nextarc;  
  122.     }  
  123.     return 0;  
  124. }  
  125.   
  126. int BFS(ALGraph g, int i, int j)  
  127. {  
  128.     queue<int>q;//  
  129.     q.push(i);  
  130.     visited[i] = 1;  
  131.     ArcNode *p;  
  132.     while(!q.empty())  
  133.     {  
  134.         int temp = q.front();  
  135.         q.pop();  
  136.         p = g.vertices[temp].firstarc;  
  137.   
  138.         while(p)  
  139.         {  
  140.             //cout << p->adjvex;  
  141.             if(p->adjvex == j)  
  142.                 return 1;  
  143.             if(!(visited[p->adjvex]))  
  144.             {  
  145.                 visited[p->adjvex] = 1;  
  146.                 q.push(p->adjvex);  
  147.             }  
  148.             p = p->nextarc;  
  149.         }  
  150.     }  
  151.     return 0;//返回不可少  
  152. }  
  153. int main()  
  154. {  
  155.     int m, n;  
  156.     //顶点,边  
  157.     cin >> n >> m;  
  158.     ALGraph g;  
  159.     CreateALGraph(g, m, n);  
  160.   
  161. //    for(int i = 1; i <= n; i++)  
  162. //    {  
  163. //        ArcNode *p = g.vertices[i].firstarc;  
  164. //        cout << "i = "  << i << ": ";  
  165. //        while(p)  
  166. //        {  
  167. //            cout << p->adjvex;  
  168. //            p = p->nextarc;  
  169. //        }  
  170. //      cout << endl;  
  171. //    }  
  172.     int v0, ve;  
  173.     cin >> v0 >> ve;  
  174.     Dijkstra(g, v0, n);  
  175.     if(dist[ve].w != INF)  
  176.         cout << dist[ve].w << endl;  
  177.     else  
  178.         cout << -1 <<endl;  
  179.   
  180.     return 0;  
  181. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值