# 无权图的单源最短路算法

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <map>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <functional>
using namespace std;
vector <int> graph[8];//邻接表
int dist[8];//最短距离
int path[8];//路径
queue<int> q;
stack<int> s;
void unweighted()
{
q.push(1);
dist[1] = 0;
path[1] = 0;
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i=0; i<graph[now].size(); i++)
{
if(dist[graph[now][i]]==-1)
{
dist[graph[now][i]] = dist[now]+1;
path[graph[now][i]] = now;
q.push(graph[now][i]);
}

}
}
}
int main()
{
graph[1].push_back(2);
graph[1].push_back(4);
graph[2].push_back(4);
graph[2].push_back(5);
graph[3].push_back(1);
graph[3].push_back(6);
graph[4].push_back(3);
graph[4].push_back(5);
graph[4].push_back(6);
graph[4].push_back(7);
graph[5].push_back(7);
graph[7].push_back(6);
memset(dist,-1,sizeof(dist));
memset(path,0,sizeof(path));
unweighted();
int i=6;
while(i!=0)
{
s.push(i);
i = path[i];
}
while(!s.empty())
{
cout<<s.top()<<"->";
s.pop();
}
cout<<endl;
for(int i=1;i<=7;i++)
cout<<dist[i]<<endl;
return 0;

}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120