分析
题目意思:给出图,求给出路径的权值和 和该路径的一些特征
注:这题最大问题在于这些判断说的模糊,啥是simple
TS simple cycle——》1.每个城市访问到 2.路径有限
3.首尾相同 4.没有多余城市
TS cycle——》1.每个城市访问到 2.路径有限
Not a TS cycle——》1.非每个城市访问到 2.路径为na
Shortest Dist(X) = TotalDist——》记录 TS simple cycle和TS cycle下 最小的路径和 和 序号
代码
#include<bits/stdc++.h>
using namespace std;
int vertex, edge, frequency, quantity, min_id, min_length{ INT_MAX };
vector< vector<int>> graph;
void InPut(vector<bool>&);
void Judge(int, vector<bool>);
int main() {
vector<bool>visit;
InPut(visit);
for (int i = 1; i < frequency + 1; i++) {
Judge(i, visit);
}
printf("Shortest Dist(%d) = %d\n", min_id, min_length);
return 0;
}
void InPut(vector<bool>& visit) {
scanf("%d %d", &vertex, &edge);
graph.resize(vertex + 1);
visit.resize(vertex + 1);
fill(visit.begin(), visit.end(), false);
visit[0] = true;
for (int i = 1; i < vertex + 1; i++) {
graph[i].resize(vertex + 1);
fill(graph[i].begin(), graph[i].end(), 0);
}
int x, y, z;
for (int i = 0; i < edge; i++) {
scanf("%d %d %d", &x, &y, &z);
graph[x][y] = z;
graph[y][x] = z;
}
scanf("%d", &frequency);
}
void Judge(int id, vector<bool>visit) {
scanf("%d", &quantity);
int a, b, dist{ 0 }, first;
bool flag_dist{ true };
scanf("%d", &a);
first = a;
for (int i = 0; i < quantity - 1; i++) {
scanf("%d", &b);
visit[b] = true;
if (flag_dist && graph[a][b] != 0)
dist += graph[a][b];
else
flag_dist = false;
a = b;
}
printf("Path %d:", id);
flag_dist ? printf(" %d ", dist) : printf(" NA ");
bool simple_flag{ true };
for (auto i : visit) {
if (!i) {
simple_flag = false;
break;
}
}
if (simple_flag && flag_dist) {
if (first == b && quantity == vertex + 1)
printf("(TS simple cycle)\n");
else
printf("(TS cycle)\n");
if (dist < min_length) {
min_length = dist;
min_id = id;
}
}
else
printf("(Not a TS cycle)\n");
}