├── README.md └── future_net ├── 50points.rar ├── 80points.rar ├── CMakeLists.txt ├── HUAWEI Code Craft 2016 初赛题目.pdf ├── HUAWEI_dfs_Dijk ├── Common.cpp ├── Common.h ├── Dijkstra.cpp ├── HuaWei.cpp ├── ReadMe.txt ├── Util.cpp ├── Util.h ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── README.md ├── books ├── An-improved-solution-algorithm-for-the-constrained-shortest-path-problem_2007_Transportation-Research-Part-B-Methodological.pdf ├── Constraints.pdf ├── Dijkstra及基于Dijkstra的前N条最短路径算法在智能交通系统中的应用.pdf ├── K最短路径算法综述.pdf ├── LKH.pdf ├── SHORTEST_PATH_PROBLEMS_WITH_RESOURCE_CONSTRAINTS .pdf ├── Solutions-of-the-kth-best-route-through-a-network-A-review_1961_Journal-of-Mathematical-Analysis-and-Applications.pdf ├── [啊哈!算法].啊哈磊.扫描版.pdf ├── ¶à·¾¶QoS·ÓÉËã·¨Ñо¿_Àî·å.caj ├── »ùÓÚ¶àÔ¼ÊøÌõ¼þµÄ×î¶Ì·¾¶Ëã·¨Ñо¿_Íõ½­.caj ├── 一种基于Dijkstra最短路径算法的改进算法.pdf ├── 利用动态规划数学模型求最短路径.pdf ├── 前N条最短路径问题的算法及应用.pdf ├── 北京奥运公交专线规划及评价方法.pdf ├── 基于Petri网的变数的交通网络建模及其最短路径算法研究.pdf ├── 基于图论的VLSI中最小斯坦纳树问题及其改进算法.pdf ├── 基于边集数组的spfa算法及其应用.pdf ├── 基于遗传算法的最短路径路由优化算法.pdf ├── 新的k最短路算法.pdf ├── 智能交通中的高效多准最短路径混合算法.pdf ├── 最短路径动态规划问题及C语言实现.pdf ├── 最短路径问题及其解法研究.pdf ├── 有具体数学方法的逼近论文.pdf ├── 权值为非负整数的稀疏图的高效APSP算法.pdf ├── 求图中受顶点数限制的所有最短路径的算法.pdf ├── 求带多个限制条件的单源多权最短路径算法.pdf ├── 求解最短路径的遗传算法中若干问题的讨论.pdf ├── 用节点子树剪枝构造网络最短路径.pdf └── 经过指定的中间节点集的最短路径算法.pdf ├── case.zip ├── case2.rar ├── case4.zip ├── codecraft_huawei_com_home_detail.pdf ├── future_net.cpp ├── information.md ├── io.cpp ├── lib ├── lib_io.h ├── lib_record.h └── lib_time.h ├── readme.txt ├── route.cpp ├── route.h ├── vectorALGOR ├── Kruskal.cpp ├── dijkstra.cpp ├── kruskal.java ├── ksp-1.0.tar.gz └── prim.cpp └── 官方新的测试样例.rar /README.md: -------------------------------------------------------------------------------- 1 | # alogrithm practice 2 | -------------------------------------------------------------------------------- /future_net/50points.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/50points.rar -------------------------------------------------------------------------------- /future_net/80points.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/80points.rar -------------------------------------------------------------------------------- /future_net/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # CMake 最低版本号要求 2 | cmake_minimum_required(VERSION 2.8) 3 | 4 | # 项目信息 5 | project(future_net) 6 | 7 | # include路径 8 | include_directories(${PROJECT_SOURCE_DIR}/lib) 9 | 10 | # 设置可执行文件生成路径 11 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin) 12 | 13 | # 生成debug版本 14 | SET(CMAKE_BUILD_TYPE "Debug") 15 | SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") 16 | SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") 17 | 18 | # 查找当前目录下的所有源文件 19 | # 并将名称保存到 DIR_LIB_SRCS 变量 20 | aux_source_directory(. DIR_SRCS) 21 | 22 | # 指定生成目标 23 | add_executable(future_net ${DIR_SRCS}) 24 | -------------------------------------------------------------------------------- /future_net/HUAWEI Code Craft 2016 初赛题目.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/HUAWEI Code Craft 2016 初赛题目.pdf -------------------------------------------------------------------------------- /future_net/HUAWEI_dfs_Dijk/Common.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/HUAWEI_dfs_Dijk/Common.cpp -------------------------------------------------------------------------------- /future_net/HUAWEI_dfs_Dijk/Common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/HUAWEI_dfs_Dijk/Common.h -------------------------------------------------------------------------------- /future_net/HUAWEI_dfs_Dijk/Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | **ʹÓÃdjistra·½·¨È¥¼ÆËãÏàÓ¦µÄ×î¶Ì·¾¶£¬ÀûÓÃ̰ÐÄËã·¨£¬Ã¿Ò»´ÎÎÒ¶¼Ñ¡È¡×îÓŵĽâ¾ö·½°¸ 3 | *¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ªÃ»Óп¼ÂǵÄÎÊÌ⣺Èç¹ûͬһʱ¿Ì£¬dijkstraÓöµ½ÁËÁ½¸öͬÑùÊÇ×î½üµÄ·¾¶Ó¦¸ÃÔõô°ì£¿ 4 | */ 5 | 6 | #include "stdafx.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | #include "Common.h" 18 | 19 | using namespace std; 20 | 21 | //djistraËã·¨ÖеĻù±¾ÔªËØ 22 | struct djiNode{ 23 | int from;//ÉÏÒ»¸ö½Úµã 24 | int distance;//¾àÀë 25 | int flag;//¸Ã½ÚµãÊÇ·ñÒѾ­¼ÓÈëµ½×î¶Ì·¾¶µ±ÖУ¬0-ûÓУ¬1-ÒѾ­¼ÓÈëÁË 26 | }; 27 | 28 | 29 | //²éÕÒµ±Ç°Â·¾¶×îС£¬¸Ã½Úµã²»ÔÚsJiheÖУ¬ÇÒÆä·¾¶²»Îª-1 30 | int findTheShortestNode(djiNode* djiList,int num,int end,int isTheLastPass){ 31 | int cost=INT_MAX; 32 | int number=-1; 33 | if(isTheLastPass==1){ 34 | for(int i=0;i& alreadyNode,int num){ 56 | for(int i=0;idestinationId)!=alreadyNode.end()){ 65 | node* tmp=previous; 66 | previous=previous->next; 67 | delete tmp; 68 | } 69 | else{ 70 | only=1; 71 | } 72 | } 73 | else{ 74 | if(previous->next==NULL) 75 | break; 76 | node* second=previous->next; 77 | if(find(alreadyNode.begin(),alreadyNode.end(),second->destinationId)!=alreadyNode.end()){ 78 | previous->next=second->next; 79 | delete second; 80 | } 81 | else{ 82 | previous=previous->next; 83 | } 84 | } 85 | } 86 | } 87 | }; 88 | 89 | 90 | 91 | 92 | void updateDJiList(djiNode* djiList,int best,vNode* nodeList,int num){ 93 | for(int i=0;idestinationId].from=best; 101 | djiList[tmp->destinationId].distance=tmp->cost; 102 | tmp=tmp->next; 103 | } 104 | 105 | }; 106 | 107 | /** 108 | *ʹÓÃdjistraË㷨ȥ×ö---ÕâÀï²ÉÓõÄÒ»ÖÖ·½Ê½ÊÇÁÚ½Ó±í£¬Ã¿Ñ¡³öÀ´Ò»¸öÏàÓ¦µÄ½Úµã¾Í¶ÔÁÚ½Ó¾ØÕó×öÒ»´Î¸üÐÂ,×îºóÒ»¸öÊǽڵãµÄÊýÁ¿ 109 | */ 110 | void dijkstraFindWay(vNode* nodeList,vNode* nodeListCopy,int& maxCost,int& isHaveResult,vector& result,int begin,int end,vector& passNode,int num){ 111 | vector alreadyNode;//ÒѾ­×ß¹ýµÄ½Úµã 112 | vector sJihe;//dijkstraËã·¨ÖеÄs¼¯ºÏ 113 | vector timesNode;//Ò»´Î¹ý³ÌÖмÓÈëµÄ½Úµã 114 | vector passedNode;//ÒѾ­¾­¹ýµÄ½Úµã£¬ÓÃÀ´ÅжÏisTheLastPass 115 | djiNode* djiList; 116 | djiList=new djiNode[num]; 117 | maxCost=0; 118 | isHaveResult=0; 119 | int isTheLastPass=0;//ÊÇ·ñÊÇ×îºóÒ»¸öµãÁË 120 | for(int i=0;idestinationId!=end){ 130 | djiList[tmp->destinationId].distance=tmp->cost; 131 | djiList[tmp->destinationId].from=begin; 132 | } 133 | tmp=tmp->next; 134 | } 135 | 136 | alreadyNode.push_back(begin); 137 | 138 | passNode.push_back(end); 139 | 140 | /** 141 | *̰À·djistraËã·¨µÄ¾ßÌåÁ÷³ÌÈçÏ 142 | *Ò»¡¢Ö´ÐÐÒ»´Î²¿·ÖdjistraËã·¨£¬Ñ¡³ö¾àÀë×î½üµÄpassNodeÖеĽڵ㣬passNodeÖеÄËùÓнڵãûÓÐÈ«²¿×ßÍ꣬ÇÒÕÒ²»µ½¾àÀë×î½üµÄ½Úµã£¬ÔòÇ¿ÐнáÊø£¬ÎÞ½â 143 | **1-1¡¢°Ñ³õʼ½ÚµãѹÈësJihe£¬È»ºó¼ÆËãµÚÒ»¸ö×î½üµÄ·£¬È»ºóѹÈësJihe 144 | **1-2¡¢°´ÕÕdjistraËã·¨¸üÐÂdjiList£¬Ñ­»·Ö´ÐÐ1-1£¬Ö±µ½Ñ¹ÈëÕ»µÄ½ÚµãÊÇpassNodeÖеĽڵ㣬Èç¹ûÕÒ²»µ½×î½ü½Úµã£¬ÔòÌø³öÑ­»·£¬ÓÎÏ·½áÊø 145 | **1-3¡¢·´Ïò»ØËÝÕÒµ½¾­¹ýµÄ½Úµã˳Ðò£¬Ñ¹ÈëÕ»ÖУ¬¸üÐÂmaxCost 146 | *¶þ¡¢¸üÐÂnodeList£¬alreadyNode£¬sJiheºÍdjiList 147 | **/ 148 | while(true){ 149 | sJihe.push_back(alreadyNode.at(alreadyNode.size()-1)); 150 | djiList[sJihe.at(0)].flag=1; 151 | int best=-1; 152 | while(find(passNode.begin(),passNode.end(),best)==passNode.end()){//Ñ­»·²éÕÒ×îÓŵĽڵ㣬ֱµ½×îÓŵĽڵãÔÚpassNodeÖÐ 153 | best=findTheShortestNode(djiList,num,end,isTheLastPass); 154 | if(best==-1)//ûÓÐ×î¼Ñ 155 | break; 156 | sJihe.push_back(best);//°Ñµ±Ç°×îÓŽڵã·ÅÈësJiheµ±ÖÐ 157 | djiList[best].flag=1;//µ±Ç°½ÚµãÒѾ­¸üе½×îÓнڵã 158 | 159 | //¸üÐÂÒ»ÏÂdjiList 160 | int cost=djiList[best].distance; 161 | node* tmps=nodeListCopy[best].next; 162 | while(tmps!=NULL){ 163 | if((djiList[tmps->destinationId].from==-1&&djiList[tmps->destinationId].flag==0)||(cost+tmps->cost)destinationId].distance){//Èç¹û¼ÓÈë×î¶ÌµÄ¾àÀëÖ®ºó±ä¶Ì»òÕß֮ǰ¸ù±¾ÎÞ·¨µ½´ï 164 | djiList[tmps->destinationId].distance=cost+tmps->cost; 165 | djiList[tmps->destinationId].from=best; 166 | } 167 | tmps=tmps->next; 168 | } 169 | } 170 | 171 | /** 172 | *ÕÒ³öÁËÒ»¸ö·ûºÏÌõ¼þµÄbest½Úµã£¬°´ÕÕ´¦ÀíµÄ¸´Ôӳ̶ȷÖΪÒÔÏÂÈýÖÖÇé¿ö 173 | *Ò»¡¢best=-1 ûÓÐÕÒµ½ÏàÓ¦µÄ·¾¶ 174 | *¶þ¡¢best=end ÒѾ­×ßµ½ÖÕµãÁË£¬ÎÒÃÇÕÒµ½ÁËÒ»Ìõ·¾¶ 175 | *Èý¡¢ÆÕͨÇé¿ö£¬ÐèÒª¸üÐÂһϵÁеÄÊý¾Ý£¬Ñ°ÕÒÏÂÒ»¸ö½Úµã£¬Õâ¸öʱºòÐèҪȥÅжÏÒ»ÏÂÊÇ·ñµ½´ï×îºóÒ»¸ö½ÚµãÁË 176 | */ 177 | /**--------------------------------------------ÅжϿªÊ¼-----------------------------------------------------------*/ 178 | if(best==-1){ 179 | break;//ûɶ˵µÄ£¬Ö±½ÓÖÕÖ¹ 180 | } 181 | else {//ÎÒÃǵõ½ÁËÒ»¸öºÏ·¨µÄbest½Úµã£¬Ê×ÏȰÑÉÏÒ»¸ö½Úµãµ½Õâ¸ö½ÚµãµÄËùÓеÄÐÅϢѹÈëalreadyNode 182 | stack tmpList; 183 | int itmp=best; 184 | while(djiList[itmp].from!=-1){ 185 | tmpList.push(itmp); 186 | itmp=djiList[itmp].from; 187 | } 188 | while(!tmpList.empty()){ 189 | alreadyNode.push_back(tmpList.top()); 190 | //timesNode.push_back(tmpList.top()); 191 | tmpList.pop(); 192 | } 193 | maxCost+=djiList[best].distance; 194 | 195 | //¸üÐÂtimesNode 196 | timesNode.clear(); 197 | for(int j=0;jdestinationId!=back) 210 | nownode=nownode->next; 211 | result.push_back(nownode->linkedId); 212 | } 213 | isHaveResult=1; 214 | break; 215 | } 216 | else{//ûµ½ÖÕµã 217 | //ÎÒÃÇÒª¸üÐÂnodeListCopy,sJihe,djiList 218 | updateNodeList(nodeListCopy,timesNode,num);//¸üÐÂnodeList---ÒòΪnodeListÊÇÿ´Î¶¼¸üУ¬Òò´ËÎÒÖ»ÐèÒª°ÑÕâ´Î¸üÐ·ŽøÈ¥ 219 | sJihe.clear(); 220 | updateDJiList(djiList,best,nodeListCopy,num);//¸üÐÂdjiList 221 | if(passedNode.size()==passNode.size()-1){ 222 | isTheLastPass=1;//µ±Ç°ÊÇ×îºóÒ»¸ö½Úµã 223 | } 224 | } 225 | } 226 | /***----------------------------------ÅжÏÖÕÖ¹¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª*/ 227 | } 228 | 229 | } 230 | 231 | 232 | int main(){ 233 | 234 | double start=GetTickCount(); 235 | 236 | //Ê×ÏȶÁÈ¡ËùÓеÄÎļþ 237 | vector lines; 238 | int num=0; 239 | readFile(lines,num); 240 | cout<<"¹²ÓÐ"< passNode;//¾­¹ý¾­µä 261 | readDemand(begin,end,passNode); 262 | cout<<"Æðʼ½ÚµãΪ"< result; 268 | dijkstraFindWay(nodeList,nodeListCopy,cost,hasResult,result,begin,end,passNode,num+1); 269 | 270 | if(hasResult==1){ 271 | cout<<"Óнá¹û"<O(E * lgV)。 62 | 当是稀疏图的情况时,此时E=V * V/lgV,所以算法的时间复杂度可为O(V^2) 。可以用优先队列进行优化,优 63 | 化后时间复杂度变为0( v * lgn)。 64 | Bellman-Ford 65 | 求单源最短路,可以判断有无负权回路(若有,则不存在最短路),时效性较好,时间复杂度O(VE)。 66 | Bellman-Ford算法是求解单源最短路径问题的一种算法。 67 | 单源点的最短路径问题是指:给定一个加权有向图G和源点s,对于图G中的任意一点v,求从s到v的最短路径。 68 | 与Dijkstra算法不同的是,在Bellman-Ford算法中,边的权值可以为负数。设想从我们可以从图中找到一个环 69 | 路(即从v出发,经过若干个点之后又回到v)且这个环路中所有边的权值之和为负。那么通过这个环路,环路 70 | 中任意两点的最短路径就可以无穷小下去。如果不处理这个负环路,程序就会永远运行下去。 而Bellman-Ford 71 | 算法具有分辨这种负环路的能力。 72 | 73 | SPFA 74 | 是Bellman-Ford的队列优化,时效性相对好,时间复杂度O(kE)。(k< 与Bellman-ford算法类似,SPFA算法采用一系列的松弛操作以得到从某一个节点出发到达图中其它所有节点的 75 | 最短路径。所不同的是,SPFA算法通过维护一个队列,使得一个节点的当前最短路径被更新之后没有必要立刻 76 | 去更新其他的节点,从而大大减少了重复的操作次数。 77 | SPFA算法可以用于存在负数边权的图,这与dijkstra算法是不同的。 78 | 与Dijkstra算法与Bellman-ford算法都不同,SPFA的算法时间效率是不稳定的,即它对于不同的图所需要的时 79 | 间有很大的差别。 80 | 在最好情形下,每一个节点都只入队一次,则算法实际上变为广度优先遍历,其时间复杂度仅为O(E)。另一方 81 | 面,存在这样的例子,使得每一个节点都被入队(V-1)次,此时算法退化为Bellman-ford算法,其时间复杂度为 82 | O(VE)。 83 | SPFA算法在负边权图上可以完全取代Bellman-ford算法,另外在稀疏图中也表现良好。但是在非负边权图中, 84 | 85 | 86 | 为了避免最坏情况的出现,通常使用效率更加稳定的Dijkstra算法,以及它的使用堆优化的版本。通常的SPFA 87 | ` 88 | -------------------------------------------------------------------------------- /future_net/books/An-improved-solution-algorithm-for-the-constrained-shortest-path-problem_2007_Transportation-Research-Part-B-Methodological.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/An-improved-solution-algorithm-for-the-constrained-shortest-path-problem_2007_Transportation-Research-Part-B-Methodological.pdf -------------------------------------------------------------------------------- /future_net/books/Constraints.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/Constraints.pdf -------------------------------------------------------------------------------- /future_net/books/Dijkstra及基于Dijkstra的前N条最短路径算法在智能交通系统中的应用.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/Dijkstra及基于Dijkstra的前N条最短路径算法在智能交通系统中的应用.pdf -------------------------------------------------------------------------------- /future_net/books/K最短路径算法综述.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/K最短路径算法综述.pdf -------------------------------------------------------------------------------- /future_net/books/LKH.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/LKH.pdf -------------------------------------------------------------------------------- /future_net/books/SHORTEST_PATH_PROBLEMS_WITH_RESOURCE_CONSTRAINTS .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/SHORTEST_PATH_PROBLEMS_WITH_RESOURCE_CONSTRAINTS .pdf -------------------------------------------------------------------------------- /future_net/books/Solutions-of-the-kth-best-route-through-a-network-A-review_1961_Journal-of-Mathematical-Analysis-and-Applications.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/Solutions-of-the-kth-best-route-through-a-network-A-review_1961_Journal-of-Mathematical-Analysis-and-Applications.pdf -------------------------------------------------------------------------------- /future_net/books/[啊哈!算法].啊哈磊.扫描版.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/[啊哈!算法].啊哈磊.扫描版.pdf -------------------------------------------------------------------------------- /future_net/books/¶à·¾¶QoS·ÓÉËã·¨Ñо¿_Àî·å.caj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/¶à·¾¶QoS·ÓÉËã·¨Ñо¿_Àî·å.caj -------------------------------------------------------------------------------- /future_net/books/»ùÓÚ¶àÔ¼ÊøÌõ¼þµÄ×î¶Ì·¾¶Ëã·¨Ñо¿_Íõ½­.caj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/»ùÓÚ¶àÔ¼ÊøÌõ¼þµÄ×î¶Ì·¾¶Ëã·¨Ñо¿_Íõ½­.caj -------------------------------------------------------------------------------- /future_net/books/一种基于Dijkstra最短路径算法的改进算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/一种基于Dijkstra最短路径算法的改进算法.pdf -------------------------------------------------------------------------------- /future_net/books/利用动态规划数学模型求最短路径.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/利用动态规划数学模型求最短路径.pdf -------------------------------------------------------------------------------- /future_net/books/前N条最短路径问题的算法及应用.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/前N条最短路径问题的算法及应用.pdf -------------------------------------------------------------------------------- /future_net/books/北京奥运公交专线规划及评价方法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/北京奥运公交专线规划及评价方法.pdf -------------------------------------------------------------------------------- /future_net/books/基于Petri网的变数的交通网络建模及其最短路径算法研究.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/基于Petri网的变数的交通网络建模及其最短路径算法研究.pdf -------------------------------------------------------------------------------- /future_net/books/基于图论的VLSI中最小斯坦纳树问题及其改进算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/基于图论的VLSI中最小斯坦纳树问题及其改进算法.pdf -------------------------------------------------------------------------------- /future_net/books/基于边集数组的spfa算法及其应用.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/基于边集数组的spfa算法及其应用.pdf -------------------------------------------------------------------------------- /future_net/books/基于遗传算法的最短路径路由优化算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/基于遗传算法的最短路径路由优化算法.pdf -------------------------------------------------------------------------------- /future_net/books/新的k最短路算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/新的k最短路算法.pdf -------------------------------------------------------------------------------- /future_net/books/智能交通中的高效多准最短路径混合算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/智能交通中的高效多准最短路径混合算法.pdf -------------------------------------------------------------------------------- /future_net/books/最短路径动态规划问题及C语言实现.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/最短路径动态规划问题及C语言实现.pdf -------------------------------------------------------------------------------- /future_net/books/最短路径问题及其解法研究.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/最短路径问题及其解法研究.pdf -------------------------------------------------------------------------------- /future_net/books/有具体数学方法的逼近论文.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/有具体数学方法的逼近论文.pdf -------------------------------------------------------------------------------- /future_net/books/权值为非负整数的稀疏图的高效APSP算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/权值为非负整数的稀疏图的高效APSP算法.pdf -------------------------------------------------------------------------------- /future_net/books/求图中受顶点数限制的所有最短路径的算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/求图中受顶点数限制的所有最短路径的算法.pdf -------------------------------------------------------------------------------- /future_net/books/求带多个限制条件的单源多权最短路径算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/求带多个限制条件的单源多权最短路径算法.pdf -------------------------------------------------------------------------------- /future_net/books/求解最短路径的遗传算法中若干问题的讨论.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/求解最短路径的遗传算法中若干问题的讨论.pdf -------------------------------------------------------------------------------- /future_net/books/用节点子树剪枝构造网络最短路径.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/用节点子树剪枝构造网络最短路径.pdf -------------------------------------------------------------------------------- /future_net/books/经过指定的中间节点集的最短路径算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/books/经过指定的中间节点集的最短路径算法.pdf -------------------------------------------------------------------------------- /future_net/case.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/case.zip -------------------------------------------------------------------------------- /future_net/case2.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/case2.rar -------------------------------------------------------------------------------- /future_net/case4.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/case4.zip -------------------------------------------------------------------------------- /future_net/codecraft_huawei_com_home_detail.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/codecraft_huawei_com_home_detail.pdf -------------------------------------------------------------------------------- /future_net/future_net.cpp: -------------------------------------------------------------------------------- 1 | #include "route.h" 2 | #include "lib_io.h" 3 | #include "lib_time.h" 4 | #include "stdio.h" 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | print_time("Begin"); 9 | char *topo[5000]; 10 | int edge_num; 11 | char *demand; 12 | int demand_num; 13 | 14 | char *topo_file = argv[1]; 15 | edge_num = read_file(topo, 5000, topo_file); 16 | if (edge_num == 0) 17 | { 18 | printf("Please input valid topo file.\n"); 19 | return -1; 20 | } 21 | char *demand_file = argv[2]; 22 | demand_num = read_file(&demand, 1, demand_file); 23 | if (demand_num != 1) 24 | { 25 | printf("Please input valid demand file.\n"); 26 | return -1; 27 | } 28 | 29 | search_route(topo, edge_num, demand); 30 | 31 | char *result_file = argv[3]; 32 | write_result(result_file); 33 | release_buff(topo, edge_num); 34 | release_buff(&demand, 1); 35 | 36 | print_time("End"); 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /future_net/information.md: -------------------------------------------------------------------------------- 1 | 2 | 赛题源自“未来网络”业务发放中的路由计算问题。算路问题属于基础算法问题,在图论、网络、交通等各个方面均有着广泛的研究与运用,里面不乏一些经典的算法,例如最短路中的广度优先搜索,Dijkstra算法等。网络算路问题的更优算法实现对于网络资源高效配置具有重要价值。 3 | 4 | *** 5 | 6 | 1 问题定义 7 | 给定一个带权重的有向图G=(V,E),V为顶点集,E为有向边集,每一条有向边均有一个权重。对于给定的顶点s、t,以及V的子集V',寻找从s到t的不成环有向路径P,使得P经过V'中所有的顶点(对经过V'中节点的顺序不做要求)。 8 | 若不存在这样的有向路径P,则输出无解,程序运行时间越短,则视为结果越优;若存在这样的有向路径P,则输出所得到的路径,路径的权重越小,则视为结果越优,在输出路径权重一样的前提下,程序运行时间越短,则视为结果越优。 9 | 10 | *** 11 | 12 | 说明: 13 | 1)图中所有权重均为[1,20]内的整数; 14 | 2)任一有向边的起点不等于终点; 15 | 3)连接顶点A至顶点B的有向边可能超过一条,其权重可能一样,也可能不一样; 16 | 4)该有向图的顶点不会超过600个,每个顶点出度(以该点为起点的有向边的数量)不超过8; 17 | 5)V'中元素个数不超过50; 18 | 6)从s到t的不成环有向路径P是指,P为由一系列有向边组成的从s至t的有向连通路径,且不允许重复经过任一节点; 19 | 7)路径的权重是指所有组成该路径的所有有向边的权重之和。 20 | 21 | *** 22 | 23 | 2 输入与输出 24 | 输入文件格式 25 | 以两个.csv 文件(csv 是以逗号为分隔符的文本文件)给出输入数据,一个为图的数据(G),一个为需要计算的路径信息(s,t,V')。文件每行以换行符(ASCII'\n'即0x0a)为结尾。 26 | 27 | 1)图的数据中,每一行包含如下的信息: 28 | LinkID,SourceID,DestinationID,Cost 29 | 其中,LinkID 为该有向边的索引,SourceID 为该有向边的起始顶点的索引,DestinationID为该有向边的终止顶点的索引,Cost 为该有向边的权重。顶点与有向边的索引均从0 开始 编号(不一定连续,但用例保证索引不重复)。 30 | 2)路径信息中,只有一行如下数据: 31 | SourceID,DestinationID,IncludingSet 32 | 其中,SourceID 为该路径的起点,DestinationID 为该路径的终点,IncludingSet 表示必须经过的顶点集合V',其中不同的顶点索引之间用'|'分割。 33 | 34 | 输出文件格式 35 | 输出文件同样为一个.csv 文件。 36 | 1)如果该测试用例存在满足要求的有向路径P,则按P 经过的有向边顺序,依次输出有向边的索引,索引之间用'|'分割; 37 | 2)如果该测试用例不存在满足要求的有向路径P,则输出两个字符NA; 38 | 3)只允许输出最多一条有向路径。 39 | 40 | *** 41 | 42 | 3 单个用例的评分机制 43 | 有解用例的排名机制 44 | 按下面流程对参赛者结果进行排名: 45 | Step1: 对于提交的结果,进行合法性检验(详见题目描述); 46 | Step2: 程序运行时间不得超过10s; 47 | 若不满足上述的结果则本用例得分为0; 48 | 49 | 50 | Step3: 计算提交的路径的权重,权重越小,排名越优; 51 | Step4: 在权重相同的结果里,用程序运行时间进行排名,时间越短,排名越优。 52 | 无解用例的排名机制 53 | 按下列流程对参赛者结果进行排名: 54 | Step1: 对于提交的结果,验证是否识别出该用例无解,若无法识别或者算法运行时间超10s,则本用例得分为0; 55 | Step2: 用程序的运行时间进行排名,时间越短,排名越优。 56 | 单个用例的评分标准如下: 57 | 根据上面排名流程得到的排名,使用标准分计分(排名第一的提交者为100分)。 58 | 若所有人均未得到正确结果,则所有人均得分为0。 59 | 60 | *** 61 | 62 | 4 最终得分机制 63 | 平台会使用N个测试用例判题,该N个测试用例分为初级、中级、高级三个等级,参赛者对于每个测试用例都会得到一个百分制分数,使用加权平均分(初级权重为0.2,中级权重为0.3,高级权重为0.5)作为该参赛者的最终得分。 64 | 特别说明:在比赛初期,平台只放出初级、中级的测试用例,故此时满分为50分,在比赛后期,才会放出高级测:试用例(具体发放时间会在网站公告通知),此时满分才为100分,请各位参赛者注意。 -------------------------------------------------------------------------------- /future_net/io.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define INLINE static __inline 12 | #define PRINT printf 13 | 14 | #define MAX_RECORD 100 15 | #define MAX_LINE_LEN 4000 16 | 17 | typedef struct 18 | { 19 | char *record[MAX_RECORD]; 20 | int head; 21 | int tail; 22 | int cnt; 23 | }RECORD_QUEUE_S; 24 | 25 | static char g_result[MAX_LINE_LEN] = "NA"; 26 | 27 | INLINE void write_file(const bool cover, const char * const buff, const char * const filename); 28 | 29 | void record_result(unsigned short edge) 30 | { 31 | static int len = 0; 32 | if (len > (MAX_LINE_LEN - 10)) 33 | return; 34 | if (len > 0) 35 | len += sprintf(g_result + len, "|"); 36 | len += sprintf(g_result + len, "%d", edge); 37 | } 38 | 39 | void print_time(const char *head) 40 | { 41 | struct timeb rawtime; 42 | struct tm * timeinfo; 43 | ftime(&rawtime); 44 | timeinfo = localtime(&rawtime.time); 45 | 46 | static int ms = rawtime.millitm; 47 | static unsigned long s = rawtime.time; 48 | int out_ms = rawtime.millitm - ms; 49 | unsigned long out_s = rawtime.time - s; 50 | ms = rawtime.millitm; 51 | s = rawtime.time; 52 | 53 | if (out_ms < 0) 54 | { 55 | out_ms += 1000; 56 | out_s -= 1; 57 | } 58 | printf("%s date/time is: %s \tused time is %lu s %d ms.\n", head, asctime(timeinfo), out_s, out_ms); 59 | } 60 | 61 | int read_file(char ** const buff, const unsigned int spec, const char * const filename) 62 | { 63 | FILE *fp = fopen(filename, "r"); 64 | if (fp == NULL) 65 | { 66 | PRINT("Fail to open file %s, %s.\n", filename, strerror(errno)); 67 | return 0; 68 | } 69 | PRINT("Open file %s OK.\n", filename); 70 | 71 | char line[MAX_LINE_LEN + 2]; 72 | unsigned int cnt = 0; 73 | while ((cnt < spec) && !feof(fp)) 74 | { 75 | line[0] = 0; 76 | fgets(line, MAX_LINE_LEN + 2, fp); 77 | if (line[0] == 0) continue; 78 | buff[cnt] = (char *)malloc(MAX_LINE_LEN + 2); 79 | strncpy(buff[cnt], line, MAX_LINE_LEN + 2 - 1); 80 | buff[cnt][4001] = 0; 81 | cnt++; 82 | } 83 | fclose(fp); 84 | //TODO 85 | // for (unsigned int i = 0; i < cnt; ++i) 86 | // { 87 | // // for (int j = 0; i < 3; ++j) 88 | // // { 89 | // // PRINT("%s\n", **buff[i][j]); 90 | // // } 91 | // PRINT("%c\n", *buff[i]); 92 | // } 93 | //end new 94 | PRINT("There are %d lines in file %s.\n", cnt, filename); 95 | 96 | return cnt; 97 | } 98 | 99 | void write_result(const char * const filename) 100 | { 101 | if (g_result[0] == '\0') 102 | return; 103 | 104 | write_file(1, g_result, filename); 105 | } 106 | 107 | void release_buff(char ** const buff, const int valid_item_num) 108 | { 109 | for (int i = 0; i < valid_item_num; i++) 110 | free(buff[i]); 111 | } 112 | 113 | INLINE void write_file(const bool cover, const char * const buff, const char * const filename) 114 | { 115 | if (buff == NULL) 116 | return; 117 | 118 | const char *write_type = cover ? "w" : "a";//1:覆盖写文件,0:追加写文件 119 | FILE *fp = fopen(filename, write_type); 120 | if (fp == NULL) 121 | { 122 | PRINT("Fail to open file %s, %s.\n", filename, strerror(errno)); 123 | return; 124 | } 125 | PRINT("Open file %s OK.\n", filename); 126 | fputs(buff, fp); 127 | fputs("\n", fp); 128 | fclose(fp); 129 | } 130 | 131 | -------------------------------------------------------------------------------- /future_net/lib/lib_io.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIB_IO_H__ 2 | #define __LIB_IO_H__ 3 | 4 | //读取文件并按行输出到buff。 5 | //buff为一个指针数组,每一个元素是一个字符指针,对应文件中一行的内容。 6 | //spec为允许解析的最大行数。 7 | extern int read_file(char ** const buff, const unsigned int spec, const char * const filename); 8 | 9 | //将result缓冲区中的内容写入文件,写入方式为覆盖写入 10 | extern void write_result(const char * const filename); 11 | 12 | //释放读文件的缓冲区 13 | extern void release_buff(char ** const buff, const int valid_item_num); 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /future_net/lib/lib_record.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIB_RECORD_H__ 2 | #define __LIB_RECORD_H__ 3 | 4 | //将结果记录到result缓冲区 5 | extern void record_result(const unsigned short edge); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /future_net/lib/lib_time.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIB_TIME_H__ 2 | #define __LIB_TIME_H__ 3 | 4 | //打印时间。入参为打印信息头 5 | void print_time(const char * const head); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /future_net/readme.txt: -------------------------------------------------------------------------------- 1 | 看到中文readme是一件非常亲切的事情!仔细看完此文档即可完成第一个用例: 2 | 3 | 1、整体指引: 4 | 1)使用一键式脚本编译、链接并打包压缩,如果编译失败请自行解决编译问题; 5 | 2)如果编译成功会在bin/目录下生成可执行二进制文件"future_net"; 6 | 3)使用如下格式调用并调试程序"./future_net /xxx/topo.csv /xxx/demand.csv /xxx/result.csv",其中topo.csv和demand.csv是输入文件,result.csv是输出文件; 7 | 4)调试成功后到竞赛官网提交SDK-gcc/目录下的压缩包"future_net.tar.gz",稍后查询成绩。 8 | 9 | 2、目录结构: 10 | SDK-gcc/ 11 | ├── bin/ 可执行二进制文件目录,shell脚本在编译前删除此目录并重新创建此目录,故没有此目录不会影响脚本运行 12 | ├── build/ 构建目录,shell脚本在编译前删除此目录并重新创建此目录,故没有此目录不会影响脚本运行 13 | ├── future_net/ 代码目录 14 | │ ├── lib/ 15 | │ │ ├── lib_io.h 读写文件的头文件 16 | │ │ ├── lib_record.h 将输出结果记录到缓冲区的头文件 17 | │ │ └── lib_time.h 打印时间的头文件 18 | │ ├── CMakeLists.txt cmake 19 | │ ├── future_net.cpp main函数源文件 20 | │ ├── route.cpp 你要写代码的源文件 21 | │ └── route.h 你要写代码的头文件 22 | ├── batch.sh 编译、链接、打包批处理脚本 23 | └── readme.txt 你正在看的文件 -_-" 这不用介绍了吧 24 | 25 | 3、shell脚本说明: 26 | 执行此脚本可以一键编译、链接、打包。如果编译和链接正确,会在bin/下生成future_net二进制文件,并按照大赛要求生成二进制文件与代码的压缩打包文件存处于SDK/下。 27 | 注意: 28 | 1)shell脚本会删除bin/和build/目录,以及这两个目录下的所有文件和目录。请不要在此保存你的任何文档; 29 | 2)如果想使用shell脚本一键功能,请保持SDK-gcc/目录下所有内容的完整,请不要修改任何目录名和文件名,并保持各目录和文件的位置关系不变。 30 | 31 | 4、手工操作说明:(非必须。如果选择使用shell脚本构建,可忽略本节内容) 32 | 1)在SDK-gcc/目录下创建build_private/目录,并在build_private/下编写makefile文件; 33 | 2)进入build_private/,执行make完成编译和链接; 34 | 3)将生成的二进制文件和代码目录置于同一级目录下,打包压缩生成"future_net.tar.gz"。 35 | 注意: 36 | 1)不要在build/下保存你的makefile文件,一旦调用batch.sh将会删除你的makefile文件; 37 | 2)生成压缩包时,应确保将future_net二进制文件置于压缩包的最外层,即打开压缩包无需进入任何目录即可看到此二进制文件,否则可能影响判题结果; 38 | 3)生成的二进制文件取名必须为future_net,否则会影响判题结果。 39 | 40 | 5、SDK代码说明: 41 | 我们已经提供了保姆式的服务,你只需要做: 42 | 1)实现route.cpp文件中的search_route接口; 43 | 2)依次调用record_result将路径结果写入缓冲区; 44 | 3)如果计算结果为没有路径,则不需要调用record_result接口即可直接输出NA。 45 | SDK已经实现了读取文件、按要求格式写文件以及打印开始和结束时间的功能。 46 | 注意:读取文件功能是指,将图的信息文件和路径信息文件按行读取到内存,其在内存中的存储格式仍是字符串格式。因为这些信息以什么格式存储涉及到算法设计,这样做是为了不禁锢你的思路。 47 | -------------------------------------------------------------------------------- /future_net/route.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "route.h" 3 | #include "lib_record.h" 4 | 5 | 6 | 7 | 8 | 9 | 10 | //TODO 11 | // IMPORTTANT!!!! 12 | // 0 function searchPath(...) return a wrong value , such as 0to19 , 13 | // we can get it but it print "no path" 14 | // 15 | // 1 存储方式由邻接矩阵转化为邻接表 16 | // 2 实现pdf中的算法 17 | // 3 注意输出路径结果为边的编号而不是顶点的编号 18 | // 19 | // 20 | // 21 | 22 | 23 | 24 | 25 | 26 | char *result_path[5000]; 27 | int path_num = 0; 28 | 29 | /* n == end point 30 | * v==start point 31 | */ 32 | int Dijkstra(int n, int v, int *dist, int *prev, AtoB c[maxnum][maxnum]) 33 | { 34 | 35 | bool s[maxnum]; // 判断是否已存入该点到S集合中 36 | for(int i=1; i<=n; ++i) 37 | { 38 | dist[i] = c[v][i].dist; 39 | s[i] = 0; // 初始都未用过该点 40 | if(dist[i] == maxint) 41 | prev[i] = 0; 42 | else 43 | prev[i] = v; 44 | } 45 | dist[v] = 0; 46 | s[v] = 1; 47 | 48 | // 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中 49 | // 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度 50 | for(int i=2; i<=n; ++i) 51 | { 52 | int tmp = maxint; 53 | int u = v; 54 | // 找出当前未使用的点j的dist[j]最小值 55 | for(int j=1; j<=n; ++j) 56 | if((!s[j]) && dist[j]=1; --i){ 119 | if(i != 1){ 120 | cout << que[i] << " -> "; 121 | 122 | } 123 | else{ 124 | cout << que[i] << endl; 125 | } 126 | } 127 | } 128 | 129 | 130 | 131 | 132 | int midPath(int IncludingSetLength, int IncludingSet[50], AtoB c[maxnum][maxnum]) 133 | { 134 | 135 | //cout << "get mid path" << endl; 136 | int midDist = 0; 137 | int distance[maxnum]; // 表示当前点到源点的最短路径长度 138 | int prevpoint[maxnum]; // 记录当前点的前一个结点 139 | 140 | for(int i=0; i<=maxnum; ++i) 141 | distance[i] = maxint; 142 | 143 | for (int i = 0; i+1 < IncludingSetLength; ++i) 144 | { 145 | 146 | /* 147 | * TODO use if {}else{} to cut runtime 148 | * 149 | * 150 | */ 151 | 152 | // if (c[ IncludingSet[i] ][ IncludingSet[i+1] ].dist != inf) 153 | // { 154 | // midDist += c[ IncludingSet[i] ][ IncludingSet[i+1] ].dist; 155 | // }else{ 156 | midDist += Dijkstra(IncludingSet[i+1], IncludingSet[i], distance, prevpoint, c); 157 | //cout << IncludingSet[i] << "到" << IncludingSet[i+1] <<"的路径: " << endl; 158 | searchPath(prevpoint, IncludingSet[i], IncludingSet[i+1]); 159 | 160 | 161 | 162 | 163 | } 164 | // for (int i = 0; i < IncludingSetLength; ++i) 165 | // { 166 | 167 | // printf( "access :%d ", IncludingSet[i]); 168 | // } 169 | 170 | if (midDist >= inf) 171 | { 172 | printf( "no result\n" ); 173 | }else{ 174 | printf( "distance :%d \n" ,midDist); 175 | } 176 | 177 | 178 | return midDist; 179 | } 180 | 181 | 182 | //the default function given(entrance) 183 | void search_route(char *topo[5000], int edge_num, char *demand) 184 | { 185 | //unsigned short result[] = {2, 6, 4}; 186 | 187 | AtoB distM[maxnum][maxnum]; 188 | 189 | int begin; 190 | int end; 191 | int id; 192 | int dist; 193 | 194 | 195 | /* 196 | *init the matrix 197 | * 198 | * 199 | */ 200 | 201 | //TODO use dist[] not matrix to change 600 202 | 203 | for (int i = 0; i < maxnum; ++i) 204 | { 205 | for (int j = 0; j < maxnum; ++j) 206 | { 207 | if (i == j) 208 | { 209 | distM[i][j].dist = 0; 210 | }else{ 211 | distM[i][j].dist = inf; 212 | } 213 | } 214 | } 215 | 216 | 217 | 218 | // for (int i = 0; i < edge_num; i++){ 219 | 220 | // int begin = ((int)*(topo[i]+2) - 48); 221 | // int end = ((int)*(topo[i]+4) - 48); 222 | 223 | // distM[begin][end].id = ((int)*(topo[i]) - 48); 224 | 225 | // distM[begin][end].dist = ((int)*(topo[i]+6)-48); 226 | 227 | // } 228 | 229 | /* format the matrix */ 230 | for (int i = 0; i < edge_num; ++i) 231 | { 232 | char* token = strtok( topo[i], ","); 233 | int k = 0; 234 | while( token != NULL ) { 235 | 236 | //printf( "result is %s\n", result ); 237 | if (k == 0) 238 | { 239 | id = atoi(token); 240 | }else if (k == 1) 241 | { 242 | begin = atoi(token); 243 | }else if (k == 2) 244 | { 245 | end = atoi(token); 246 | }else if (k ==3) 247 | { 248 | dist = atoi(token); 249 | } 250 | k++; 251 | token = strtok( NULL, "," ); 252 | 253 | } 254 | 255 | 256 | distM[begin][end].id = id; 257 | 258 | distM[begin][end].dist = dist; 259 | 260 | printf( "from %d to %d ,token is :%d ", begin, end, distM[begin][end].dist ); 261 | 262 | //token = strtok( NULL, ","); 263 | printf("\n"); 264 | } 265 | 266 | printf("\n\n\n\n"); 267 | 268 | /* 269 | * get the necessary point which is in demand.csv 270 | * 271 | */ 272 | 273 | /* for demand */ 274 | 275 | int SourceID; 276 | int DestinationID; 277 | int IncludingSet[5000]; 278 | 279 | //int len = strlen(demand); 280 | //printf("length is :%d\t",len); 281 | char* token = strtok( demand, ",|"); 282 | int i = 0; 283 | int IncludingSetLength = 0; 284 | 285 | while( token != NULL ) 286 | { 287 | /* While there are tokens in "string" */ 288 | //printf( "token is :%s ", token ); 289 | 290 | if (i == 0) 291 | { 292 | SourceID = atoi(token); 293 | }else if (i ==1) 294 | { 295 | DestinationID = atoi(token); 296 | }else if (i >= 2) 297 | { 298 | IncludingSet[IncludingSetLength] = atoi(token); 299 | IncludingSetLength++; 300 | } 301 | 302 | /* Get next token: */ 303 | 304 | token = strtok( NULL, ",|"); 305 | i++; 306 | } 307 | printf( "start :%d \n", SourceID ); 308 | printf( "end :%d \n", DestinationID ); 309 | printf( "number :%d \n", IncludingSetLength); 310 | // for (int i = 0; i < IncludingSetLength; ++i) 311 | // { 312 | 313 | // printf( "access :%d ", IncludingSet[i] ); 314 | 315 | // } 316 | 317 | 318 | int arrNum = 1; 319 | int midDist = inf; 320 | 321 | int totalDist = inf; 322 | 323 | int firstDist = inf; 324 | int lastDist = inf; 325 | 326 | int tempDist = 0; 327 | 328 | int firstdistance[maxnum]; // 表示当前点到源点的最短路径长度 329 | int firstprevpoint[maxnum] = {0}; // 记录当前点的前一个结点 330 | 331 | for(int i=0; i<=edge_num/2; ++i) 332 | firstdistance[i] = maxint; 333 | 334 | int lastdistance[maxnum]; // 表示当前点到源点的最短路径长度 335 | int lastprevpoint[maxnum] = {0}; // 记录当前点的前一个结点 336 | 337 | for(int i=0; i<=edge_num/2; ++i) 338 | lastdistance[i] = maxint; 339 | 340 | int copyIncludingSet[maxnum]; 341 | 342 | do 343 | { 344 | /* Dijkstra(int SourceID, int DestinationID, int distance[], int prevpoint[], AtoB distM[][]);*/ 345 | 346 | midDist = midPath(IncludingSetLength, IncludingSet, distM); 347 | firstDist = Dijkstra(IncludingSet[0] , SourceID, firstdistance, firstprevpoint, distM); 348 | lastDist = Dijkstra(DestinationID, IncludingSet[IncludingSetLength], lastdistance, lastprevpoint, distM); 349 | 350 | tempDist = firstDist + midDist + lastDist; 351 | cout << firstDist << " " << midDist << " " << lastDist << "\n" ; 352 | if (tempDist < totalDist) 353 | { 354 | totalDist = tempDist; 355 | //copy the shortest IncludingSet to the new array 356 | //the last time we copied is the shortest IncludingSet 357 | 358 | //memcpy(copyIncludingSet,IncludingSet, IncludingSetLength*sizeof(int)); 359 | 360 | for (int i = 0; i < IncludingSetLength; ++i) 361 | { 362 | copyIncludingSet[i] = IncludingSet[i]; 363 | } 364 | cout << "\n\n\n\n\n\n\n\n\n\n copy \n\n\n\n\n\n\n" ; 365 | 366 | } 367 | 368 | arrNum++; 369 | 370 | }while (next_permutation(IncludingSet, (IncludingSet+IncludingSetLength))); 371 | 372 | cout << "array number is :" << arrNum-1 << endl; 373 | 374 | printf("\n\n\n\n\n"); 375 | for (int i = 0; i < IncludingSetLength; ++i) 376 | { 377 | printf("access %d", copyIncludingSet[i]); 378 | } 379 | printf("\n\n\n\n\n"); 380 | 381 | midDist = midPath(IncludingSetLength, IncludingSet, distM); 382 | 383 | //firstDist = Dijkstra(IncludingSet[0] , SourceID, firstdistance, firstprevpoint, distM); 384 | searchPath(firstprevpoint, SourceID, IncludingSet[0]); 385 | 386 | //lastDist = Dijkstra(DestinationID, IncludingSet[IncludingSetLength], lastdistance, lastprevpoint, distM); 387 | searchPath(lastprevpoint, IncludingSet[IncludingSetLength], DestinationID); 388 | 389 | printf("\n\n\n\n\n"); 390 | 391 | 392 | 393 | 394 | // int distance[maxnum]; // 表示当前点到源点的最短路径长度 395 | // int prevpoint[maxnum]; // 记录当前点的前一个结点 396 | 397 | // for(int i=0; i<=edge_num/2; ++i) 398 | // distance[i] = maxint; 399 | 400 | // int case0; 401 | 402 | // // you can set startpoint and endpoint as you want 403 | // case0 = Dijkstra(DestinationID, SourceID, distance, prevpoint, distM); 404 | 405 | // // 最短路径长度 406 | // cout << "startPoint到endPoint的最短路径长度: " << case0 << endl; 407 | 408 | // cout << "tartPoint到endPoint的的路径: " << endl; 409 | // searchPath(prevpoint, SourceID, DestinationID); 410 | 411 | /* wirte to file */ 412 | // for (int i = 0; i < path_num; i++) 413 | // record_result(*result_path[i]); 414 | } 415 | -------------------------------------------------------------------------------- /future_net/route.h: -------------------------------------------------------------------------------- 1 | #ifndef __ROUTE_H__ 2 | #define __ROUTE_H__ 3 | 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | 14 | const int maxnum = 200; 15 | const int maxint = 999999; 16 | const int inf = 999999; 17 | 18 | 19 | 20 | struct AtoB{ 21 | int dist; 22 | int id; 23 | int route[50]; 24 | }; 25 | 26 | void searchPath(int *prev,int v, int u); 27 | 28 | int Dijkstra(int n, int v, int *dist, int *prev, AtoB c[maxnum][maxnum]); 29 | 30 | int midPath(int IncludingSetLength, int IncludingSet[50], AtoB c[maxnum][maxnum]); 31 | 32 | void search_route(char *graph[5000], int edge_num, char *condition); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /future_net/vectorALGOR/Kruskal.cpp: -------------------------------------------------------------------------------- 1 | typedef struct 2 | { 3 | char vertex[VertexNum]; //顶点表 4 | int edges[VertexNum][VertexNum]; //邻接矩阵,可看做边表 5 | int n,e; //图中当前的顶点数和边数 6 | }MGraph; 7 | 8 | typedef struct node 9 | { 10 | int u; //边的起始顶点 11 | int v; //边的终止顶点 12 | int w; //边的权值 13 | }Edge; 14 | 15 | void kruskal(MGraph G) 16 | { 17 | int i,j,u1,v1,sn1,sn2,k; 18 | int vset[VertexNum]; //辅助数组,判定两个顶点是否连通 19 | int E[EdgeNum]; //存放所有的边 20 | k=0; //E数组的下标从0开始 21 | for (i=0;i %d, %d",E[j].u,E[j].v,E[j].w); 48 | k++; 49 | for (i=0;i 3 | using namespace std; 4 | 5 | const int maxnum = 100; 6 | const int maxint = 999999; 7 | 8 | 9 | void Dijkstra(int n, int v, int *dist, int *prev, int c[maxnum][maxnum]) 10 | { 11 | bool s[maxnum]; // 判断是否已存入该点到S集合中 12 | for(int i=1; i<=n; ++i) 13 | { 14 | dist[i] = c[v][i]; 15 | s[i] = 0; // 初始都未用过该点 16 | if(dist[i] == maxint) 17 | prev[i] = 0; 18 | else 19 | prev[i] = v; 20 | } 21 | dist[v] = 0; 22 | s[v] = 1; 23 | 24 | // 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中 25 | // 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度 26 | for(int i=2; i<=n; ++i) 27 | { 28 | int tmp = maxint; 29 | int u = v; 30 | // 找出当前未使用的点j的dist[j]最小值 31 | for(int j=1; j<=n; ++j) 32 | if((!s[j]) && dist[j]=1; --i) 68 | if(i != 1) 69 | cout << que[i] << " -> "; 70 | else 71 | cout << que[i] << endl; 72 | } 73 | 74 | int main() 75 | { 76 | freopen("input.txt", "r", stdin); 77 | // 各数组都从下标1开始 78 | int dist[maxnum]; // 表示当前点到源点的最短路径长度 79 | int prev[maxnum]; // 记录当前点的前一个结点 80 | int c[maxnum][maxnum]; // 记录图的两点间路径长度 81 | int n, line; // 图的结点数和路径数 82 | 83 | // 输入结点数 84 | cin >> n; 85 | // 输入路径数 86 | cin >> line; 87 | int p, q, len; // 输入p, q两点及其路径长度 88 | 89 | // 初始化c[][]为maxint 90 | for(int i=1; i<=n; ++i) 91 | for(int j=1; j<=n; ++j) 92 | c[i][j] = maxint; 93 | 94 | for(int i=1; i<=line; ++i) 95 | { 96 | cin >> p >> q >> len; 97 | if(len < c[p][q]) // 有重边 98 | { 99 | c[p][q] = len; // p指向q 100 | c[q][p] = len; // q指向p,这样表示无向图 101 | } 102 | } 103 | 104 | for(int i=1; i<=n; ++i) 105 | dist[i] = maxint; 106 | for(int i=1; i<=n; ++i) 107 | { 108 | for(int j=1; j<=n; ++j) 109 | printf("%8d", c[i][j]); 110 | printf("\n"); 111 | } 112 | 113 | Dijkstra(n, 1, dist, prev, c); 114 | 115 | // 最短路径长度 116 | cout << "源点到最后一个顶点的最短路径长度: " << dist[n] << endl; 117 | 118 | // 路径 119 | cout << "源点到最后一个顶点的路径为: "; 120 | searchPath(prev, 1, n); 121 | } -------------------------------------------------------------------------------- /future_net/vectorALGOR/kruskal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * sample Kruskal.java Description: 3 | * kruskal算法的思想是找最小边,且每次找到的边不会和以找出来的边形成环路,利用一个一维数组group存放当前顶点所在连通图标示(每条最小边,属于一个连通图),直到顶点都找完 4 | * 1.0 YESUN Jul 18, 5 | * 2013 8:48:28 AM Create. ChangeLog: 6 | */ 7 | public class Kruskal { 8 | 9 | 10 | 11 | /** 12 | * Description: 13 | * 14 | * @param args 15 | * 1.0 YESUN Jul 18, 2013 8:47:10 AM Create. ChangeLog: 16 | */ 17 | public static void main(String[] args) { 18 | int[][] map = new int[][]{ 19 | {0,10,MAX,MAX,MAX,11,MAX,MAX,MAX}, 20 | {10,0,18,MAX,MAX,MAX,16,MAX,12}, 21 | {MAX,MAX,0,22,MAX,MAX,MAX,MAX,8}, 22 | {MAX,MAX,22,0,20,MAX,MAX,16,21}, 23 | {MAX,MAX,MAX,20,0,26,MAX,7,MAX}, 24 | {11,MAX,MAX,MAX,26,0,17,MAX,MAX}, 25 | {MAX,16,MAX,MAX,MAX,17,0,19,MAX}, 26 | {MAX,MAX,MAX,16,7,MAX,19,0,MAX}, 27 | {MAX,12,8,21,MAX,MAX,MAX,MAX,0} 28 | }; 29 | kruskal(map); 30 | } 31 | 32 | static int MAX = Integer.MAX_VALUE; 33 | 34 | /** 35 | * Description: by yesun 36 | * @param arcs 37 | * 1.0 YESUN Jul 18, 2013 1:42:42 PM Create. 38 | * ChangeLog: 39 | */ 40 | public static void kruskal(int[][] arcs) { 41 | //顶点个数 42 | int num = arcs.length; 43 | //存放对应顶点所在连通图标识 44 | int[] group = new int[num]; 45 | 46 | int sum = 0, n1 = 0, n2 = 0; 47 | boolean finished = false; 48 | int groupNum = 1; 49 | 50 | while(!finished) { 51 | int min = Integer.MAX_VALUE; 52 | //找出所有边中最小值 53 | for(int i = 0; i < num; i++) { 54 | for(int j = i+1; j < num; j++) { 55 | if(arcs[i][j] > 0 && arcs[i][j] < min){ 56 | //the point which have been search are put in one group 57 | //如果group相同,则表示处理过,不相同或都为0都表示没处理过 58 | if (group[i] != group[j] || (group[i] == 0 && group[j] == 0)) { 59 | min = arcs[i][j]; 60 | n1 = i; 61 | n2 = j; 62 | } 63 | } 64 | } 65 | } 66 | 67 | if(min == Integer.MAX_VALUE){ 68 | continue; 69 | } 70 | 71 | System.out.println(n1 + " ---> " + n2 + " " + min); 72 | sum += min; 73 | 74 | //找到了最小值,设置连通标记 75 | if(group[n1] == 0 && group[n2] == 0){ 76 | group[n1] = groupNum; 77 | group[n2] = groupNum; 78 | groupNum++; 79 | } 80 | else if(group[n1] > 0 && group[n2] > 0) { 81 | int tmp = group[n2]; 82 | for(int m = 0; m < group.length; m++){ 83 | if(group[m] == tmp){ 84 | group[m] = group[n1]; 85 | } 86 | } 87 | } 88 | else{ 89 | if(group[n1] == 0){ 90 | group[n1] = group[n2]; 91 | } 92 | else{ 93 | group[n2] = group[n1]; 94 | } 95 | } 96 | 97 | for(int i = 0; i < group.length; i++) { 98 | if(group[i] != group[0]){ 99 | finished = false; 100 | break; 101 | }//all if this array's value is same 102 | else{ 103 | finished = true; 104 | } 105 | } 106 | 107 | if(finished) { 108 | break; 109 | } 110 | } 111 | 112 | System.out.println(" sum:"+sum); 113 | 114 | } 115 | 116 | } -------------------------------------------------------------------------------- /future_net/vectorALGOR/ksp-1.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/vectorALGOR/ksp-1.0.tar.gz -------------------------------------------------------------------------------- /future_net/vectorALGOR/prim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MaxInt 0x3f3f3f3f 4 | #define N 110 5 | //创建map二维数组储存图表,low数组记录每2个点间最小权值,visited数组标记某点是否已访问 6 | int map[N][N],low[N],visited[N]; 7 | int n; 8 | 9 | int prim() 10 | { 11 | int i,j,pos,min,result=0; 12 | memset(visited,0,sizeof(visited)); 13 | //从某点开始,分别标记和记录该点 14 | visited[1]=1;pos=1; 15 | //第一次给low数组赋值 16 | for(i=1;i<=n;i++) 17 | if(i!=pos) low[i]=map[pos][i]; 18 | //再运行n-1次 19 | for(i=1;ilow[j]) 25 | { 26 | min=low[j];pos=j; 27 | } 28 | //最小权值累加 29 | result+=min; 30 | //标记该点 31 | visited[pos]=1; 32 | //更新权值 33 | for(j=1;j<=n;j++) 34 | if(visited[j]==0&&low[j]>map[pos][j]) 35 | low[j]=map[pos][j]; 36 | } 37 | return result; 38 | } 39 | 40 | int main() 41 | { 42 | int i,v,j,ans; 43 | while(scanf("%d",&n)!=EOF) 44 | { 45 | //所有权值初始化为最大 46 | memset(map,MaxInt,sizeof(map)); 47 | for(i=1;i<=n;i++) 48 | for(j=1;j<=n;j++) 49 | { 50 | scanf("%d",&v); 51 | map[i][j]=map[i][j]=v; 52 | } 53 | ans=prim(); 54 | printf("%d\n",ans); 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /future_net/官方新的测试样例.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pantynopants/algorithm/817030ff3af17e863e847dc1a9ee33812c92259e/future_net/官方新的测试样例.rar --------------------------------------------------------------------------------