Pagini recente » Cod sursa (job #1865146) | Cod sursa (job #2347634) | Cod sursa (job #1792101) | Cod sursa (job #3280270) | Cod sursa (job #3038585)
#include <bits/stdc++.h>
using ll=long long;
#define S second
#define F first
#define endl '\n'
#define spid ios_base::sync_with_stdio(false);cin.tie(NULL);
const int mod=1e9+7;
const double pi=3.14159265359;
const int maxn=19;
using namespace std;
int m,n;
vector<vector<pair<ll,ll>>> A(maxn,vector<pair<ll,ll>>());
ll dp[262144][19];
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
int main(){
InParser fin("hamilton.in");
ofstream cout("hamilton.out");
fin>>n>>m;
for(int i=0;i<m;i++){
int c1,c2,c3;
fin>>c1>>c2>>c3;
A[c2].push_back({c1,c3});
}
for(int i=0;i<n;i++){
for(int j=0;j<(1<<n);j++)dp[j][i]=1e18;
}
dp[1][0]=0;
for(int k=0;k<(1<<n);k++){
for(int i=0;i<n;i++){
if(k&(1<<i)){
for(int j=0;j<A[i].size();j++){
if(k&(1<<A[i][j].F))dp[k][i]=min(dp[k][i],dp[k^(1<<i)][A[i][j].F]+A[i][j].S);
}
}
}
}
ll mn=1e18;
for(int i=0;i<A[0].size();i++){
mn=min(mn,dp[(1<<n)-1][A[0][i].F]+A[0][i].S);
}
cout<<mn<<endl;
}