Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <algorithm>
- #define INF 9223372036854775807
- std::ifstream fin("input.txt");
- std::ofstream fout("output.txt");
- int** matrix;
- long long** dp;
- int n;
- int main(int argc, char* argv[]) {
- fin >> n;
- //adjacency matrix
- matrix = new int*[n];
- for (int i = 0; i < n; i++) {
- matrix[i] = new int[n];
- for (int j = 0; j < n; j++)
- fin >> matrix[i][j];
- }
- //dp
- dp = new long long*[1 << n];
- for (int i = 0; i < (1 << n) + 1; i++) {
- dp[i] = new long long[n];
- for (int j = 0; j < n; j++)
- dp[i][j] = INF;
- }
- //start pos
- for (int i = 0; i < n; i++)
- dp[1 << i][i] = 0;
- //main
- for (int mask = 1; mask < 1 << n; mask++) {
- for (int cur_planet = 0; cur_planet < n; cur_planet++) {
- if (dp[mask][cur_planet] == INF) continue;
- for (int tg_planet = 0; tg_planet < n; tg_planet++) {
- if (!(mask & (1 << tg_planet)) && matrix[cur_planet][tg_planet] != -1) {
- dp[mask ^ (1 << tg_planet)][tg_planet] = std::min(dp[mask ^ (1 << tg_planet)][tg_planet], dp[mask][cur_planet] + matrix[cur_planet][tg_planet]);
- }
- }
- }
- }
- //out
- long long min = INF;
- for (int planet = 0; planet < n; planet++) {
- if (dp[(1 << n) - 1][planet] < min) min = dp[(1 << n) - 1][planet];
- }
- fout << min;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement