Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- using namespace std;
- typedef unsigned short ushort;
- int main()
- {
- ifstream in("aquarium.in");
- int n;
- in >> n;
- int m[13][13];
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- in >> m[i][j];
- int res[16384][13];
- int pm[16384][13];
- int pl[16384][13];
- for(ushort i = 0; i < (1 << n); i++)
- {
- for(int j = 0; j < n; j++)
- if(i & (1 << j))
- {
- ushort pmask = i ^ (1 << j);
- res[i][j] = 1000000000;
- if(pmask != 0)
- {
- for(int l = 0; l < n; l++)
- if(pmask & (1 << l))
- if(res[pmask][l] + m[l][j] < res[i][j])
- {
- res[i][j] = res[pmask][l] + m[l][j];
- pl[i][j] = l;
- pm[i][j] = pmask;
- }
- }
- else
- {
- res[i][j] = 0;
- pl[i][j] = -1;
- }
- }
- }
- in.close();
- int mlast = 0;
- for(int i = 0; i < n; i++)
- if(res[(1 << n) - 1][i] < res[(1 << n) - 1][mlast])
- mlast = i;
- vector<int> path;
- int pl_c = mlast, pm_c = (1 << n) - 1;
- while(pl_c != -1)
- {
- path.push_back(pl_c);
- int opl = pl_c, opm = pm_c;
- pl_c = pl[opm][opl];
- pm_c = pm[opm][opl];
- }
- ofstream out("aquarium.out");
- out << res[(1 << n) - 1][mlast] <<'\n';
- for(int i = path.size() - 1; i >= 0; i--)
- out << (i == path.size() - 1 ? "" : " ") << path[i] + 1;
- out << '\n';
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment