1003 Emergency (25分) && L2-001 紧急救援 (25分) C++ 邻接表实现
Posted on 2020-09-10 17:28 AlexMaxes 阅读(138) 评论(0) 收藏 举报两个题目是一样的 中文的多了一点点小需求
L2-001 紧急救援 (25分)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
输入格式:
输入第一行给出4个正整数N、M、S、D,其中N(2)是城市的个数,顺便假设城市的编号为0 ~ (;M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。
第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。
输出格式:
第一行输出最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出结尾不能有多余空格。
输入样例:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2
输出样例:
2 60
0 1 3
MYCODE:
Dijstra算法,为了实现多条最短路径进行了一点修改
1 #include <iostream> 2 #include <vector> 3 #include <limits.h> //INT_MAX 4 using namespace std; 5 6 #define MaxSize 500 7 8 typedef struct Road 9 { 10 int distance; 11 int City; 12 Road *nextRoad; 13 /* data */ 14 } Road, *ptrRoad; 15 16 typedef struct 17 { 18 int man; 19 ptrRoad firstRoad; 20 } City, *ptrCity; 21 22 typedef struct 23 { 24 int cityNum; 25 int roadNum; 26 int myCity; 27 int aimCity; 28 City city[MaxSize]; 29 /* data */ 30 } Country, *ptrCountry; 31 32 void getCountry(ptrCountry s); 33 void goFind(ptrCountry C); 34 void getAllPath(int start, vector<vector<int>> &path, vector<vector<int>> &allpath, vector<int> temperpath); 35 36 int main() 37 { 38 //freopen("data.txt", "r", stdin); 39 Country C; 40 getCountry(&C); 41 goFind(&C); 42 return 0; 43 } 44 void getCountry(ptrCountry s) 45 { 46 int a, b, c, d; 47 cin >> a >> b >> c >> d; 48 s->cityNum = a; 49 s->roadNum = b; 50 s->myCity = c; 51 s->aimCity = d; 52 for (int i = 0; i < a; i++) 53 { 54 int t; 55 cin >> t; 56 s->city[i].man = t; 57 s->city[i].firstRoad = NULL; 58 } 59 for (int i = 1; i <= b; i++) 60 { 61 int j, k, l; 62 cin >> j >> k >> l; 63 ptrRoad road = (ptrRoad)malloc(sizeof(Road)); 64 road->City = k; 65 road->distance = l; 66 road->nextRoad = s->city[j].firstRoad; 67 s->city[j].firstRoad = road; 68 ptrRoad road2 = (ptrRoad)malloc(sizeof(Road)); 69 road2->City = j; 70 road2->distance = l; 71 road2->nextRoad = s->city[k].firstRoad; 72 s->city[k].firstRoad = road2; 73 } 74 } 75 76 void goFind( ptrCountry C) 77 { 78 /*Dijstra改良算法*/ 79 int dist[C->cityNum], isVisted[C->cityNum]; 80 vector<vector<int>> path(C->cityNum); 81 for (int i = 0; i < C->cityNum; i++) 82 { 83 dist[i] = INT_MAX; 84 isVisted[i] = 0; 85 } 86 ptrRoad Troad = C->city[C->myCity].firstRoad; 87 dist[C->myCity] = 0; 88 path[C->myCity].push_back(-1); 89 isVisted[C->myCity] = 1; 90 while (Troad) 91 { 92 dist[Troad->City] = Troad->distance; 93 path[Troad->City].push_back(C->myCity); 94 Troad = Troad->nextRoad; 95 } 96 while (1) 97 { 98 int V = -1, min = INT_MAX - 1; 99 for (int i = 0; i < C->cityNum; i++) 100 if ((dist[i] < min) && (!isVisted[i])) 101 { 102 min = dist[i]; 103 V = i; 104 } 105 if (V == -1) 106 break; 107 isVisted[V] = 1; 108 ptrRoad Proad = C->city[V].firstRoad; 109 while (Proad) 110 { 111 if (!isVisted[Proad->City]) 112 { 113 if (dist[V] + Proad->distance < dist[Proad->City]) 114 { 115 dist[Proad->City] = dist[V] + Proad->distance; 116 path[Proad->City].clear(); 117 path[Proad->City].push_back(V); 118 } 119 else if (dist[V] + Proad->distance == dist[Proad->City]) 120 path[Proad->City].push_back(V); 121 } 122 Proad = Proad->nextRoad; 123 } 124 } 125 vector<vector<int>> allpath; 126 vector<int> temperpath; 127 getAllPath(C->aimCity,path, allpath,temperpath); 128 int maxMan = 0; 129 auto maxPath = allpath[0]; 130 for (auto i : allpath) 131 { 132 int sum = 0; 133 for (auto j : i) 134 sum += C->city[j].man; 135 if (sum > maxMan) 136 { 137 maxMan = sum; 138 maxPath.swap(i); 139 } 140 } 141 142 cout << allpath.size() << " " << maxMan << endl; 143 144 for (auto i = maxPath.rbegin(); i != maxPath.rend(); i++) 145 { 146 cout << *i; 147 if((*i)!=maxPath[0]) 148 cout << " "; 149 } 150 return; 151 } 152 153 void getAllPath(int start, vector<vector<int>> &path, vector<vector<int>> &allpath, vector<int> temperpath) 154 { 155 temperpath.push_back(start); 156 if (path[start][0] == -1) 157 { 158 allpath.push_back(temperpath); 159 return; 160 } 161 for (int i = 0; i < (int)path[start].size(); i++) 162 getAllPath(path[start][i], path, allpath, temperpath); 163 }
浙公网安备 33010602011771号