华为6地铁最佳路径
来源:互联网 发布:超星尔雅网络通识课 编辑:程序博客网 时间:2024/04/28 02:43
- /******************************************************************************
- Copyright (C), 2001-2011, Huawei Tech. Co., Ltd.
- ******************************************************************************
- File Name :
- Version :
- Author :
- Created : 2010/3
- Last Modified :
- Description :
- Function List :
- History :
- 1.Date : 2010/3
- Author :
- Modification: Created file
- ******************************************************************************/
- #include "FindMinimalPath.h"
- #include <map>
- #include <set>
- using namespace std;
- struct stop{
- unsigned int next[100];//最大有100个下一站,可连通
- bool paased;//每次前往下一战时,先判断下一站是否已通过,若为通过则将其加入oldstop
- int nnum;//可连通数量
- int lnum;//线路数量
- unsigned int LineNo[100];//所在线路的id
- unsigned int path[100][100];//对于每条路径。。。我只保存前一个值是不够的。。。可能有多条路径,最多100条
- int pnum;//路径数量
- //int paassN;//
- };
- map<unsigned int,stop> allstop;
- unsigned int oldstop[1000];
- unsigned int newos[1000];//用来替换正在计算中的起点站
- int newosn;//总起点暂时总数
- int oldn;//起点总数
- int passN;//结果站点总数
- //int ps;//路径总数
- //int newps;//新路径暂时总数
- //unsigned int path[1000][100];
- //unsigned int npath[1000][100];
- /*******************************************************************************************************************
- 函数说明: 增加某条地铁线路
- 函数原型: void AddLine(unsigned int LineNo, unsigned int StationNum ,unsigned int *pStationArray);
- 输入参数:
- LineNo 地铁线路号;
- StationNum 该条地铁线中的站点数目,由调用者保证不小于2;
- pStationArray 该条地铁线的所有站点信息,pStationArray指向的存储空间在函数外会被释放,请自行申请存储空间;
- 输出参数: 无
- 返回值 : 无
- ********************************************************************************************************************/
- void AddLine(unsigned int LineNo, unsigned int StationNum ,unsigned int *pStationArray)
- {
- /* 在这里实现功能 */
- //假设,对与 1 ,2, 1的环线线路,如何进行判断,站台数是2。。。
- unsigned int p = 0 ;
- unsigned int start = *pStationArray;
- unsigned int before = *pStationArray;
- map<unsigned int,stop>::iterator t;
- for(int i = 0 ; i < StationNum ; i++){
- if(allstop.count(*pStationArray)){//如果与其他线路交叉
- t = allstop.find(*pStationArray);
- t->second.LineNo[t->second.lnum++] = LineNo ;
- if( i == 0){
- before = *pStationArray++;
- continue;
- }
- }else{
- stop news;
- news.LineNo[0] = LineNo ;
- news.lnum = 1;
- news.paased = false;
- news.nnum = 0;
- news.pnum = 0;
- allstop.insert(pair<unsigned int,stop>(*pStationArray , news));
- }
- if(i > 0){
- t = allstop.find(before);
- t->second.next[t->second.nnum++] = *pStationArray;
- t = allstop.find(*pStationArray) ;
- t->second.next[t->second.nnum++] = before;
- }
- before = *pStationArray++;
- }
- if( start == *pStationArray) {//即为环,这里的stationnum是多少就是多少,不重复包含初始站和终点站
- t->second.next[t->second.nnum++] = start ;
- t = allstop.find(start);
- t->second.next[t->second.nnum++] = before ;
- }
- return;
- }
- /*********************************************************************************************************************
- 函数说明:计算从起点站到终点站的最短路线长度
- 函数原型:int CalcMinPathLen(unsigned int SrcStation, unsigned int DesStation);
- 输入参数:
- SrcStation 起点站;
- DesStation 终点站;
- 输出参数:无
- 返回值 :
- 起点站到终点站的最短路线长度
- -1:任何出错情况(包括路线不存在、站点不存在、起点和终点重叠等等)
- **********************************************************************************************************************/
- bool gonext(unsigned int DesStation){
- map<unsigned int,stop>::iterator p;
- map<unsigned int,stop>::iterator next;
- newosn=0;
- passN++;
- //newps=0;
- for(int i=0;i<oldn;i++){
- p = allstop.find(oldstop[i]);
- for(int j=0;j<p->second.nnum;j++){
- next = allstop.find(p->second.next[j]);
- if(!next->second.paased){
- //next->second.paased = true;//一次前进过程中,可以从不同的方向到达同一点,但是,不能从回到原来的点
- int pathnun=p->second.pnum;
- for(int l=0;l<pathnun;l++){
- for(int k=0;k < passN - 1;k++){
- next->second.path[next->second.pnum][k] = p->second.path[l][k];
- }
- next->second.path[next->second.pnum++][passN-1] = next->first;
- // npath[newps][passN-1] = next->first;
- //next->second.path[next->second.pnum++] = path[newps++];
- }
- //next
- newos[newosn++] = next->first;
- }
- }
- }
- oldn = newosn;
- //ps = newps;
- int nn=0;
- for(int i=0;i < oldn;i++){
- p = allstop.find(newos[i]);
- if(!p->second.paased){
- oldstop[nn++] = newos [i];
- p->second.paased= true ;
- }
- }
- oldn = nn;
- //for (int i=0;i < ps;i++){
- // for(int j=0;j < passN ;j++){
- // path[i][j] = npath[i][j];
- // }
- //}
- for (int i=0;i<oldn;i++){
- if(oldstop[i] == DesStation)
- return false;
- }
- return true;
- }
- int CalcMinPathLen(unsigned int SrcStation, unsigned int DesStation)
- {
- /* 在这里实现功能 */
- if(SrcStation==DesStation)return -1;
- if(!allstop.count(SrcStation)||!allstop.count(DesStation))return -1;
- map<unsigned int ,stop>::iterator p;
- p = allstop.begin();
- for(int i=0; i< allstop.size();i++){//初始化一部分数据
- p -> second.paased = false;
- p -> second.pnum = 0;
- p++;
- }
- p = allstop.find(SrcStation );
- p->second.paased = true;
- p->second.pnum=1;
- oldn = 0;
- oldstop[oldn++] = SrcStation ;
- //path[0][0] = SrcStation;
- passN = 0;
- // ps = 0;
- while(gonext(DesStation));//寻找路径
- return passN;
- };
- /**********************************************************************************************************
- 函数说明:输出从起点站到终点站的最短路线
- 函数原型:int SearchMinPathes(unsigned int SrcStation,
- unsigned int DesStation,
- unsigned int* pPathNum,
- unsigned int* pPathLen,
- unsigned int **ppPathes);
- 输入参数:
- SrcStation 起点站;
- DesStation 终点站;
- Output Param
- pPathNum 最短路线条数;
- pPathLen 最短路线长度;
- ppPathes 存储最短路线的内存地址,内存空间在本函数内申请,调用者释放,内存空间的存储格式请见PPT要求;
- 返回值 :
- 0 :成功
- -1:任何出错情况(包括路线不存在、站点不存在、起点和终点重叠等等)
- ************************************************************************************************************/
- int SearchMinPathes(unsigned int SrcStation,
- unsigned int DesStation,
- unsigned int* pPathNum,
- unsigned int* pPathLen,
- unsigned int **ppPathes)
- {
- /* 在这里实现功能 */
- map<unsigned int,stop>::iterator p;
- int r = CalcMinPathLen(SrcStation,DesStation);
- if(r==-1)return -1;
- else{
- *pPathNum = passN;
- p = allstop.find(DesStation);
- r = p->second.pnum;
- *pPathLen = r;
- //r*=passN;
- unsigned int *save = (unsigned int*)malloc(sizeof(unsigned int)*r*passN);
- for(int i=0;i<r;i++){
- *ppPathes = save;
- for(int j=0;j<passN;j++)
- *save++ = p->second.path[i][j];
- }
- }
- return 0;
- }
- /*************************************************************************************************
- 函数说明:输出从起点站到终点站的最优路线
- 函数原型:int SearchBestPathes(unsigned int SrcStation,
- unsigned int DesStation,
- unsigned int *pPathNum,
- unsigned int* pPathLen,
- unsigned int** ppPathes);
- 输入参数:
- SrcStation 起点站;
- DesStation 终点站;
- Output Param
- pPathNum 最优路线条数;
- pPathLen 最短路线长度;
- ppPathes 存储最短路线的内存地址,内存格式见下图,内存空间在本函数内申请,调用者释放;
- 返回值 :
- 0:成功
- -1:任何出错情况(包括路线不存在、站点不存在、起点和终点重叠等等)
- **************************************************************************************************/
- int SearchBestPathes(unsigned int SrcStation,
- unsigned int DesStation,
- unsigned int *pPathNum,
- unsigned int* pPathLen,
- unsigned int** ppPathes)
- {
- /* 在这里实现功能 */
- int mini = 999;//最短路线
- int changes = 0;
- int all = 0;
- int pathnum = 0 ;
- int pathchange[100];
- set<unsigned int> oldline;
- set<unsigned int>::iterator t;
- map<unsigned int,stop>::iterator p;
- map<unsigned int,stop>::iterator last;
- map<unsigned int,stop>::iterator now;
- map<unsigned int,stop>::iterator start;
- int r = CalcMinPathLen(SrcStation,DesStation);\
- if(r == -1)return -1;
- else{
- p = allstop.find(DesStation);
- start = allstop.find(SrcStation);
- all = p->second.pnum;
- for(int i=0;i<all;i++){
- last = start;
- changes = 0;
- for(int j=0;j<last->second.lnum;j++){
- oldline.insert(last->second.LineNo[j]);//将第一个点可能的线路记录下来
- }
- for(int j=0;j<passN;j++){
- now = allstop.find( p->second.path[i][j]);
- int n = oldline.size();
- t = oldline.begin();
- bool htheline=false;
- for (int k=0; k < n ;k++){
- htheline = false ;
- for(int m=0;m<now->second.lnum;m++){
- if( *t == now->second.LineNo[m]){
- htheline =true;
- break;
- }
- }
- if(!htheline){
- t = oldline.erase(t);
- }else{
- t++;
- }
- }
- if(oldline.size()==0){//为空时,即必须要换车了,将这个点的所有可能线路保存下来
- changes++;
- for(int j=0;j<last->second.lnum;j++){
- for(int k=0;k<now->second.lnum;k++)
- if(last->second.LineNo[j]==now->second.LineNo[k])
- oldline.insert(last->second.LineNo[j]);//将从前一站到这一站可能使用的线路记录下来。
- }
- }else{
- ;//继续以剩余的线路继续前行
- }
- last = now;
- }
- pathchange[i] = changes ;
- oldline.clear();
- }
- mini = 999;
- for(int i=0; i<all;i++){
- mini = mini<pathchange[i]?mini:pathchange[i];//编程之美
- }
- //oldline.clear();
- for(int i=0;i<all;i++){
- if(pathchange[i]==mini)
- pathchange[pathnum++] = i;//获得最佳路线的数量和序号
- }
- *pPathNum = pathnum;
- *pPathLen = passN ;
- unsigned int *save = (unsigned int*)malloc(sizeof(unsigned int)*pathnum*passN);
- for(int i=0;i<pathnum;i++){
- *ppPathes = save;
- for(int j=0;j<passN;j++)
- *save++ = p->second.path[pathchange[i]][j++];
- }
- }
- return 0;
- }
- /*************************************************************************************************
- 函数说明:清空所有的线路信息
- 函数原型:void Clear();
- 输入参数:无
- 输出参数:无
- 返回值 :无
- **************************************************************************************************/
- void Clear()
- {
- /* 在这里实现功能 */
- allstop.clear();
- return ;
- };
0 0
- 华为6地铁最佳路径
- 华为练习 6 地铁最佳路径
- 华为地铁换乘问题
- 华为机试 地铁换乘问题(最短路径算法)
- 华为机试题-地铁换乘
- 地铁换乘最短路径
- 递归求地铁两站间最短路径
- 地铁换乘(华为机试样题)
- 华为笔试题(1)--地铁最少
- 华为笔试题——地铁换乘
- 华为机试地铁换乘问题
- 华为机试--地铁换乘问题
- 华为笔试题:地铁换乘问题
- 最佳路径
- 华为2014上机考试样题_高级题_地铁换乘最短路径_无向无权图+邻接表存储+BFS广度优先算法
- 华为2014上机考试样题_高级题_地铁换乘最短路径_无向无权图+邻接表存储+BFS广度优先算法
- 华为2014 so 板间最佳路径的权值 Dijkstra学习
- 地铁网络(分层图-哈密尔顿路径)
- linux c mysql 编程(上)
- HDOJ 1250 Hat's Fibonacci
- __declspec(dllexport) 和 __declspec(dllimport)
- 加载磊科336usb无线网卡驱动到s3c6410开发板中去
- linux c mysql 编程(下)
- 华为6地铁最佳路径
- 延伸正规表示法的符号意义
- 网站用户行为数据统计与分析之八:logstash与mongodb集成
- Openfire 性能优化
- ZOJ-1057 这题看的会哭出来<坑!>
- Windows Phone开发,引入dll出现“无法向项目中添加对较高版本或不兼容程序集的引用”
- 编写一个UNIX文件系统
- ReportStudio入门教程(十二) - 列表的复杂表头之继承类
- Codeforces 414B