pat1150 Travelling Salesman Problem

题意:输入一个图,输入q条路径,判断每条路是否是简单TS环,TS环,还是不是TS环。

思路:遍历ifelse就好。

代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int n, m, x, y, c, q, k;
map, 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 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;
}

 

你可能感兴趣的:(pat)