7-1 单源最短路径
本题目要求一有向有权图(以边列表的形式读入),一个起点和一个终点。输出源点到终点的最短路径。
输入格式:
输入三个整数n(100000<n<10000000),s和d,其中n表示边数目,s为起点,d为终点。然后依次输入n条边:v1 v2 w(v1为出度端点,v2为入度端点,w为权值,三者均为非负整数,其中w不超过20,v1和v2不超过1000000且节点序号不一定连续)
输出格式:
一个整数,表示s到d的最短距离。如果不可达,则输出-1.
输入样例:
在这里给出一组输入。例如:
11 0 8
0 1 6
0 2 4
0 3 5
1 4 1
2 4 1
3 5 2
4 6 9
4 7 7
5 7 4
6 8 2
7 8 4
输出样例:
在这里给出相应的输出。例如:
15
因为实验隐藏的测试用例很大,故采用邻接表结构存储边权图;
为了解决内存超限问题,故将数据保存在堆中,即通过new创建数组
为了解决超时问题,故采用了优先队列
段错误可能原因:终点给小了,用例中可能有超过了终点的大小的节点出现
解决办法:直接开一个MAX大小的数组(MAX=1000000),省略不需要的节点信息,例如Path
#include<iostream>
#include<algorithm>
#include<queue>
#define MAX 1000000
#define MAXInt 31245
using namespace std;
typedef pair<int,int> Pair;//用优先队列选取权重最小的节点
int arc,s,e;
priority_queue<Pair,vector<Pair>,greater<Pair> >que;
typedef struct ArcNode{
int adjvex;
int weight;
struct ArcNode* nextarc;
}ArcNode;
typedef struct VNode{
// int data;//数组下标即节点名称,不需要data信息
bool vis;
int wei;
// int path;//用pair存的值<weight,该权重的节点> ,不需要路径,故path不需要
ArcNode *firstarc;
}VNode;
typedef struct{
VNode* vertice;
int vexnum,arcnum;
}ALGraph;
void Creat(ALGraph &G);
void Dij(ALGraph &G,int s);
int main(){
ALGraph G;
Creat(G);
#if 0 //此处为测试段,测试是否成功创建邻接表
for(int i=0;i<MAX;i++){
ArcNode* p=G.vertice[i].firstarc;
while(p!=NULL){//!p是不正确的
cout<<p->adjvex<<" ";
p=p->nextarc;
}
cout<<endl;
}
#endif
/*以下为控制结果的输出,若未找到,输出-1*/
Dij(G,s);
if(G.vertice[e].wei!=MAXInt)
cout<<G.vertice[e].wei<<endl;
else
cout<<-1<<endl;
delete []G.vertice;
}
void Creat(ALGraph &G){
cin>>arc>>s>>e;//输入边数,起点,终点
G.vertice=new VNode[MAX];
G.arcnum=arc;
//G.vexnum=e+1;
for(int k=0;k<MAX;k++){
G.vertice[k].firstarc=NULL;
// G.vertice[k].data=k;
G.vertice[k].vis=false;
G.vertice[k].wei=MAXInt;
// G.vertice[k].path=-1;
}
for(int i=0;i<G.arcnum;i++){
int v1,v2,wei;
cin>>v1>>v2>>wei;
ArcNode *pe=new ArcNode;
pe->adjvex=v2;
pe->weight=wei;
pe->nextarc=G.vertice[v1].firstarc;
G.vertice[v1].firstarc=pe;
}
}
void Dij(ALGraph &G,int s){
G.vertice[s].wei=0;//初始化起点weight为0
/*错误点1:初始化只需将起点初始化插入队列即可,不需要对起点的邻接点也初始化,
此处操作与循环中操作相同*/
/*
ArcNode *p=G.vertice[s].firstarc;
while(p!=NULL){
G.vertice[p->adjvex].wei=p->weight;//初始化权重
G.vertice[p->adjvex].path=s;//初始化前驱节点
// ArcNode *d=p;
p=p->nextarc;
// delete d;//释放d所指向的内存
}
*/
que.push(Pair(0,s));
while(!que.empty()){
Pair tmp=que.top();
que.pop();
int v=tmp.second;//节点
int w=tmp.first;//权重
if(G.vertice[v].vis==true)
continue;
else
G.vertice[v].vis=true;//s已被访问
ArcNode *p=G.vertice[v].firstarc;
while(p!=NULL){
int v0=p->adjvex;
if(w+p->weight<G.vertice[v0].wei&&!G.vertice[v0].vis){
G.vertice[v0].wei=G.vertice[v].wei+p->weight;
//G.vertice[v0].path=v;//更新前驱节点
que.push(Pair(G.vertice[v0].wei,v0));
}
p=p->nextarc;//错误点2:p未循环
}
}
}
对于上述代码的优化:
1.可以在找到终点时就退出循环,输出结果
2.对于此处的优先队列来说,我以pair(相当于下图的map)为关键字存储,故会存在同一个节点对应不同weight的情况,例如:之前存入了<3,1> (节点1的weight为3),更新之后存入了<2,1>(节点1的权重为2),虽不影响结果,但队列里会有重复的节点,浪费内存
改进:入队前与已在队列里的节点比较weight,小于则修改weight
3、采用链式前向星+优先队列
https://pursuit.blog.youkuaiyun.com/article/details/107674290
反思与总结
通过这次实验,让我意识到,从代码思想到代码实现,再到具体的实际应用,这之间都是有距离的。
代码思想到实现:这考验的是个人代码实现水平,把思想变成代码的能力有待加强
代码思想到实际应用:实际应用过程中,由于数据量,时间限制等多方面因素的影响,这就需要改善代码的适应性,结合一些其他的手段以投入实际应用。算法不可能是赤裸裸的直接用的。对于实际问题的处理能力有待增强。