题意:输入一个图,输入q条路径,判断每条路是否是简单TS环,TS环,还是不是TS环。
思路:遍历ifelse就好。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <climits>
using namespace std;
int n, m, x, y, c, q, k;
map<pair<int, int>, int> mp;
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &x, &y, &c);
mp[make_pair(x, y)] = c;
mp[make_pair(y, x)] = c;
}
scanf("%d", &q);
int p = -1, ans = INT_MAX;
for (int i = 1; i <= q; i++) {
int ty = 0, cnt = 0; set<int> st;
scanf("%d", &k); scanf("%d", &x); int s = x, res = 0; st.insert(x);
for (int j = 1; j < k; j++) {
scanf("%d", &y); cnt++; st.insert(y);
if (mp.count(make_pair(x, y)) && ty != -1) {
res += mp[make_pair(x, y)];
x = y;
} else {
ty = -1; x = y; res = INT_MAX;
}
}
if (ty == -1) {
printf("Path %d: NA (Not a TS cycle)\n", i);
} else if (s != y || st.size() != n) {
printf("Path %d: %d (Not a TS cycle)\n", i, res);
} else if (s == y && cnt != n) {
printf("Path %d: %d (TS cycle)\n", i, res);
if (ans > res) {ans = res; p = i;}
} else if (s == y && cnt == n) {
printf("Path %d: %d (TS simple cycle)\n", i, res);
if (ans > res) {ans = res; p = i;}
}
}
printf("Shortest Dist(%d) = %d\n", p, ans);
return 0;
}
版权声明:本文为csx0987原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。