Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #include <algorithm>
- #include <vector>
- #define bit(i, x) (((x) >> (i)) & 1)
- #define task "taxi"
- using namespace std;
- using ll = long long;
- using ld = long double;
- const int N = 21 + 2;
- int n, m, trace[1 << 21][21];
- int d[N][N], par[N][N], a[N][N];
- bool flag;
- ll dp[1 << 21][21];
- vector<int> res;
- const ll Inf = 1e17;
- void Read()
- {
- cin >> n;
- m = 2 * n + 1;
- for (int i = 0; i < m; ++i)
- for (int j = 0; j < m; ++j)
- cin >> d[i][j],
- a[i][j] = d[i][j];
- }
- void Dijkstra(int x, int a[], int par[])
- {
- struct Tque
- {
- int v;
- int w;
- Tque() {}
- Tque(int v, int w)
- {
- this->v = v;
- this->w = w;
- }
- bool Valid(int a[])
- {
- return w == a[v];
- }
- bool operator<(const Tque &a) const
- {
- return w > a.w;
- }
- };
- priority_queue<Tque> s;
- s.push(Tque(x, 0));
- a[x] = 0;
- while (s.size())
- {
- Tque c = s.top();
- s.pop();
- if (!c.Valid(a))
- continue;
- for (int i = 0; i < m; ++i)
- {
- if (a[i] > a[c.v] + d[c.v][i])
- {
- a[i] = a[c.v] + d[c.v][i];
- par[i] = c.v;
- s.push(Tque(i, a[i]));
- }
- }
- }
- }
- void Floyd()
- {
- fill_n(&a[0][0], N * N, Inf);
- for (int i = 0; i < m; ++i)
- Dijkstra(i, a[i], par[i]);
- }
- void Shortest_path(int i, int j)
- {
- if (i == j)
- return;
- Shortest_path(i, par[i][j]);
- res.push_back(j);
- }
- void Trace(int i, int j)
- {
- if (i == 0)
- return;
- Trace(i ^ (1 << j), trace[i][j]);
- res.push_back(trace[i][j]);
- Shortest_path(trace[i][j], j);
- res.push_back(j);
- }
- void Solve()
- {
- fill_n(&dp[0][0], (1 << 21) * 21, Inf);
- dp[1][0] = 0;
- for (int i = 1; i < (1 << m); ++i)
- {
- flag = 0;
- for (int j = 1; j <= n; ++j)
- if (bit(j + n, i) && (!bit(j, i)))
- {
- flag = 1;
- break;
- }
- if (flag)
- continue;
- for (int j = 0; j < m; ++j)
- if (bit(j, i))
- for (int t = 0; t < m; ++t)
- if (t != j && bit(t, i))
- {
- if (dp[i][j] > dp[i ^ (1 << j)][t] + a[t][j])
- {
- dp[i][j] = dp[i ^ (1 << j)][t] + a[t][j];
- trace[i][j] = t;
- }
- }
- }
- ll ans = Inf;
- int v;
- for (int i = 0; i < m; ++i)
- if (ans > dp[(1 << m) - 1][i] + a[i][0])
- {
- ans = dp[(1 << m) - 1][i] + a[i][0];
- v = i;
- }
- cout << ans << "\n";
- Trace((1 << m) - 1, v);
- Shortest_path(v, 0);
- res.resize(unique(res.begin(), res.end()) - res.begin());
- cout << res.size() << "\n";
- for (auto i : res)
- cout << i << " ";
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Floyd();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement