Skip to content

Commit

Permalink
UVA10496
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Feb 22, 2019
1 parent 352d84c commit ce64454
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
85 changes: 85 additions & 0 deletions myCpps/collectingBeepers_UVA10496.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <bits/stdc++.h>

using namespace std;

ifstream fin("collectingBeepers_UVA10496.in");
ofstream fout("collectingBeepers_UVA10496.out");

const int inf = INT_MAX / 3;

struct point
{
int x;
int y;
};

point _point(int x, int y)
{
point temp{x, y}; return temp;
}

int _abs(int x)
{
if (x > 0) return x;
return -x;
}

void solve(int u, int visit, vector<vector<int>> & a, vector<vector<int>> & dp)
{
int n = a.size();

if (visit == ((1 << n) - 1))
{
dp[u][visit] = a[u][0];
return;
}
else if (dp[u][visit] < inf)
{
return;
}

int ans = inf;
for (int v = 0; v <= n - 1; ++v)
{
if ((visit & (1 << v)) == 0)
{
int temp = visit | (1 << v);
solve(v, temp, a, dp);

ans = min(ans, a[u][v] + dp[v][temp]);
}
}

dp[u][visit] = ans;
}

int main()
{
int testCase; fin >> testCase;
for (int t = 1; t <= testCase; ++t)
{
int xM, yM, xK, yK; fin >> xM >> yM >> xK >> yK;

int n; fin >> n; ++n;
vector<vector<int>> a(n, vector<int>(n, inf));

vector<point> p(n); p[0] = _point(xK, yK);
for (int u = 1; u <= n - 1; ++u)
{
fin >> p[u].x >> p[u].y;

for (int v = 0; v <= u - 1; ++v)
{
a[u][v] = _abs(p[u].x - p[v].x) + _abs(p[u].y - p[v].y);
a[v][u] = a[u][v];
}
}

vector<vector<int>> dp(n, vector<int>(1 << n, inf));
solve(0, 0, a, dp);

fout << "The shortest path has length " << dp[0][1] << '\n';
}

return 0;
}
11 changes: 11 additions & 0 deletions myCpps/collectingBeepers_UVA10496.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
2
20 20
1 10
3
1 11
1 8
1 15
10 10
1 1
1
1 1
2 changes: 2 additions & 0 deletions myCpps/collectingBeepers_UVA10496.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The shortest path has length 14
The shortest path has length 0
87 changes: 87 additions & 0 deletions myCpps/collectingBeepers_UVA10496OJ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <bits/stdc++.h>

using namespace std;


const int inf = INT_MAX / 3;

struct point
{
int x;
int y;
};

point _point(int x, int y)
{
point temp{x, y}; return temp;
}

int _abs(int x)
{
if (x > 0) return x;
return -x;
}

void solve(int u, int visit, vector<vector<int>> & a, vector<vector<int>> & dp)
{
int n = a.size();

if (visit == ((1 << n) - 1))
{
dp[u][visit] = a[u][0];
return;
}
else if (dp[u][visit] < inf)
{
return;
}

int ans = inf;
for (int v = 0; v <= n - 1; ++v)
{
if ((visit & (1 << v)) == 0)
{
int temp = visit | (1 << v);
solve(v, temp, a, dp);

ans = min(ans, a[u][v] + dp[v][temp]);
}
}

dp[u][visit] = ans;
}

int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int testCase; cin >> testCase;
for (int t = 1; t <= testCase; ++t)
{
int xM, yM, xK, yK; cin >> xM >> yM >> xK >> yK;

int n; cin >> n; ++n;
vector<vector<int>> a(n, vector<int>(n, inf));

vector<point> p(n); p[0] = _point(xK, yK);
for (int u = 1; u <= n - 1; ++u)
{
cin >> p[u].x >> p[u].y;

for (int v = 0; v <= u - 1; ++v)
{
a[u][v] = _abs(p[u].x - p[v].x) + _abs(p[u].y - p[v].y);
a[v][u] = a[u][v];
}
}

vector<vector<int>> dp(n, vector<int>(1 << n, inf));
solve(0, 0, a, dp);

cout << "The shortest path has length " << dp[0][1] << '\n';
}

cout.flush();
return 0;
}

0 comments on commit ce64454

Please sign in to comment.