1150 Travelling Salesman Problem(旅行商问题 ,实际没一点关系)
大致题意很直白,就是给出一个无向图,然后给出K条路径,按题目要求判断每条路径,并输出相应的结果。
所以这是一道简单模拟题。
1 #include<iostream> 2 #include<unordered_set> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 220, inf = 0x3fffffff; 7 int main() { 8 int n,m,G[maxn][maxn],u,v,k,num,index,minTotalDist = inf;//邻接矩阵 9 fill(G[0],G[0]+maxn*maxn,inf); 10 cin>>n>>m; 11 for(int i = 0; i < m; ++i) { 12 cin>>u>>v; 13 cin>>G[u][v]; 14 G[v][u] = G[u][v]; 15 } 16 cin>>k; 17 for(int i = 1; i <= k; ++i) { 18 cin>>num; 19 vector<int> path(num); 20 unordered_set<int> st;//统计不同顶点个数 21 for(int j = 0; j < num; ++j) { 22 scanf("%d",&path[j]); 23 st.insert(path[j]); 24 } 25 int TotalDist = 0; 26 for(int j = 0; j < num-1; ++j) { 27 u = path[j],v = path[j+1]; 28 if(G[u][v] != inf) TotalDist += G[u][v]; 29 else { 30 TotalDist = inf; 31 break; 32 } 33 } 34 printf("Path %d:",i); 35 if(TotalDist == inf) printf(" NA (Not a TS cycle)\n"); 36 else { 37 printf(" %d",TotalDist); 38 if(path[0] == path[num-1] && st.size() == n) { 39 if(TotalDist < minTotalDist) { 40 index = i; 41 minTotalDist = TotalDist; 42 } 43 if(num-1 == n) printf(" (TS simple cycle)\n"); 44 else printf(" (TS cycle)\n"); 45 } else printf(" (Not a TS cycle)\n"); 46 } 47 } 48 printf("Shortest Dist(%d) = %d\n",index,minTotalDist); 49 return 0; 50 }