1150 Travelling Salesman Problem
Posted wsggb123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1150 Travelling Salesman Problem相关的知识,希望对你有一定的参考价值。
实际上挺水。。。
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) "); 78 } 79 else if (f3 == 0) { 80 printf("(TS cycle) "); 81 } 82 else { 83 printf("(TS simple cycle) "); 84 } 85 86 } 87 printf("Shortest Dist(%d) = %d ", index, shortest); 88 system("pause"); 89 };
以上是关于1150 Travelling Salesman Problem的主要内容,如果未能解决你的问题,请参考以下文章
1150 Travelling Salesman Problem
1150 Travelling Salesman Problem (25 分)难度: 难 / 知识点: 图 模拟 未完成
hdu5402:Travelling Salesman Problem
HDU 5402 Travelling Salesman Problem