-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbee1947.cpp
More file actions
52 lines (39 loc) · 1.21 KB
/
Copy pathbee1947.cpp
File metadata and controls
52 lines (39 loc) · 1.21 KB
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
/*
* Contest : Beecrowd
* Problem : 1947 - Rota do Taxista
* Link : https://judge.beecrowd.com/pt/problems/view/1947
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
const int maxn = 16, maxm = (1 << 15) + 10;
int n, m, k;
int mat[maxn][maxn], dp[maxm][maxn];
pair<int, int> tur[maxn];
int solve(int mask, int i) {
if (mask == (1 << n) - 1)
return dp[mask][i] = 0; // caso todas as cidades já tenham sido visitadas
if (dp[mask][i] != -1)
return dp[mask][i]; // se a DP ja foi calculada
int ans = 1e9;
for (int v = 0; v < n; v++) {
if (mask & (1 << v) or !mat[i][v])
continue; // se a cidade atual já foi visitada ou não há rota de i à v
ans = min(ans, solve((mask | (1 << v)), v) + mat[i][v]); // pego o minimo entre ir para cada vizinho
}
return dp[mask][i] = ans;
}
int main() {
fastio
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> k;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
mat[a][b] = mat[b][a] = c;
}
for (int i = 1; i <= k; ++i) { cin >> tur[i].first >> tur[i].second; }
memset(dp, -1, sizeof(dp));
cout << solve(1, 0) << "\n"; // começando da cidade 0
}