Pagini recente » Autentificare | Cod sursa (job #1533184) | Cod sursa (job #1722740) | Cod sursa (job #1546976) | Cod sursa (job #2253353)
#include <bits/stdc++.h>
using namespace std;
int costuri[19][19];
int dp[1 << 19][19];
const int hi = 1<<19;
int minimul = (1 << 29);
void initializare(){
for (int i = 0; i < hi; i++){
for (int j = 0; j < 19; j ++){
dp[i][j] = ( 1 << 29);
}
}
dp[1][0] = 0;
for (int i = 0; i < 19; i++){
for(int j = 0; j<19; j++){
costuri[i][j] = hi;
}
}
}
int main() {
ifstream f("hamilton.in");
ofstream g("hamilton.out");
int n, m;
f >> n >>m;
initializare();
for (int i = 1; i <= m; i++){
int x, y, cost;
f >> x >> y >> cost;
costuri[x][y] = cost;
}
for (int j = 0; j < (1 << n); j++){
for (int i = 0; i < n; i++){
if((1 << i) & j){
for (int x = 0; x < n; x ++){
if (!((1 << x) & j)){
dp[j^(1 << x)][x] = min(dp[j^(1 << x)][x],dp[j][i] + costuri[i][x]);
}
}
}
}
}
for (int j = 0; j < n; j++){
minimul = min(minimul,dp[(1 << n) - 1][j] + costuri[j][0]);
}
g << minimul;
return 0;
}