-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path116.cpp
79 lines (68 loc) · 2.01 KB
/
116.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
dynamic programming > traveling salesman problem (TSP)
difficulty: medium
date: 14/Nov/2019
by: @brpapa
*/
#include <iostream>
#include <queue>
#include <cstring>
#define INF 2147483647
#define min(a, b) ((a)<(b) ? (a):(b))
#define min3(a, b, c) ((a)<(b) ? min(a, c):min(b, c))
#define adj(n, sup) ((n)<(0)? (sup-1):((n)%(sup))) //reescreve n, caso tenha extrapolado os limites 0 e sup-1
using namespace std;
int m[10][100], tab[10][100];
int ans[100]; //ans[i] = linha do melhor caminho na coluna i
int qteLIN, qteCOL;
void dpBU(int qteLIN, int qteCOL) {
//caso base
for (int i = 0; i < qteLIN; i++)
tab[i][qteCOL-1] = m[i][qteCOL-1];
//preenche tab, da última para a primeira coluna, de cima para baixo
for (int j = qteCOL-2; j >= 0; j--)
for (int i = 0; i < qteLIN; i++)
tab[i][j] = m[i][j] + min3(
tab[adj(i-1, qteLIN)][j+1],
tab[i][j+1],
tab[adj(i+1, qteLIN)][j+1]
);
}
//! seria mais simples rodar dp de novo
void recover() {
int minAux = INF;
//coluna 0
for (int i = 0; i < qteLIN; i++)
if (tab[i][0] < minAux) {
minAux = tab[i][0];
ans[0] = i;
}
//próximas colunas
for (int j = 1; j < qteCOL; j++) {
minAux = INF;
priority_queue<int> path; //guarda linhas válidas
for (int k = -1; k <= 1; k++)
path.push(adj(ans[j-1]+k, qteLIN));
while (!path.empty()) {
int i = path.top();
path.pop();
if (tab[i][j] <= minAux) { //para minAux iguais prioriza o menor i
minAux = tab[i][j];
ans[j] = i;
}
}
}
}
int main() {
while (scanf("%d %d", &qteLIN, &qteCOL) != EOF) {
for (int i = 0; i < qteLIN; i++)
for (int j = 0; j < qteCOL; j++)
scanf("%d", &m[i][j]);
dpBU(qteLIN, qteCOL);
recover(); //gera ans
for (int j = 0; j < qteCOL-1; j++)
printf("%d ", ans[j]+1);
printf("%d\n%d\n", ans[qteCOL-1]+1, tab[ans[0]][0]);
}
return 0;
}