[问题描述]
上网下载真实南京公交线路图,建立南京主要公交线路图的存储结构。
[基本要求]
(1)输入任意两站点,给出转车次数最少的乘车路线。
(2)输入任意两站点,给出经过站点最少的乘车路线。
[基本思路]
根据公交线路图建立两个邻接矩阵分别记为f,f2,一个表示两站被一条线路相连,一个表示两站相邻。
转车最少的乘车路线即针对f求最短路径,过站最少的乘车路线即针对f2求最短路径。
在笔者的代码中,最短路径的求法采用了迪杰斯特拉(Dijkstra)算法,时间复杂度为o(n^2)。对于此题的五千多个站点来讲够用。
在输出的时候,为了输出搭乘公交车的路数,我提前建立了一个set<int>数组进行线路上的站点存储,对于最少转车来讲,我根据每一辆车的上车点和下车点来寻找所搭乘的路线,直接输出即可。对于最少过站来讲稍微麻烦一点,因为要输出所有的过站,而搭乘一辆车可能过多站,我就是根据相邻两站判断所乘车次,再与前一次所乘车次进行对比,若相同,则输出“过 XXX站 ”,否则,前一次的输出为“到 XXX站 ”,这次的输出为“乘XXX路车从 XXX 站 "。
[代码实现]
1.头文件以及函数外变量定义
#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<iomanip>
#include<stack>
using namespace std;
map<string, int>station;//记录站的编号
set<int>lines[995];//记录线路上的站
map<int, string>nistation;//翻译站的编号
int n = 1;//记录不同公交站点的个数
int disp[5130];//共5124站,开大一点备用
int pre[5130] = { 0 };
bool visited[5130] = { false };
bool f[5130][5130] = { 0 };//在一条线路上
bool f2[5130][5130] = { 0 };//相邻的两站
2.文件读入和邻接矩阵建立
void initialize()
{
int* temp = new int[64];
int num = 0;//记录一条线路上的站点个数
int p = 0, lastp = 0;
for (int i = 0; i < 64; i++)
{
temp[i] = 0;
}
fstream file("lines.txt", ios::in);
if (file.fail())
{
cout << "文件打开失败!!!" << endl;
exit(0);
}
int line = 0;
while (!file.eof())
{
string s;
file >> s;
if (is_digit(s[0]) && !s.empty())
{
if (n != 1)
{
record(temp, num, line);
conect(temp, num);
p = lastp = 0;
num = 0;
}
line = 0;
int i = 0;
while (is_digit(s[i]))
{
line = line * 10 + int(s[i] - '0');
i++;
}
continue;
}
if (!is_digit(s[0]) && !s.empty())
{
if (station.find(s) == station.end())
{
station[s] = n;
nistation[n] = s;
n++;
temp[num] = n;
}
temp[num] = station[s];
lastp = p;
p = station[s];
if (lastp != 0)
{
f2[p][lastp] = 1;
f2[lastp][p] = 1;
}
num++;
}
}
record(temp, num, line);
conect(temp, num);
}
3.源代码
#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<iomanip>
#include<stack>
using namespace std;
map<string, int>station;
set<int>lines[995];
map<int, string>nistation;
int n = 1;//记录不同公交站点的个数
int disp[5130];//共5124站,开大一点备用
int pre[5130] = { 0 };
bool visited[5130] = { false };
bool f[5130][5130] = { 0 };//在一条线路上
bool f2[5130][5130] = { 0 };//相邻的两站
bool is_digit(char c)
{
if (c <= '9' && c >= '0')
return true;
return false;
}
void be_back()
{
fill(visited, visited + n, false);
fill(pre, pre + n, 0);
fill(disp, disp + n + 1, 9999999);
for (int i = 0; i <= n + 5; i++)
{
pre[i] = i;//初始状态设每个点的前驱为自身
}
}
void conect(int* temp, int num)
{
for (int i = 0; i < num; i++)
{
for (int j = 0; j < num; j++)
{
f[temp[i]][temp[j]] = 1;
f[temp[j]][temp[i]] = 1;
}
}
for (int i = 0; i < num; i++)
{
temp[i] = 0;
}
}
void record(int* temp, int num,int line)
{
for (int i = 0; i < num; i++)
{
//cout << nistation[temp[i]];
lines[line].insert(temp[i]);
}
}
void initialize()
{
int* temp = new int[64];
/*int max= 0;
int maxp = 0;*/
int num = 0;//记录一条线路上的站点个数
int p = 0, lastp = 0;
for (int i = 0; i < 64; i++)
{
temp[i] = 0;
}
fstream file("lines.txt", ios::in);
if (file.fail())
{
cout << "文件打开失败!!!" << endl;
exit(0);
}
int line = 0;
/*clock_t start;
clock_t end;
start = clock();*/
while (!file.eof())
{
string s;
file >> s;
if (is_digit(s[0]) && !s.empty())
{
if (n != 1)
{
record(temp, num, line);
conect(temp, num);
p = lastp = 0;
/*if (max < num)
{
max = num;
maxp = line;
}*/
num = 0;
}
line = 0;
int i = 0;
while (is_digit(s[i]))
{
line = line * 10 + int(s[i] - '0');
i++;
}
//cout << line << endl;
continue;
}
if (!is_digit(s[0]) && !s.empty())
{
if (station.find(s) == station.end())
{
station[s] = n;
nistation[n] = s;
n++;
temp[num] = n;
}
temp[num] = station[s];
lastp = p;
p = station[s];
if (lastp != 0)
{
f2[p][lastp] = 1;
f2[lastp][p] = 1;
}
num++;
}
}
record(temp, num, line);
conect(temp, num);
//cout << max << ' ' << maxp << endl;
//file.close();
//end = clock();
//double endtime = (double)(end - start) / clocks_per_sec;
//cout << "total time:" << endtime << endl; //s为单位
//cout << n - 1;
//cout << nistation[5123];
//fstream stationfile("station.txt", ios::out);
//for (int i = 0; i < n; i++)
//{
// stationfile << nistation[i] << '\n';
//}
}
void minzhuan()
{
string station1, station2;
while (1)
{
cout << "起始站:";
cin >> station1;
if (station.find(station1) == station.end())
{
cout << "不存在该站!!!" << endl << "请重新输入!!!";
}
else
break;
}
while (1)
{
cout << "终点站:";
cin >> station2;
if (station.find(station2) == station.end())
{
cout << "不存在该站!!!" << endl << "请重新输入!!!";
}
else
break;
}
//cout << station1 << station2;
int s1 = station[station1], s2 = station[station2];
//cout << s1 << s2;
if (f[s1][s2])
{
cout << "不需转车";
system("pause");
system("cls");
return;
}
be_back();
disp[s1] = 0;
for (int i = 1; i <= n + 5; i++)
{
int u = -1, min = 9999999;
for (int j = 1; j <= n + 5; j++)
{
if (visited[j] == false && disp[j] <= min)
{
u = j;
min = disp[j];
}
}
if (u == -1)
break;
visited[u] = true;
for (int v = 1; v <= n; v++)
{
if (visited[v] == false && f[u][v] == 1)
{
if (disp[u] + 1 < disp[v]) {
disp[v] = disp[u] + 1;
pre[v] = u;//pre保存当前结点的上一个节点,以便输出最短路线
}
}
}
}
stack<int> myStack;
int temp = s2;
myStack.push(s2);//先加终点
while (s1 != temp)
{
temp = pre[temp];
myStack.push(temp);//注意这里是把当前点的上一个结点放进去,此时换成了temp
}
cout << "起点" << station1 << "到" << station2 << "转车最少的路线为: "<<endl;
int nz = 0;
while (!myStack.empty())
{
nz++;
string s(100, '\0');
s = nistation[myStack.top()];
myStack.pop();
int way[10] = { 0 };
if (!myStack.empty())
{
int k = 0;
string s2 = nistation[myStack.top()];
for (int i = 1; i < 991; i++)
{
if (lines[i].find(station[s]) != lines[i].end())
{
if (lines[i].find(station[s2]) != lines[i].end())
{
way[k] = i;
k++;
}
}
}
cout << "乘 ";
if (k == 1)
cout << setw(3) << way[0] << " 路汽车从" << s << "到" << s2 << endl;
else
{
for (int i = 0; i < k - 1; i++)
{
cout << way[i] << " 或 ";
}
cout << setw(3) << way[k - 1] << " 路汽车从" << s << "到" << s2 << endl;
}
}
else
{
cout << "到站" << endl;
}
//cout << s << " ";
}
cout << endl << "需要转" << nz - 2 << "次车" << endl;
system("pause");
system("cls");
return;
}
void minzhan()
{
string station1, station2;
while (1)
{
cout << "起始站:";
cin >> station1;
if (station.find(station1) == station.end())
{
cout << "不存在该站!!!" << endl << "请重新输入!!!";
}
else
break;
}
while (1)
{
cout << "终点站:";
cin >> station2;
if (station.find(station2) == station.end())
{
cout << "不存在该站!!!" << endl << "请重新输入!!!";
}
else
break;
}
int s1 = station[station1], s2 = station[station2];
if (f2[s1][s2])
{
cout << "两站相邻";
system("pause");
system("cls");
return;
}
be_back();
disp[s1] = 0;
for (int i = 1; i <= n; i++)
{
int u = -1, min = 9999999;
for (int j = 1; j <= n; j++)
{
if (visited[j] == false && disp[j] <= min)
{
u = j;
min = disp[j];
}
}
if (u == -1)
break;
visited[u] = true;
for (int v = 1; v <= n; v++) {
if (visited[v] == false && f2[u][v] == 1)
{
if (disp[u] + 1 < disp[v]) {
disp[v] = disp[u] + 1;
pre[v] = u;//pre保存当前结点的上一个节点,以便输出最短路线
//cout << v << " " << pre[v] << endl;
}
}
}
}
stack<int> myStack;
int temp = s2;
myStack.push(s2);//先加终点
while (s1 != temp)
{
temp = pre[temp];
myStack.push(temp);//注意这里是把当前点的上一个结点放进去,此时换成了temp
}
cout << "起点" << station1 << "到" << station2 << "过站最少的路线为: " << endl;
int nz = 0;
int last = -1;
bool flag2 = 1;
while (!myStack.empty())
{
nz++;
string s(100, '\0');
s = nistation[myStack.top()];
myStack.pop();
int way[10] = { 0 };
bool flag = 1;
if (!myStack.empty())
{
int k = 0;
string s2 = nistation[myStack.top()];
for (int i = 1; i < 991; i++)
{
if (lines[i].find(station[s]) != lines[i].end())
{
if (lines[i].find(station[s2]) != lines[i].end())
{
if (i != last)
{
last = i;
flag = 0;
}
break;
}
}
}
if (flag == 0)
{
if (flag2 != 1)
{
cout << " 到 " << s << endl;
}
cout << "乘 " << setw(3) << last << " 路车从 " << s;
flag2 = 0;
}
else
{
cout << " 过 " << s;
}
}
else
{
cout << " 到 " << s << endl;
cout << "到站" << endl;
}
//cout << s << " ";
}
cout << endl << "需要过" << nz - 2 << "站" << endl;
system("pause");
system("cls");
return;
}
int main()
{
initialize();
//for (set<int>::iterator i = lines[990].begin(); i != lines[990].end(); i++)
// cout << *i << endl; //注意指针运算符
while (1)
{
int choice;
cout << "1.输入任意两站点,给出转车次数最少的乘车路线。" << endl;
cout << "2.输入任意两站点,给出经过站点最少的乘车路线。" << endl;
cout << "3.退出" << endl;
cin >> choice;
switch (choice)
{
case 3:return 0;
case 1:minzhuan(); break;
case 2:minzhan(); break;
}
}
}
[运行结果]
[所用文件格式]
下载地址:https://download.csdn.net/download/weixin_63343322/87262277