The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?" It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical computer science. (Quoted from "https://en.wikipedia.org/wiki/Travelling_salesman_problem".)
In this problem, you are supposed to find, from a given list of cycles, the one that is the closest to the solution of a travelling salesman problem.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<N≤200), the number of cities, and M, the number of edges in an undirected graph. Then M lines follow, each describes an edge in the format City1 City2 Dist
, where the cities are numbered from 1 to N and the distance Dist
is positive and is no more than 100. The next line gives a positive integer K which is the number of paths, followed by K lines of paths, each in the format:
n C1 C2 ... Cn
where n is the number of cities in the list, and Ci's are the cities on a path.
Output Specification:
For each path, print in a line Path X: TotalDist (Description)
where X
is the index (starting from 1) of that path, TotalDist
its total distance (if this distance does not exist, output NA
instead), and Description
is one of the following:
TS simple cycle
if it is a simple cycle that visits every city;TS cycle
if it is a cycle that visits every city, but not a simple cycle;Not a TS cycle
if it is NOT a cycle that visits every city.
Finally print in a line Shortest Dist(X) = TotalDist
where X
is the index of the cycle that is the closest to the solution of a travelling salesman problem, and TotalDist
is its total distance. It is guaranteed that such a solution is unique.
Sample Input:
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 6
Sample 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
题目大意
给定N个点和M条边,之后给定K条路径,对于每一条路径,检查它的类型。最后输出符合条件的最短路途的编号和长度。
分析
使用dis数组保存两个点之间的距离,如果两点之间不连通,则表示为-1.
使用record数组保存距离最小的路径长度和名称。
循环遍历待检测路径。使用tra()函数进行检查。下面是各种类型路径的判断条件:
1>Not a TS cycle
没有经过所有的城市
没有从起点出发回到起点
路径中包含不连通的城市
2>TS cycle
符合TS simple cycle的条件,但是中间重复经过了若干个城市
3>TS simple cycle
剩余的所有情况
通过tra()函数返回的内容,进行输出,并更新record数组中的数据,最后输出符合条件的最短路途编号和长度。
python实现
def tra(num, n, get, dis):
types = "TS simple cycle"
if num < n + 1 or get[0] != get[-1]:
types = "Not a TS cycle"
visted = [0 for x in range(n)]
total = 0
for x in range(1, num):
if dis[get[x]][get[x-1]] == -1:
types = "Not a TS cycle"
return "NA", types
else:
if visted[get[x]-1] == 1:
types = "TS cycle"
total += dis[get[x]][get[x-1]]
visted[get[x]-1] = 1
if 0 in visted:
types = "Not a TS cycle"
return total, types
def main():
line = input().split(" ")
n, m = int(line[0]), int(line[1])
dis = [[ -1 for x in range(n)] for x in range(n)]
for x in range(m):
line = input().split(" ")
start, to, length = int(line[0])-1, int(line[1])-1, int(line[2])
dis[start][to] = length
dis[to][start] = length
k = int(input())
record = [float("inf"), 0]
for x in range(k):
line = input().split(" ")
num = int(line[0])
get = [ int(x)-1 for x in line[1:]]
result = tra(num, n, get, dis)
print('Path {}: {} ({})'.format(x+1, result[0], result[1]))
if result[1] != "Not a TS cycle":
if result[0] < record[0]:
record = [result[0],x+1]
print('Shortest Dist({}) = {}'.format(record[1],record[0]))
if __name__ == "__main__":
main()