实际上挺水。。。
1 #include <iostream> 2 #include <stdlib.h> 3 #include <string> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<map> 8 #include<set> 9 #include <unordered_map> 10 using namespace std; 11 12 int main(){ 13 int v, e; 14 cin >> v >> e; 15 int m1[201][201]; 16 fill(m1[0], m1[0]+ 201 * 201, 0); 17 18 for (int i = 0; i < e; i++) { 19 int v1, v2, d; 20 cin >> v1 >> v2 >> d; 21 m1[v1][v2] = d; 22 m1[v2][v1] = d; 23 } 24 int num; 25 cin >> num; 26 int index = -1; 27 int shortest = 9999999; 28 29 for (int i = 0; i < num; i++) { 30 int k; 31 cin >> k; 32 vector<int> route; 33 for (int j = 0; j < k; j++) { 34 int node; 35 cin >> node; 36 route.push_back(node); 37 } 38 int total = 0; 39 int vis[201]; 40 fill(vis, vis + 201, 0); 41 bool f1=1, f2=1,f3=1; 42 for (int j = 0; j < k-1; j++) { 43 if (m1[route[j]][route[j + 1]] == 0) { 44 f1 = 0; 45 } 46 else { 47 total += m1[route[j]][route[j + 1]]; 48 vis[route[j + 1]]++; 49 } 50 } 51 52 for (int a = 1; a <= v; a++) { 53 if (vis[a] == 0) { 54 f2 = 0; 55 } 56 else if (vis[a] > 1) { 57 f3 = 0; 58 } 59 } 60 61 printf("Path %d: ", i + 1); 62 63 if (f1 != 0 && f2!=0) { 64 if (shortest > total) { 65 shortest = total; 66 index = i + 1; 67 } 68 } 69 if (f1 == 0) { 70 printf("NA "); 71 } 72 else { 73 printf("%d ", total); 74 } 75 76 if (f2 == 0) { 77 printf("(Not a TS cycle)\n"); 78 } 79 else if (f3 == 0) { 80 printf("(TS cycle)\n"); 81 } 82 else { 83 printf("(TS simple cycle)\n"); 84 } 85 86 } 87 printf("Shortest Dist(%d) = %d\n", index, shortest); 88 system("pause"); 89 };
原文地址:https://www.cnblogs.com/wsggb123/p/10226316.html
时间: 2024-11-09 02:23:17