The travelling salesman problem (TSP) 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 actually an NP-complete problem (there's NO polynomial algrithms to solve this problem), It is important in operations research and theoretical computer science and also has many kinds of applications such as planning, logistics, and the manufacture of microchips, etc...
Check this wikipedia link for more details.
pthreads
.
You are a travelling salesman, given a map and start point, and you need to find the shortest route which starts at the given point, goes over all these cities only once (except the start city) and finally goes back.
You should implement a travelling salesman problem(TSP) solver in C.
t
,n
and s
t
means the number of threads (main thread is not included), 0< t <=20
n
stands for the total number of citiess
stands for the start point, 0 <= s < n
n
will be guaranteed to<= 10
in Autolab testcases.n
lines represent a symmetric adjacency matrix A
(because the edges are undirected
). A[i][j]
represents the distance
between the cities i
and j
, for all valid i, j
, 1000>=A[i][j]>=0
and they are all integers
.
0
means their is no road between city i,j
For example:
2 4 0
0 100 100 100
100 0 35 25
100 35 0 30
100 25 30 0
200\n
stdio.h stdlib.h string.h pthread.h
t
number of threads and all of them should do reasonable
things (Otherwilse we will deduct 50% of your points). precise
value, so Randomized Algorithms
and k-Approximation Algorithms
will be very likely to fail the test cases. tsp.c
and tsp.h
, which will be created by yourself. We will not offer a code template this time. CC=gcc
CFLAGS=-Wpedantic -Wall -Wextra -Werror -std=c89 -pthread
tsp.o: tsp.c tsp.h
${CC} ${CFLAGS} tsp.c -o tsp.o
ulimit -v 40960
in Terminal and then run ./tsp.o < input_file_name
tsp.c tsp.h
, it should produce a tsp.o file using our Makefile and can be tested using this command
./tsp.o < input_file_name
small <= 10
testcases, to verify the correctness of your program and it will contribute 60%
of the whole points.DOES NOT
guarentee your correctness in the larger testcases on the bigger server, if you have something wrong with our code and use some randomized Algorithm or approximation algrithms.40%
of points: if you fail on the bigger testcases (like find a sub-optimal sulution), you will got 0
points, for programs which output the right answer, the fastest 20 people will get full points, the top 21~50 will get 80%, top 51~80 get 60%, others get 40%(n-1)!
permutations of routes) and optimize it using pthread might be a safe and reasonable choice.Last Modified: Apr. 11th, 2018