diff options
| author | Paul Jungeblut <s_jungeb@i08pc79.atis-stud.uni-karlsruhe.de> | 2014-11-26 11:46:02 +0100 |
|---|---|---|
| committer | Paul Jungeblut <s_jungeb@i08pc79.atis-stud.uni-karlsruhe.de> | 2014-11-26 11:46:02 +0100 |
| commit | 7a217262958cdf11dfb1f90b86df31acde90ff45 (patch) | |
| tree | 8d96850058853910ebf2ef75827b0862180fd0c1 /graph/bitonicTSP.cpp | |
| parent | 2c133b76f193478bb85d41a76ae78695cc067452 (diff) | |
| parent | 24507ee02a8fe20fb22ae30642e3b15e411e82eb (diff) | |
merge
Diffstat (limited to 'graph/bitonicTSP.cpp')
| -rw-r--r-- | graph/bitonicTSP.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/graph/bitonicTSP.cpp b/graph/bitonicTSP.cpp new file mode 100644 index 0000000..4e4dda0 --- /dev/null +++ b/graph/bitonicTSP.cpp @@ -0,0 +1,13 @@ +vector< vector<double> > dp; //initialize with -1 +vector< vector<double> > dist; //initialize with all dists between points +vector<int> lr, rl; //left-to-right and right-to-left paths +int n; //number of points +double get(int p1, int p2) { //call get(0, 0) to get length of shortest bitonic route + int v = max(p1, p2) + 1; + if (v == n - 1) return dist[p1][v] + dist[v][p2]; + if (dp[p1][p2] > -0.5) return dp[p1][p2]; + double tryLR = dist[p1][v] + get(v, p2), tryRl = dist[v][p2] + get(p1, v); + if (tryLR < tryRL) lr.push_back(v); //reconstructs the path, pushes v to rl if the choice does not matter + else rl.push_back(v); //change this if needed + return min(tryLR, tryRL); +}
\ No newline at end of file |
