PAT Level A 1150 Traveling Salesman Problem

Link to original title

The "Traveling Salesman Problem" is the question: "Given a list of cities and the distance between each pair of cities, what is the shortest route to visit each city and return to the original city?"

This is an NP-hard problem in combinatorial optimization and is of great importance in operations research and theoretical computer science.

In this problem, you are asked to find the path closest to the solution of the traveling salesman problem from a given list of paths.

Input Format
The first line contains two integers N and M, which represent the number of cities and the number of edges in the undirected graph, respectively.

Next M lines, each line describes an edge in the format of City1 City2 Dist, where the city numbers are from 1 to N, and Dist is positive and does not exceed 100.

The next line contains an integer K representing the number of paths given.

The next K lines describe the path in the format:

n C1 C2 … Cn
n represents the number of cities passed by a given route, and Ci is the number of cities passed by the route.

Output format
For each path, output Path X: TotalDist (Description) on one line.

where X is the route number (starting at 1), TotalDist is the total distance of the route (or NA if the distance does not exist), and Description is one of the following:

TS simple cycle, if it's a simple cycle visiting every city.
TS cycle, if this is a cycle that visits every city, but not a simple cycle.
Not a TS cycle, if this is not a cycle that visited every city.
The last line, output Shortest Dist(X) = TotalDist, where X is the circuit number closest to the solution of the traveling salesman problem, and TotalDist is its total distance.

Guaranteed to have a unique solution.

Data range
2<N≤200,
N−1≤M≤N(N−1)2,
1≤K≤1000,
1≤n≤300
Input example:
6 10
6 2 1
3 4 1
1 5 1
2 5 1
3 1 8
4 1 6
1 6
1 6 3 1 1 2
1 4 5 1 7 7 5 1 4 3 6 2 5 7 6 1 3 4 5 2 6 6 5 1 4 3 6 2 9 6 2 1 6 3 4 5 2 6 4 1 2 5 1 7 6 1 2 5 4 3 1 7 6 3 2 5 4 1 6Example output: Path 1: 11 (TS simple cycle) Path 2: 13 (TS simple cycle) Path 3: 10 (Not a TS cycle) Path 4: 8 (TS cycle) Path 5: 3 (Not a TS cycle) Path 6: 13 (Not a TS cycle) Path 7: NA (Not a TS cycle)

















Shortest Dist(4) = 8

My solution:

#include <bits/stdc++.h>
using namespace std;
const int N = 210, INF = 0x3f3f3f3f;
int n, m, k;
int d[N][N], vi[310];
bool st[N];
int main(){
    int min_dist = INF;
    int min_id;
    cin >> n >> m;
    memset(d, 0x3f, sizeof d);
    for(int i = 0; i < m; i ++ ){
        int a, b, c;
        cin >> a >> b >> c;
        d[a][b] = d[b][a] = c; 
    }
    cin >> k;
    for(int T = 1; T <= k; T ++ ){
        int sum = 0;
        bool success = true;
        int v;
        cin >> v;
        memset(st, 0, sizeof st);
        for(int i = 1; i <= v; i ++ ) cin >> vi[i];
        for(int i = 1; i + 1 <= v; i ++ ){
            int a = vi[i], b = vi[i + 1];
            if(d[a][b] == INF){
                sum = -1;
                success = false;
                break;
            }
            else{
                st[a] = true;
                sum += d[a][b];
            }
        }
        for(int i = 1; i <= n; i ++ ){
            if(!st[i]){
                success = false;
                break;
            }
        }
        if (vi[1] != vi[v]) success = false;
        
        if(sum == -1){
            printf("Path %d: NA (Not a TS cycle)\n", T);
        } 
        else{
            if(!success){
                printf("Path %d: %d (Not a TS cycle)\n", T, sum);
            }
            else{
                if(v == n + 1) printf("Path %d: %d (TS simple cycle)\n", T, sum);
                else printf("Path %d: %d (TS cycle)\n", T, sum);
                if(sum < min_dist){
                    min_dist = sum;
                    min_id = T;
                }
            }
        }
    }
    printf("Shortest Dist(%d) = %d\n", min_id, min_dist);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/126046421