Comments (3)
import java.io.;
import java.util.;
public class TSE {
// there are four nodes in example graph (graph is
// 1-based)
static int n = 4;
// give appropriate maximum to avoid overflow
static int MAX = 1000000;
// dist[i][j] represents shortest distance to go from i
// to j this matrix can be calculated for any given
// graph using all-pair shortest path algorithms
static int[][] dist = {
{ 0, 0, 0, 0, 0 }, { 0, 0, 10, 15, 20 },
{ 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 },
{ 0, 20, 25, 30, 0 },
};
// memoization for top down recursion
static int[][] memo = new int[n + 1][1 << (n + 1)];
static int fun(int i, int mask)
{
if (mask == ((1 << i) | 3))
return dist[1][i];
if (memo[i][mask] != 0)
return memo[i][mask];
int res = MAX;
for (int j = 1; j <= n; j++)
if ((mask & (1 << j)) != 0 && j != i && j != 1)
res = Math.min(res,
fun(j, mask & (~(1 << i)))
+ dist[j][i]);
return memo[i][mask] = res;
}
// Driver program to test above logic
public static void main(String[] args)
{
int ans = MAX;
for (int i = 1; i <= n; i++)
// try to go from node 1 visiting all nodes in
// between to i then return from i taking the
// shortest route to 1
ans = Math.min(ans, fun(i, (1 << (n + 1)) - 1)
+ dist[i][1]);
System.out.println(
"The cost of most efficient tour = " + ans);
}
}
from java.
import java.io.*;
import java.util.*;
public class TSE {
// there are four nodes in example graph (graph is
// 1-based)
static int n = 4;
// give appropriate maximum to avoid overflow
static int MAX = 1000000;
// dist[i][j] represents shortest distance to go from i
// to j this matrix can be calculated for any given
// graph using all-pair shortest path algorithms
static int[][] dist = {
{ 0, 0, 0, 0, 0 }, { 0, 0, 10, 15, 20 }, { 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 }, { 0, 20, 25, 30, 0 },
};
// memoization for top down recursion
static int[][] memo = new int[n + 1][1 << (n + 1)];
static int fun(int i, int mask)
{
if (mask == ((1 << i) | 3)) return dist[1][i]; if (memo[i][mask] != 0) return memo[i][mask]; int res = MAX; for (int j = 1; j <= n; j++) if ((mask & (1 << j)) != 0 && j != i && j != 1) res = Math.min(res, fun(j, mask & (~(1 << i))) + dist[j][i]); return memo[i][mask] = res;
}
// Driver program to test above logic
public static void main(String[] args)
{
int ans = MAX; for (int i = 1; i <= n; i++) // try to go from node 1 visiting all nodes in // between to i then return from i taking the // shortest route to 1 ans = Math.min(ans, fun(i, (1 << (n + 1)) - 1) + dist[i][1]); System.out.println( "The cost of most efficient tour = " + ans);
}
}
In the fun function, the base case condition mask == ((1 << i) | 3) is incorrect. It should be mask == (1 << (n + 1)) - 1
from java.
Related Issues (20)
- Update stale issue message HOT 1
- [FEATURE REQUEST] <Check Consonants> HOT 1
- [FEATURE REQUEST] <Vowels Count> HOT 1
- [FEATURE REQUEST] <Implementation of SRTF Scheduling Algorithm> HOT 1
- [BUG] Missing Package Declaration in Test File Causing Build Failure HOT 1
- [FEATURE REQUEST] Add Highest Response Ratio Next (HRRN) scheduling algorithm HOT 2
- [OTHER] Java Implementation of Playfair Cipher HOT 4
- Better descriptive comments could be added. HOT 5
- Implementetion of rand method HOT 2
- [FEATURE REQUEST] 2x2 Matrix Eigenvalues and Eigenvectors HOT 2
- List HOT 1
- "Temperature Conversion" could be further added in the list of conversion algorithms. HOT 1
- [BUG] There is a issue with clang file for latest clang versions HOT 2
- Complicated Solution of Anagrams could be simplified!
- [BUG] Resource leak: `sc` is never closed. `Java(536871799)` HOT 2
- [OTHER] Enable more checks in checkstyle HOT 22
- [FEATURE REQUEST] <Implementation of binary to gray algorithm>
- [FEATURE REQUEST] Rabin Karp String Algorithm HOT 3
- [FEATURE REQUEST] Sleep Sort Algorithm HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java.