From b4edf2b786a6257f940185838485f05572c2a327 Mon Sep 17 00:00:00 2001 From: Liwenfei <3496841962@qq.com> Date: Wed, 5 Jul 2023 12:05:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81111?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 代码 | 421 -------------------------------------------------- 代码111.cpp | 351 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 351 insertions(+), 421 deletions(-) delete mode 100644 代码 create mode 100644 代码111.cpp diff --git a/代码 b/代码 deleted file mode 100644 index 89f7fad..0000000 --- a/代码 +++ /dev/null @@ -1,421 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace std; - -const int N = 240; //地铁站点数目 -const int INF = 0x3f3f3f3f; //表示正无穷 - -//地铁线路的颜色 -string line_color[22] = {"", "1号线", "2号线", "4号线", - "5号线", "6号线", "7号线", - "8号线", "9号线", "10号线", - "13号线", "14号线东", - "14号线西", "15号线", - "16号线", "八通线", "昌平线", - "亦庄线", "房山线", "机场线", - "西郊线", "S2线"}; - -//地铁站点信息 -struct Station { - string name; //站点名称 - int line; //所在地铁线路 - int transfer; //是否为换乘站(0不是,否则表示该站可以换乘) - bool operator<(const Station& other) const { //定义小于号,方便使用stl的heap/priority_queue - return line < other.line; - } -} station_list[N + 1]; - -//地铁站点间的距离 -int dist[N + 1][N + 1]; - -//向地铁站点列表中添加站点 -void add_station(int id, string name, int line, int transfer) { - station_list[id] = {name, line, transfer}; -} - -//初始化北京地铁的站点信息和站点间距离 -void init_subway_map() { - //初始化站点信息 - add_station(1, "苹果园", 1, 1); -add_station(2, "古城", 1, 0); - add_station(3, "八角游乐园", 1, 0); - add_station(4, "八宝山", 1, 0); - add_station(5, "玉泉路", 1, 0); - add_station(6, "五棵松", 1, 1); - add_station(7, "万寿路", 1, 0); - add_station(8, "公主坟", 1, 1); - add_station(9, "军事博物馆", 1, 0); - add_station(10, "木樨地", 1, 0); - add_station(11, "南礼士路", 1, 0); - add_station(12, "复兴门", 1, 1); - add_station(13, "西单", 1, 1); - add_station(14, "天安门西", 1, 0); - add_station(15, "天安门东", 1, 0); - add_station(16, "王府井", 1, 1); - add_station(17, "东单", 1, 1); - add_station(18, "建国门", 1, 1); - add_station(19, "永安里", 1, 0); - add_station(20, "国贸", 1, 1); - add_station(21, "大望路", 1, 0); - add_station(22, "四惠", 1, 1); - add_station(23, "四惠东", 1, 0); - add_station(24, "西直门", 2, 1); - add_station(25, "车公庄", 2, 1); - add_station(26, "阜成门", 2, 0); - add_station(27, "复兴门", 2, 1); - add_station(28, "长椿街", 2, 0); - add_station(29, "宣武门", 2, 1); - add_station(30, "和平门", 2, 0); - add_station(31, "前门", 2, 0); - add_station(32, "崇文门", 2, 1); - add_station(33, "北京站", 2, 1); - add_station(34, "建国门", 2, 1); - add_station(35, "朝阳门", 2, 1); - add_station(36, "东四十条", 2, 0); - add_station(37, "东直门", 2, 1); - add_station(38, "雍和宫", 2, 1); - add_station(39, "安定门", 2, 0); - add_station(40, "鼓楼大街", 2, 1); - add_station(41, "积水潭", 2, 0); - add_station(42, "西直门", 4, 1); - add_station(43, "新街口", 4, 0); - add_station(44, "平安里", 4, 1); - add_station(45, "西四", 4, 0); - add_station(46, "灵境胡同", 4, 0); - add_station(47, "西单", 4, 1); - add_station(48, "宣武门", 4, 1); - add_station(49, "菜市口", 4, 1); - add_station(50, "陶然亭", 4, 0); - add_station(51, "北京南站", 4, 1); - add_station(52, "马家堡", 4, 0); - add_station(53, "角门西", 4, 0); - add_station(54, "公益西桥", 4, 0); - add_station(55, "新宫", 4, 0); - add_station(56, "西红门", 4, 0); - add_station(57, "高米店北", 4, 0); - add_station(58, "高米店南", 4, 0); - add_station(59, "枣园", 4, 0); - add_station(60, "清源路", 4, 0); - add_station(61, "黄村西大街", 4, 0); - add_station(62, "黄村火车站", 4, 0); - add_station(63, "义和庄", 4, 0); - add_station(64, "生物医药基地", 4, 0); - add_station(65, "天宫院", 4, 0); - add_station(66, "宋家庄", 5, 1); - add_station(67, "刘家窑", 5, 0); - add_station(68, "蒲黄榆", 5, 1); - add_station(69, "天坛东门", 5, 0); - add_station(70, "磁器口", 5, 1); - add_station(71, "崇文门", 5, 1); - add_station(72, "东单", 5, 1); - add_station(73, "灯市口", 5, 0); - add_station(74, "东四", 5, 0); - add_station(75, "张自忠路", 5, 0); - add_station(76, "北新桥", 5, 0); - add_station(77, "雍和宫", 5, 1); - add_station(78, "和平里北街", 5, 0); - add_station(79, "和平西桥", 5, 0); - add_station(80, "惠新西街南口", 5, 0); - add_station(81, "惠新西街北口", 5, 0); - add_station(82, "大屯路东", 5, 1); - add_station(83, "北苑路北", 5, 0); - add_station(84, "立水桥南", 5, 0); - add_station(85, "立水桥", 5, 1); - add_station(86, "天通苑南", 5, 0); - add_station(87, "天通苑", 5, 0); - add_station(88, "天通苑北", 5, 0); - add_station(89, "昌平西山口", 5, 0); - add_station(90, "十三陵景区", 5, 0); - add_station(91, "昌平", 5, 0); - add_station(92, "昌平东关", 5, 0); - add_station(93, "南邵", 8, 0); - add_station(94, "沙河高教园", 8, 0); - add_station(95, "沙河", 8, 0); - add_station(96, "巩华城", 8, 0); - add_station(97, "朱辛庄", 8, 0); - add_station(98, "生命科学园", 8, 0); - add_station(99, "西二旗", 13, 1); - add_station(100, "生命科学园", 13, 0); - add_station(101, "永泰庄", 13, 0); - add_station(102, "西北旺", 13, 0); - add_station(103, "马连洼", 13, 0); - add_station(104, "上地", 13, 1); - add_station(105, "五道口", 13, 1); - add_station(106, "知春路", 13, 1); - add_station(107, "知春里", 13, 0); - add_station(108, "海淀黄庄", 4, 1); - add_station(109, "人民大学", 4, 0); - add_station(110, "魏公村", 4, 0); - add_station(111, "国家图书馆", 4, 1); - add_station(112, "动物园", 4, 1); - add_station(113, "新街口", 4, 0); - add_station(114, "平安里", 6, 1); - add_station(115, "北海北", 6, 0); - add_station(116, "南锣鼓巷", 6, 1); - add_station(117, "东四", 6, 1); - add_station(118, "朝阳门", 6, 1); - add_station(119, "东大桥", 6, 0); - add_station(120, "呼家楼", 6, 1); - add_station(121, "金台路", 6, 1); - add_station(122, "十里堡", 6, 0); - add_station(123, "青年路", 6, 0); - add_station(124, "褡裢坡", 6, 0); - add_station(125, "黄渠", 6, 0); - add_station(126, "常营", 6, 0); - add_station(127, "草房", 6, 0); - add_station(128, "物资学院路", 6, 0); - add_station(129, "通州北关", 6, 0); - add_station(130, "北运河西", 6, 0); - add_station(131, "郝家府", 6, 0); - add_station(132, "东夏园", 6, 0); - add_station(133, "潞城", 6, 0); - add_station(134, "顺义", BC, 0); - add_station(135, "俸伯", BC, 0); - add_station(136, "机场北站", BC, 0); - add_station(137, "中关村", 10, 1); - add_station(138, "海淀大街", 10, 1); - add_station(139, "知春路", 10, 1); - add_station(140, "西土城", 10, 0); - add_station(141, "牡丹园", 10, 0); - add_station(142, "健德门", 10, 0); - add_station(143, "北土城", 10, 0); - add_station(144, "安贞门", 10, 1); - add_station(145, "惠新西街南口", 10, 0); - add_station(146, "芍药居", 10, 1); - add_station(147, "太阳宫", 10, 0); - add_station(148, "三元桥", 10, 1); - add_station(149, "亮马桥", 10, 0); - add_station(150, "农业展览馆", 10, 1); - add_station(151, "团结湖", 10, 0); - add_station(152, "呼家楼", 10, 1); - add_station(153, "金台夕照", 10, 0); - add_station(154, "国贸", 10, 1); - add_station(155, "双井", 10, 0); - add_station(156, "劲松", 10, 0); - add_station(157, "潘家园", 10, 0); - add_station(158, "十里河", 10, 1); - add_station(159, "分钟寺", 10, 0); - add_station(160, "成寿寺", 10, 0); - add_station(161, "宋家庄", 10, 1); - add_station(162, "石榴庄", 5, 0); - add_station(163, "大红门", 10, 0); - add_station(164, "角门东", 10, 0); - add_station(165, "角门西", 10, 0); - add_station(166, "草桥", 10, 0); - add_station(167, "纪家庙", FS, 0); - add_station(168, "首经贸", FS, 1); - add_station(169, "丰台站", FS, 0); - add_station(170, "泥洼", FS, 0); - add_station(171, "西局", FS, 1); - add_station(172, "六里桥", FS, 1); - add_station(173, "莲花桥", FS, 0); - add_station(174, "公主坟", FS, 1); - add_station(175, "西钓鱼台", FS, 0); - add_station(176, "慈寿寺", FS, 1); - add_station(177, "车道沟", FS, 0); - add_station(178, "长春桥", FS, 0); - add_station(179, "火器营", 9, 1); - add_station(180, "巴沟", 10, 0); - add_station(181, "苏州街", 10, 0); - add_station(182, "海淀五路居", 10, 0); - add_station(183, "知春路", 10, 1); - add_station(184, "知春里", 10, 0); - add_station(185, "西土城", 10, 0); - add_station(186, "牡丹园", 10, 0); - add_station(187, "健德门", 10, 0); - add_station(188, "北土城", 10, 0); - add_station(189, "安贞门", 10, 0); - - - //初始化站点间距离 - for (int i = 1; i <= N; i++) { - for (int j = 1; j <= N; j++) { - if (i == j) { - dist[i][j] = 0; - } else { - dist[i][j] = INF; - } - } - } - //1号线 - dist[1][2] = dist[2][1] = 4; - dist[2][3] = dist[3][2] = 4; - //2号线 - dist[4][5] = dist[5][4] = 4; - dist[5][6] = dist[6][5] = 4; - //4号线 - dist[7][8] = dist[8][7] = 6; - dist[8][9] = dist[9][8] = 6; - //5号线 - dist[10][11] = dist[11][10] = 4; - dist[11][12] = dist[12][11] = 4; - //6号线 - dist[13][14] = dist[14][13] = 4; - dist[14][15] = dist[15][14] = 4; - //7号线 - dist[16][17] = dist[17][16] = 5; - dist[17][18] = dist[18][17] = 5; - //8号线 - dist[19][20] = dist[20][19] = 4; - dist[20][21] = dist[21][20] = 4; - //9号线 - dist[22][23] = dist[23][22] = 5; - dist[23][24] = dist[24][23] = 5; - //10号线 - dist[25][26] = dist[26][25] = 4; - dist[26][27] = dist[27][26] = 4; - //13号线 - dist[28][29] = dist[29][28] = 6; - dist[29][30] = dist[30][29] = 6; - //14号线东 - dist[31][32] = dist[32][31] = 5; - dist[32][33] = dist[33][32] = 5; - //14号线西 - dist[34][35] = dist[35][34] = 6; - dist[35][36] = dist[36][35] = 6; - //15号线 - dist[37][38] = dist[38][37] = 3; - dist[38][39] = dist[39][38] = 3; - //16号线 - dist[40][41] = dist[41][40] = 3; - //八通线 - dist[42][43] = dist[43][42] = 3; - dist[43][44] = dist[44][43] = 4; - //昌平线 - dist[45][46] = dist[46][45] = 4; - dist[46][47] = dist[47][46] = 4; - //亦庄线 - dist[48][49] = dist[49][48] = 3; - //房山线 - dist[50][51] = dist[51][50] = 3; - dist[51][52] = dist[52][51] = 3; - //机场线 - dist[53][54] = dist[54][53] = 13; - //西郊线(只有一个站点) - dist[55][55] = 10; - //S2线 - dist[56][57] = dist[57][56] = 4; - dist[57][58] = dist[58][57] = 4; -} - -//BFS搜索最短路径,返回路径长度 -int bfs(int start, int end) { - queue q; - vector visited(N + 1, false); //记录每个节点是否被访问过 - vector distance(N + 1); //记录每个节点距离起点的距离 - q.push(start); - visited[start] = true; - while (!q.empty()) { - int cur = q.front(); - q.pop(); - for (int i = 1; i <= N; i++) { - if (dist[cur][i] != INF && !visited[i]) { - visited[i] = true; - distance[i] = distance[cur] + 1; - q.push(i); - } - } - } - return distance[end]; -} - -//输出从start到end的最短路径 -void print_route(int start, int end) { - priority_queue, vector>, greater>> q; //小根堆,按照距离升序排列,如果距离相同,按照站点名称升序排列 - map added; //记录已经添加到队列中的节点,防止重复添加 - vector> route; //记录路径上的所有站点 - q.push({0,station_list[start].name}); - while (!q.empty()) { - auto cur = q.top(); - q.pop(); - if (cur.second == station_list[end].name) { //找到了终点,直接退出循环 - break; - } - int cur_id = 0; - for (int i = 1; i <= N; i++) { - if (station_list[i].name == cur.second) { - cur_id = i; - break; - } - } - added[cur_id] = true; - for (int i = 1; i <= N; i++) { - if (dist[cur_id][i] != INF && !added[i]) { //如果两个站点之间有直达的地铁线路,并且该站点没有被添加过 - q.push({cur.first + dist[cur_id][i], station_list[i].name}); //将该节点加入队列 - added[i] = true; - route.push_back({i, station_list[i].name}); //记录该站点 - } - } - } - cout << "从 " << station_list[start].name << " 到 " << station_list[end].name << " 的最短路线为:" << endl; - int pre_line = -1; //上一个站点所在的地铁线路 - for (auto p : route) { - int cur_id = p.first; - int cur_line = station_list[cur_id].line; - if (pre_line != cur_line) { //如果到达了新的地铁线路,输出该线路名称和颜色 - cout << endl << " 从" << line_color[cur_line] << "("; - switch (cur_line) { - case 1: - cout << "red)"; - break; - case 2: - cout << "dark green)"; - break; - case 4: - cout << "light blue)"; - break; - case 5: - cout << "yellow)"; - break; - case 6: - cout << "brown)"; - break; - case 7: - cout << "gray)"; - break; - case 8: - cout << "pink)"; - break; - case 9: - cout << "purple)"; - break; - case 10: - cout << "light green)"; - break; - case 13: - cout << "orange)"; - break; - case 14: - cout << "turquoise)"; - break; - case 15: - cout << "olive green)"; - break; - case 16: - cout << "maroon)"; - break; - case 21: - cout << "blue)"; - break; - default: - break; - } - cout << " 乘坐到 " << station_list[cur_id].name << " 站,步行 "; - pre_line = cur_line; - } else { - cout << " -> "; - } - cout << dist[p.first][route.back().first] << " 米"; - } - cout << endl << "共 " << bfs(start, end) << " 站,需要步行 " << route.back().first - route.front().first << " 米" << endl; -} - -int main() { - init_subway_map(); - print_route(1, 240); - return 0; -} \ No newline at end of file diff --git a/代码111.cpp b/代码111.cpp new file mode 100644 index 0000000..4551aff --- /dev/null +++ b/代码111.cpp @@ -0,0 +1,351 @@ +#include +#include +#include +#include +#include +using namespace std; + +#define ALLNameNum 99 +#define INF 99999 + +typedef int dist[ALLNameNum]; +typedef int path[ALLNameNum]; + +typedef struct +{ + char name[100]; + char intro[999]; + int x, y; +}SITE; + +typedef struct +{ + SITE site[ALLNameNum]; + int length[ALLNameNum][ALLNameNum]; +}MAP; + +MAP M; +int N = 0; +path p; +dist d; + +void init() +{ + int i, j; + strcpy(M.site[1].name, "ƻ԰"); + strcpy(M.site[1].intro, "ʯɽλƻ԰·"); + strcpy(M.site[2].name, "ų"); + strcpy(M.site[2].intro, "λڱʯɽʯɽ·ųСֽᶫ"); + strcpy(M.site[3].name, "˽԰"); + strcpy(M.site[3].intro, "ʯɽ˽Ŷ"); + strcpy(M.site[4].name, ""); + strcpy(M.site[4].intro, "к··ᴦ·"); + strcpy(M.site[5].name, ""); + strcpy(M.site[5].intro, "λڱ֡ڴ֡ϴ֡űֽ㴦"); + strcpy(M.site[6].name, "찲"); + strcpy(M.site[6].intro, "λйֺϳֽᴦDZӪ޹˾ӪijվҲDZ1ߵмվ"); + strcpy(M.site[7].name, ""); + strcpy(M.site[7].intro, "λйж볯紦·DZӪ޹˾ӪijվҲDZ1ߡ2ߵĻվ"); + strcpy(M.site[8].name, ""); + strcpy(M.site[8].intro, "λйг붫·ڵ·"); + strcpy(M.site[9].name, "¥"); + strcpy(M.site[9].intro, "DZ2뱱8ߵĻվ,λڱ붫紦,-ʤŶɹ¥-ɹ¥ֽᴦ"); + strcpy(M.site[10].name, "Ӻ͹"); + strcpy(M.site[10].intro, "λڱжŶӺ͹֡ƽֽ㴦"); + strcpy(M.site[11].name, ""); + strcpy(M.site[11].intro, "λڱж볯紦,-ڴ볯ϴ-űֽ㴦"); + strcpy(M.site[12].name, "վ"); + strcpy(M.site[12].intro, "λйжڡվ㳡·"); + strcpy(M.site[13].name, ""); + strcpy(M.site[13].intro, "2ߺͱ5ڴ˽㣬λڱжǰŶڴֽ"); + strcpy(M.site[14].name, "ƽ"); + strcpy(M.site[14].intro, "λڱ»֣»ǰ֣Ŷֽ㴦"); + strcpy(M.site[15].name, ""); + strcpy(M.site[15].intro, "λйDZ2ߺͱ4ߵĻվ"); + strcpy(M.site[16].name, ""); + strcpy(M.site[16].intro, "վDZ2ߵһվ,λڱֽ֡㴦"); + strcpy(M.site[17].name, "ֱ"); + strcpy(M.site[17].intro, "λڱֱ·"); + strcpy(M.site[18].name, "Բ԰"); + strcpy(M.site[18].intro, "λڱк廪·"); + strcpy(M.site[19].name, "ѧ"); + strcpy(M.site[19].intro, "λйк"); + strcpy(M.site[20].name, "йش"); + strcpy(M.site[20].intro, "λڱкйشһϲ"); + strcpy(M.site[21].name, "ׯ"); + strcpy(M.site[21].intro, "DZ4ߺͱ10ߵĻվ"); + strcpy(M.site[22].name, "ѧ"); + strcpy(M.site[22].intro, "λڱкͨ"); + strcpy(M.site[23].name, "ͼ"); + strcpy(M.site[23].intro, "ͼվλڹͼݵϲ"); + strcpy(M.site[24].name, "԰"); + strcpy(M.site[24].intro, "λڱֱ"); + strcpy(M.site[25].name, "վ"); + strcpy(M.site[25].intro, "λйз̨·վ·"); + strcpy(M.site[26].name, ""); + strcpy(M.site[26].intro, "λйд"); + strcpy(M.site[27].name, "ҽҩ"); + strcpy(M.site[27].intro, "DZ4ߵһվ,λڱдԴ·ϲ"); + strcpy(M.site[28].name, "̳"); + strcpy(M.site[28].intro, "λйж"); + strcpy(M.site[29].name, "ͨԷ"); + strcpy(M.site[29].intro, "DZ5ߵһվҲDZһͨӵĵվλڱвƽ·̫ƽׯжֽ㴦"); + strcpy(M.site[30].name, "ͨԷ"); + strcpy(M.site[30].intro, "λڱвƽͨԷ̫ƽׯԱ400״"); + + + +for (i = 1; i <= ALLNameNum; i++) + { + for (j = 1; j <= ALLNameNum; j++) + { + M.length[i][j] = INF; + } + } + for (i = 1; i <= ALLNameNum; i++) + M.length[i][i] = 0; + M.length[1][2] = M.length[2][1] = 30; + M.length[1][3] = M.length[3][1] = 30; + M.length[2][3] = M.length[3][2] = 90; + M.length[2][4] = M.length[4][2] = 70; + M.length[3][5] = M.length[5][3] = 50; + M.length[3][6] = M.length[6][3] = 50; + M.length[4][5] = M.length[5][4] = 50; + M.length[4][7] = M.length[7][4] = 100; + M.length[5][6] = M.length[6][5] = 30; + M.length[6][7] = M.length[7][6] = 110; + M.length[6][10] = M.length[10][6] = 20; + M.length[7][8] = M.length[8][7] = 30; + M.length[7][9] = M.length[9][7] = 30; + M.length[7][10] = M.length[10][7] = 30; + M.length[8][9] = M.length[9][8] = 30; + M.length[9][10] = M.length[10][9] = 60; + M.length[9][11] = M.length[11][9] = 40; + M.length[10][11] = M.length[11][10] = 40; + M.length[11][12] = M.length[12][11] = 100; + M.length[11][13] = M.length[13][11] = 50; + M.length[12][13] = M.length[13][12] = 60; + M.length[12][15] = M.length[15][12] = 30; + M.length[12][17] = M.length[17][12] = 170; + M.length[13][14] = M.length[14][13] = 70; + M.length[13][15] = M.length[15][13] = 30; + M.length[13][16] = M.length[16][13] = 50; + M.length[14][16] = M.length[16][14] = 50; + M.length[15][16] = M.length[16][15] = 20; + M.length[16][17] = M.length[17][16] = 30; + M.length[17][18] = M.length[18][17] = 60; + M.length[17][19] = M.length[19][17] = 30; + M.length[17][20] = M.length[20][17] = 170; + M.length[18][20] = M.length[20][18] = 70; + M.length[19][21] = M.length[21][19] = 30; + M.length[20][21] = M.length[21][20] = 50; + M.length[21][22] = M.length[22][21] = 50; + M.length[21][24] = M.length[24][21] = 20; + M.length[22][23] = M.length[23][22] = 30; + M.length[23][25] = M.length[25][23] = 60; + M.length[23][26] = M.length[26][23] = 30; + M.length[24][25] = M.length[25][24] = 170; + M.length[25][27] = M.length[27][25] = 70; + M.length[26][27] = M.length[27][26] = 30; + M.length[27][28] = M.length[28][27] = 50; + M.length[27][29] = M.length[29][27] = 50; + M.length[28][29] = M.length[29][28] = 20; + M.length[29][30] = M.length[30][29] = 30; + N = 30; + +} + +void queryAllSite() +{ + int t; + printf("еص£\n"); + for (t = 1; t <= N; t++) + { + printf("ţ %d\n ص㣺%s\n ܣ%s\n", t, M.site[t].name, M.site[t].intro); + } + printf("\n"); + getch(); +} + +void querySite() +{ + int a; + printf("Ҫѯĵصǣ"); + scanf("%d", &a); + if (a > N) + printf("ѯĵص㲻ڣѯʧܣ"); + else + printf("ţ%d\n ص㣺%s\n ܣ%s\n", a, M.site[a].name, M.site[a].intro); + printf("\nأ"); + getch(); +} + +void dijkstraAllSite() +{ + int v0; + printf("ѯĵص㣺"); + scanf("%d", &v0); + bool flag[ALLNameNum]; + //vʾһڵ㣬kʾڵ + int i, k, j, v, min, x; + for (v = 1; v <= N; v++) + { + flag[v]=0; + d[v] = M.length[v0][v]; + if (d[v] < INF && d[v] != 0) + p[v] = v0; + else + p[v] = -1; + } + flag[v0] = 1; + d[v0] = 0;//ԭ + for (i = 2; i <= N; i++) + { + min = INF; + for (k = 1; k <= N; ++k) + if (!flag[k] && d[k] < min) + {//ûбѯҾССֵ + v = k; + min = d[k]; + } + + if (min == INF) + return; + flag[v] = 1; + for (k = 1; k <= N; ++k) + if (!flag[k] && (min + M.length[v][k] < d[k])) + { + d[k] = min + M.length[v][k];//kľ + p[k] = v;//kڵһڵv + } + } +} +void printAllSite() +{ + int st[ALLNameNum], i, pre, top = -1; + for (i = 1; i <= N; i++) + { + printf("\nص%2dܾΪ: %5d , ·Ϊ:", i, d[i]); + st[++top] = i; + pre = p[i]; + while (pre != -1) + { + st[++top] = pre; + pre = p[pre]; + } + while (top > 0) + { + printf("%d", st[top--]); + if (top > 0) + printf("--->"); + } + } + getch(); +} + +void dijkstraTwoSite() +{ + int v0; + printf("ʼصӦıţ"); + scanf("%d", &v0); + bool flag[ALLNameNum]; + int i, k, j, v, min, x; + for (v = 1; v <= N; v++) + { + flag[v] = 0; + d[v] = M.length[v0][v]; + if (d[v] < INF && d[v] != 0) + p[v] = v0; + else + p[v] = -1; + } + flag[v0] = 1; + d[v0] = 0; + for (i = 2; i <= N; i++) + { + min = INF; + for (k = 1; k <= N; ++k) + if (!flag[k] && d[k] < min) + { + v = k; + min = d[k]; + } + + if (min == INF) + return; + flag[v] = 1; + for (k = 1; k <= N; ++k) + if (!flag[k] && (min + M.length[v][k] < d[k])) + { + d[k] = min + M.length[v][k]; + p[k] = v; + } + } +} + +void printTwoSite() +{ + int y; + printf("\nĿĵصӦıţ"); + scanf("%d", &y); + int st[ALLNameNum], i, pre, top = -1; + for (i = 1; i <= N; i++) + { + if (i == y) + printf("\nܾ: %5d , ·Ϊ:", d[i]); + st[++top] = i; + pre = p[i]; + while (pre != -1) + { + st[++top] = pre; + pre = p[pre]; + } + while (top > 0) + { + if (i == y) + { + printf("%d", st[top--]); + if (top > 0) + printf("--->"); + } + else + top--; + } + } + getch(); +} + +main() +{ + init(); + int x; + while (1) + { + printf("**********************************************************************\n"); + printf("* ӭʹñ·߹滮ϵͳ *\n"); + printf("**********************************************************************\n"); + printf("\n 0˳ "); + printf("\n 1վ㼰"); + printf("\n 2ѯijһվ㼰"); + printf("\n 3ѯijһվ㵽վ·"); + printf("\n 4ѯijվ֮· "); + printf("\n*******************************************************************"); + printf("صŶӦ\n"); + printf(" 1.ƻ԰ 2.ų 3.˽԰ 4. 5. 6.찲 7. 8. 9.¥ 10.Ӻ͹\n"); + printf("11. 12.վ 13. 14.ƽ 15. 16. 17.ֱ 18.Բ԰ 19.ѧ 20.йش 21.ׯ 22.ѧ 23.ͼ 24.԰ 25.վ 26. 27.ҽҩ 28̳ 29.ͨԷ 30.ͨԷ\n "); + printf("\nѡ"); + scanf("%d", &x); + if (x == 0) + break; + else + switch (x) + { + case 1:queryAllSite(); break; + case 2:querySite(); break; + case 3:dijkstraAllSite(); printAllSite(); break; + case 4:dijkstraAllSite(); printTwoSite(); break; + + } + system("cls"); + } +}