双向广搜的DIJKSTRA算法--简易的北京地铁导航实现

本学期的课程设计,实现最短路的算法,于是采用了DIJKSTRA算法,并用双向广搜优化了。

实现了简易的北京地铁导航。于是把代码分享出来。

(核心代码是find_min(),Dijkstra()部分)

转载或者用到里面的代码请注明博主姓名以及出处!

(注:只输入了图片里的地铁站信息,所用到的文件最下面有下载,因为这些文件是我和同学一条一条的录入的,所以如果你用到请务必注明这些文件的出处)



代码:

/*************************************************************** 
 
* 
版权所有 (C)2014, 五十风 
 
* 
程序名称:北京地铁导航 
 
* 
作    者:五十风 
 
* 
完成日期:2015.1.20 

 
***************************************************************/  

#include
#include
#include
#include
#include
#include
#include 
#include
using namespace std;

#define N 200
const double V=0.555;
int n;
struct road
{
	int des;//终点站名
	double dis;//距离
};
//本站能到达的其他站的站名
struct subway
{
	subway()
	{count=0;}
	int count;
	road lu[6];
};
//临时记录起点到达其他点的最短路
struct min_road
{
	min_road()
	{
		count=0;
		dis=0.0;
	}
	int count;
	double dis;
	int transit[N];//经过的点,包含起点,与目的地

};
struct s_e
{
	int s_to_des;
	int e_to_des;
};
struct cmp
{
	bool operator()(road x,road y)
    {
        return x.distemp)
					{
						s_dian=i;
						e_dian=dian.lu[j].des;
						min=temp;
					}
					break;
				}
			}
		}
	}
	if(f)
		return 0;
	vis[e_dian]=true;
	for(i=0;i<_dis[s_dian].count;i++)
	{
		_dis[e_dian].transit[i]=_dis[s_dian].transit[i];
	}
	_dis[e_dian].transit[i]=e_dian;
	_dis[e_dian].count=_dis[s_dian].count+1;
	_dis[e_dian].dis=min;
	return e_dian;
}
s_e Dijkstra(subway *arry,bool *s_vis,bool *e_vis,min_road *s_dis,min_road *e_dis,int start,int end,int n)
{
	int temp1,temp2,i,j;
	s_e temp;
	for(i=1;i>num)
			{
				fin>>s;
				if(num==a.transit[i])
				{
					f=1;
					break;
				}
			}
			fin.close();
			if(f)
				cout<=0;i--)
		{
			f=0;
			fin.open("no.txt");
			while(fin>>num)
			{
				fin>>s;
				if(num==a.transit[i])
				{
					f=1;
					break;
				}
			}
			fin.close();
			if(f)
				cout<= start && code <= end)
{
return true;
}
return false;
}

char convert(wchar_t n)
{
if (In(0xB0A1,0xB0C4,n)) return 'a';
if (In(0XB0C5,0XB2C0,n)) return 'b';
if (In(0xB2C1,0xB4ED,n)) return 'c';
if (In(0xB4EE,0xB6E9,n)) return 'd';
if (In(0xB6EA,0xB7A1,n)) return 'e';
if (In(0xB7A2,0xB8c0,n)) return 'f';
if (In(0xB8C1,0xB9FD,n)) return 'g';
if (In(0xB9FE,0xBBF6,n)) return 'h';
if (In(0xBBF7,0xBFA5,n)) return 'j';
if (In(0xBFA6,0xC0AB,n)) return 'k';
if (In(0xC0AC,0xC2E7,n)) return 'l';
if (In(0xC2E8,0xC4C2,n)) return 'm';
if (In(0xC4C3,0xC5B5,n)) return 'n';
if (In(0xC5B6,0xC5BD,n)) return 'o';
if (In(0xC5BE,0xC6D9,n)) return 'p';
if (In(0xC6DA,0xC8BA,n)) return 'q';
if (In(0xC8BB,0xC8F5,n)) return 'r';
if (In(0xC8F6,0xCBF0,n)) return 's';
if (In(0xCBFA,0xCDD9,n)) return 't';
if (In(0xCDDA,0xCEF3,n)) return 'w';
if (In(0xCEF4,0xD188,n)) return 'x';
if (In(0xD1B9,0xD4D0,n)) return 'y';
if (In(0xD4D1,0xD7F9,n)) return 'z';
return '\0';
}

char pinyin(string sChinese)
{
	char chr[3];
	wchar_t wchr = 0;
	char buff=0x00;
	int j=0,i=0;
	memset(chr, 0x00, sizeof(chr));
	chr[0] = sChinese[j++];
	chr[1] = sChinese[j++];
	chr[2] = '\0';
	wchr = 0;
	wchr = (chr[0] & 0xff) << 8;
	wchr |= (chr[1] & 0xff);
	buff = convert(wchr);
	return buff;

}

int find_num(string s)
{
	char c;
	int num;
	string a;
	c=pinyin(s);
	a=c;
	a="subway\\"+a+".txt";
	ifstream fin;
	
	fin.open(a.c_str());
	while(fin>>a)
	{
		fin>>num;
		if(a==s)
		{
			fin.close();
			return num;
		}
	}
	fin.close();
	return -1;
}

void meun()
{
	int i=0,j;
		char a=3,b=5;
		cout<<"\t\t";
		for(j=1;j<10;j++)
		{
			if(i==0) 
			{
				if(!(j&1)) cout<0.0 && a<=6.0) return 3;
	if( a>6.0 &&a<=12.0) return 4;
	if(a>12.0 &&a<=32.0)
	{
		m=4;
		a-=12.0;
		m++;
		while(a>0)
		{
			a-=10;
			m++;
		}
		return m;
	}
	if(a>32.0)
	{
		m=6;
		a-=32.0;
		m++;
		while(a>0)
		{
			a-=20;
			m++;
		}
		return m;

	}
}
void newsubway()
{
	ifstream fin;
	int temp_num;
	fin.open("num.txt");
	fin>>temp_num;	
	temp_num++;
	fin.close();

	ofstream fout,fout2,fout3;
	cout<<"输入新增站点名:";
	string s;
	string name;
	cin>>s;
	string c;
	c=pinyin(s);
	name="subway\\"+c+".txt";
	fout.open(name.c_str(),ios::app);
	fout<>one;
	cin>>two;
	cin>>dis;
	s=find_num(one);
	e=find_num(two);
	fout.open("road.txt",ios::app);
	fout<>n;
	n++;
	fin2.close();
	fin.open("road.txt");
	while(fin>>a)
	{
	
		fin>>b;
		fin>>c;
		input(arry,a,b,c);
	}
	fin.close();
	sort_road(arry,n);
	while(1)
	{
		meun();
		cout<<"输入选择:";
		cin>>choose;
		if(choose==0)
		{
			cout<>a)
			{
				fin>>s;
				cout<>one;
		cout<<"输入终点:";
		cin>>two;
		start=find_num(one);
		end=find_num(two);
		if(start==-1 || end==-1)
		{
			cout<<"输入的地铁站错误"<

所用到的文件下载:点击打开链接

你可能感兴趣的:(代码分享)