void SK66( int node, int source, int dest, int iterCount, const Graph & graph, const EdgeInfoDict & edgeInfoDict, const Conditions & conditions, SK66_D_dict & ddict, SK66_F_dict & fdict, ShortestPathDict & pathDict) { // 当迭代次数为0时, 直接计算node->dest单源最短路径,存入结果字典里 if(iterCount == 0) { std::pair<int, int> pathToBeSolve(node, dest); if(!pathDict.count(pathToBeSolve)) Dijkstra(graph, edgeInfoDict, node, pathDict); if(!pathDict.count(pathToBeSolve)) { std::pair<std::pair<int, int>, int> key; key.first = pathToBeSolve; key.second = 0; Path path; path.first = 0xffffff; // 六个f, 足够表示无穷大了, 防止在后续加法中溢出 fdict[key] = path; } else { std::pair<std::pair<int, int>, int> key; key.first = pathToBeSolve; key.second = 0; ShortestPathDict::const_iterator PPath = pathDict.find(pathToBeSolve); Path path = PPath->second; fdict[key] = path; } } else { // 当迭代次数大于0的时候 std::pair<std::pair<int, int>, int> key; key.first = std::pair<int, int>(node, dest); key.second = iterCount; Path minCostPath; minCostPath.first = 0x7fffffff; for(Conditions::const_iterator iter = conditions.begin(); iter != conditions.end(); ++iter) { if(*iter == node) continue; // 计算D(v_i, v_l) std::pair<int, int> leftHalfPathToBeSolve(node, *iter); if(!pathDict.count(leftHalfPathToBeSolve)) { Dijkstra(graph, edgeInfoDict, node, pathDict); } if(!pathDict.count(leftHalfPathToBeSolve)) { Path leftHalfPath; leftHalfPath.first = 0xffffff; ddict[leftHalfPathToBeSolve] = leftHalfPath; } else { ShortestPathDict::const_iterator PPath = pathDict.find(leftHalfPathToBeSolve); Path leftHalfPath = PPath->second; ddict[leftHalfPathToBeSolve] = leftHalfPath; } // 计算F(v_l, t) // 筛选出最小值 std::pair<std::pair<int, int>, int> rightHalfPathToBeSolve; rightHalfPathToBeSolve.first.first = *iter; rightHalfPathToBeSolve.first.second = dest; rightHalfPathToBeSolve.second = iterCount-1; if(!fdict.count(rightHalfPathToBeSolve)) { SK66(*iter, source, dest, iterCount-1, graph, edgeInfoDict, conditions, ddict, fdict, pathDict); } if(ddict[leftHalfPathToBeSolve].first + fdict[rightHalfPathToBeSolve].first < minCostPath.first) { minCostPath.first = ddict[leftHalfPathToBeSolve].first + fdict[rightHalfPathToBeSolve].first; minCostPath.second.first.clear(); minCostPath.second.second.clear(); minCostPath.second.first.insert(minCostPath.second.first.end(), ddict[leftHalfPathToBeSolve].second.first.begin(), ddict[leftHalfPathToBeSolve].second.first.end()); minCostPath.second.first.insert(minCostPath.second.first.end(), fdict[rightHalfPathToBeSolve].second.first.begin(), fdict[rightHalfPathToBeSolve].second.first.end()); minCostPath.second.second.insert( minCostPath.second.second.end(), ddict[leftHalfPathToBeSolve].second.second.begin(), ddict[leftHalfPathToBeSolve].second.second.end()); minCostPath.second.second.insert( minCostPath.second.second.end(), fdict[rightHalfPathToBeSolve].second.second.begin(), fdict[rightHalfPathToBeSolve].second.second.end()); } } fdict[key] = minCostPath; } }
void CScheduleDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 CString strAGCNo, strTargetNo, strOpt; m_comboAGCNo.GetWindowTextW(strAGCNo); m_comboTargetNo.GetWindowTextW(strTargetNo); m_comboOpt.GetWindowTextW(strOpt); if (strAGCNo.IsEmpty() || strTargetNo.IsEmpty() || strOpt.IsEmpty()) { AfxMessageBox(_T("请输入车号,目标点号和操作!")); return; } // 类型转换 BYTE agvno = _ttoi(strAGCNo); UINT16 targetno = _ttoi(strTargetNo); UINT16 opt = (UINT16)m_comboOpt.GetItemData(m_comboOpt.GetCurSel()); if (++MSG_TAG > 255) MSG_TAG = 0; // 任务编号 UINT16 taskno = ++MSG_TASK; // 还要接收E1消息,其中的M6与这条tag不一样要重发 m_m6.tag = MSG_TAG; m_m6.agvno = agvno; m_m6.target = targetno; m_m2.tag = MSG_TAG; m_m2.agvno = agvno; m_m2.taskno = taskno; m_m2.taskopcode = opt; m_m1.tag = MSG_TAG; m_m1.agvno = agvno; m_m1.taskno = taskno; // 段数,可行走段号 通过Dijkstra算法计算 // 获取小车当前点号 CObList& clientList = m_pDoc->m_pListenSocket->m_clientList; POSITION pos = clientList.GetHeadPosition(); CClientSocket* pClient = NULL; while (pos) { pClient = (CClientSocket*)clientList.GetNext(pos); if (pClient->m_e1.agvno == agvno) break; } // 计算最短路径 if (!pClient) { AfxMessageBox(_T("还没用任何小车连接!")); return; } pClient->m_targetPt = targetno; Dijkstra(pClient->m_e1.curPoint, targetno); auto& vecRoute = m_pDoc->m_vecRoute; m_m1.secnum = (BYTE)vecRoute.size() - 1; // 段数 // 可行走段 auto& mapSideNo = m_pDoc->m_sideNo; //<段,段号> auto it = vecRoute.begin(); int prevSide = *it; std::advance(it, 1); int i = 0; // 控制段索引 CString strSideNo, strTemp; // 输出 for (auto it2 = it; it2 != vecRoute.end(); ++it2) { unsigned sideNo = mapSideNo[make_pair(prevSide, *it2)]; prevSide = *it2; m_m1.secno[i++] = sideNo; strTemp.Format(_T("%d-"), sideNo); strSideNo += strTemp; } if (strSideNo.IsEmpty()) strSideNo = _T("''"); Msg_M1M2M6 m1m2m6; m1m2m6.m1 = m_m1; m1m2m6.m2 = m_m2; m1m2m6.m6 = m_m6; // 写入数据库 m_AdoConn.OnInitADOConn(); CTime tm = CTime::GetCurrentTime(); CString strTm; strTm.Format(_T("%04d/%02d/%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond()); CString str; // str.Format(_T("%d,\'%s\',%d,%d,%d,%d,%d,%d,%d,\'%s\'"), taskno, strTm, agvno, 1, pClient->m_e1.curPoint, \ targetno, MSG_TAG, opt, m_m1.secnum, strSideNo); CString sql = _T("insert into tasklist (") \ _T("taskno,starttime,agvno,priority,startPt,endPt,msgtag,taskOptCode,sidenum,sideno) ") \ _T("values(") + str + _T(")"); m_AdoConn.ExecuteSQL((_bstr_t)sql); m_AdoConn.ExitConn(); // 给小车发送M1,M2,M6消息 pClient->Send(&m1m2m6, sizeof(m1m2m6)); CDialogEx::OnOK(); }
main() { FILE *f; char filename[50]; printf("input the name of the file:"); scanf("%s",filename);//the file's name can be absolute path,include the .txt example: D:\\file1\\file2\\test.txt f=fopen(filename,"rb"); if (f==NULL)//if can not open the file,stop the program { printf("OMG,the file is not existed!\n"); system("pause"); return; } get_the_map(f); getchar(); printf("input the start street name:"); gets(start_street); printf("input the start house number:"); scanf("%d",&start_house); getchar(); printf("input the destination street name:"); gets(destination_street); printf("input the destination house number:"); scanf("%d",&destination_house); Insert_start_destination(street_num,start_house,destination_house,start_street,destination_street); if (start_unique=='\0' || destination_unique=='\0') { system("pause"); return 0; } if (start_unique == destination_unique) { printf("the start point is the same as destination!\n"); system("pause"); return 0; } int k=1; int i,j; Dijkstra();//Dijkstra算法存储从起点开始,每个点到起点的最短路径的父亲节点 struct search_path BFS_array[50]; /*the destination node has his father nodes,and these father nodes also have their own father nodes. So it's a tree,and I use the BFS method to find every shortest path. */ int label; label=Index(destination_unique); BFS_array[1].unique=destination_unique; i=1; int from,present;//from 和 to 都是数组的角标 from=2; j=from; present=from+adj_list[label].father_num; while (i<=adj_list[label].father_num)//此次循环得到终点以及终点的父节点,将他们纳入BFS_array这个集合 { BFS_array[j].unique=adj_list[label].father_unique_num[i]; BFS_array[j].pre_label=1; j++; i++; } get_BFS_list(BFS_array,from,present);////////!!!!!!!!!!!!! i=1; int counter=0; while(BFS_array[i].unique!='\0')//get the total number of the shortest pathes { if (BFS_array[i].unique==start_unique) counter++; i++; } struct each_path shortest_ways[counter+1]; get_the_pathes(shortest_ways,BFS_array); int ways=counter; for (i=1;i<=ways;i++) { path_link present; present=&shortest_ways[i]; float pre_x,pre_y; if (present->unique_num==1) { pre_x=start_x; pre_y=start_y; } else { present->x=present->unique_num/100; present->y=present->unique_num%100; pre_x=present->x; pre_y=present->y; } present=present->next_node; while(present!=NULL) { float X,Y; if (present->unique_num==2) { X=pre_x-destination_x; Y=pre_y-destination_y; present->distance=sqrt(pow(X,2)+pow(Y,2))*mile; } else { present->x=present->unique_num/100; present->y=present->unique_num%100; X=pre_x-present->x; Y=pre_y-present->y; present->distance=sqrt(pow(X,2)+pow(Y,2))*mile; pre_x=present->x; pre_y=present->y; } present=present->next_node; } } turn_by_turn(shortest_ways,ways); system("pause"); }
int main() { printf("\nCOMP2000 Assignment 5\n\nStudent # : 812000767\n\n\n\n********** Graph ************* \n\n\n"); FILE*in = fopen("input.txt", "r"); FILE*out = fopen("output.txt", "w"); // part (a) char word[MaxWordSize] = { '\0' }; int numVertices = 0; fscanf(in, "%s", &word); while (strcmp(word,"END") != 0){ numVertices++; // getting the number of vertices's insertNode(&rootPtr, word); fscanf(in, "%s", &word); } Graph G = newGraph(numVertices); buildGraph(in, G); printGraph(out, G); // end part (a) printf("-> Part (a) finished ... \n\n"); // part b fscanf(in, "%s", &word); search(rootPtr, word); depthFirstTraversal(out, G, Placement); breadthFirstTraversal(out, G, Placement); fscanf(in, "%s", &word); search(rootPtr, word); depthFirstTraversal(out, G, Placement); breadthFirstTraversal(out, G, Placement); fprintf(out, "\n"); fprintf(out, "\n"); // end part b printf("-> Part (b) finished ... \n\n"); // part (c) fscanf(in, "%s", &word); while (strcmp(word, "END") != 0){ search(rootPtr, word); fprintf(out, "Minimal cost path from %s:\n", word); Dijkstra(out, G, Placement, word); fscanf(in, "%s", &word); Placement = 0; fprintf(out, "\n"); } // end part (c) printf("-> Part (c) finished ... \n\n"); Placement = j = order = 0; fclose(in); fclose(out); printf("********* Finished ***********\n\n"); system("PAUSE"); return 0; }
//Calls Dijkstra to obtain the fastest path to POI vector<unsigned> the_fastest_path(unsigned inter_id_start, const vector<unsigned>& destinations) { int size = destinations.size(); if (size == 1) return DirectedPath(inter_id_start, destinations[0]); else return Dijkstra(inter_id_start, destinations); }
#include "mainwindow.h" #include "ui_mainwindow.h" #include "nodo.h" #include "dijkstra.h" #include "floyd.h" #include "kruskal.h" #include "QComboBox" #include "QLabel" #include "QString" QLabel * label; Dijkstra dijkstra = Dijkstra(); Floyd floyd = Floyd(); Kruskal kruskal = Kruskal(); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("Map Path Simulator"); ui->mapa->setPixmap(QPixmap(":/Images/mapa.png")); layout = new QVBoxLayout(this); //for(int i = 0; i < 54; i++) //{ //label = new QLabel("hola"); //QString hola; //hola.append("hola"); //label->setText(hola); //layout->addWidget(label); //}
int main(int argc, char *argv[]) { Dijkstra(0); return 0; }