Problem:
Reorganization of Highway Network
The highway network in the country of Zuia consists of cities (points) and the roads that connect them. This highway network has been constructed to meet the following conditions:
There are $N$ cities (points) assigned the numbers 1 to $N$.
The roads are one-way, and there are only two roads at most that directly connect one point to another (and when there are two, they are opposite of each other).
From any point, you can always get to all the other points.
Maintaining a highway network costs money for each road. To avoid financial difficulties, the country of Zuia has decided to discontinue some roads and has appointed you as the person in charge. You have to reorganize the entire highway network so that the cost of maintaining it is minimized, without changing the conditions above.
Write a program which reads the number of points and information about the roads and finds the minimum maintenance cost of the entire highway network after the reorganization.
Input
The input is given in the following form.
$N$ $M$
$u_1$ $v_1$ $c_1$
$u_2$ $v_2$ $c_2$
:
$u_M$ $v_M$ $c_M$
In the first line, the number of points $N$ ($2 \leq N \leq 13$) and the number of roads $M$ ($N \leq M \leq N \times (N-1)$) are given. In the following $M$ lines, the starting point $u_i$ and the ending point $v_i$ of the $i$-th road ($1 \leq u_i \ne v_i \leq N$) and the maintenance cost $c_i$ ($1 \leq c_i \leq 10,000$) are given.
Output
Output the minimum value of the maintenance cost of the entire highway network after the reorganization in a line.
Sample Input and Output
Sample Input 1
3 4
1 2 2
2 3 3
1 3 1
3 1 1
Sample Output 1
6
Sample Input 2
5 8
1 2 7
1 5 2
2 1 8
2 3 3
3 4 5
4 5 8
5 1 5
5 2 1
Sample Output 2
24

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rng(i,l,r) for(int i=(l);i<(r);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrng(i,l,r) for(int i=(r)-1;i>=(l);i--)
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define pb push_back

template<class T> bool chmin(T &a,T b){return (a>b)?a=b,1:0;}
template<class T> bool chmax(T &a,T b){return (a<b)?a=b,1:0;}

int dp1[13][13][1<<13];
int dp2[13][13][1<<13];
int dp3[1<<13];

signed main(){
	int N,M;cin>>N>>M;
	
	vector<vector<int>> d(N,vector<int>(N,1e9));
	rep(i,M){
		int u,v,w;cin>>u>>v>>w;
		u--,v--;
		d[u][v]=w;
	}
	
	rep(u,N)rep(i,N)rep(s,1<<N)dp1[u][i][s]=1e9;
	rep(i,N){
		dp1[i][i][1<<i]=0;
	}
	rep(u,N)rep(s,1<<N)rep(i,N){
		if(s>>i&1){
			rep(j,N){
				if(s>>j&1)continue;
				chmin(dp1[u][j][s|(1<<j)],dp1[u][i][s]+d[i][j]);
			}
		}
	}
	
	rep(u,N)rep(v,N){
		rep(s,1<<N){
			dp2[u][v][s]=1e9;
			
			if(s>>u&1)continue;
			if(s>>v&1)continue;
			
			rep(a,N)rep(b,N){
				if((s>>a&1)&&(s>>b&1)){
					chmin(dp2[u][v][s],d[u][a]+d[b][v]+dp1[a][b][s]);
				}
			}
		}
	}
	
	rep(s,1<<N)dp3[s]=1e9;
	rep(i,N)dp3[1<<i]=0;
	rep(s,1<<N){
		int rs=((1<<N)-1)^s;
		int cur=rs;
		
		while(cur){
			rep(u,N)rep(v,N){
				if((s>>u&1)&&(s>>v&1)){
					chmin(dp3[s|cur],dp3[s]+dp2[u][v][cur]);
				}
			}
			cur--;
			cur&=rs;
		}
	}
	
	cout<<dp3[(1<<N)-1]<<"\n";
}
accuracy: 1
time_ms: 320
memory_kb: 25048
final_rank: 15

code_snippet: // AOJ #0450 Reorganization of Highway Network
// 2025.4.30

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (ll)1e15;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin(), m = Cin();

    vector<vector<ll>> c(n, vector<ll>(n, INF));
    for(int i = 0; i < m; i++){
        int u = Cin()-1, v = Cin()-1; ll w = Cin();
        c[u][v] = min(c[u][v], w);
    }

    int LIM = 1 << n;
    vector<ll> cost_cycle(LIM, INF);
    for(int r = 0; r < n; r++){
        vector<vector<ll>> dp(LIM, vector<ll>(n, INF));
        dp[1<<r][r] = 0;
        for(int mask = 1; mask < LIM; mask++){
            if(!(mask & (1<<r))) continue;
            for(int v = 0; v < n; v++){
                if(v == r || !(mask & (1<<v))) continue;
                int pm = mask ^ (1<<v);
                if(!(pm & (1<<r))) continue;
                ll best = INF;
                for(int u = 0; u < n; u++){
                    if(!(pm & (1<<u))) continue;
                    if(dp[pm][u] == INF || c[u][v] == INF) continue;
                    best = min(best, dp[pm][u] + c[u][v]);
                }
                dp[mask][v] = best;
            }
            for(int v = 0; v < n; v++){
                if(v == r) continue;
                if(dp[mask][v] < INF && c[v][r] < INF)
                    cost_cycle[mask] = min(cost_cycle[mask], dp[mask][v] + c[v][r]);
            }
        }
    }

    vector<ll> f(LIM, INF);
    f[0] = 0;
    for(int mask = 1; mask < LIM; mask++){
        int bc = __builtin_popcount(mask);
        if(bc == 1){
            f[mask] = 0;
            continue;
        }
        f[mask] = cost_cycle[mask];
        int low = mask & -mask;
        for(int A = (mask-1)&mask; A; A = (A-1)&mask){
            if(!(A & low)) continue;
            int B = mask ^ A;
            if(B == 0) continue;
            ll fa = f[A], fb = f[B];
            if(fa == INF || fb == INF) continue;
            ll e1 = INF, e2 = INF;
            for(int u = 0; u < n; u++) if(A & (1<<u))
                for(int v = 0; v < n; v++) if(B & (1<<v)) e1 = min(e1, c[u][v]);
            if(e1 == INF) continue;
            for(int u = 0; u < n; u++) if(B & (1<<u))
                for(int v = 0; v < n; v++) if(A & (1<<v)) e2 = min(e2, c[u][v]);
            if(e2 == INF) continue;
            f[mask] = min(f[mask], fa + fb + e1 + e2);
        }
    }

    ll ans = f[LIM-1];
    for(int m1 = 1; m1 < LIM-1; m1++){
        if(cost_cycle[m1] == INF) continue;
        int cm = (LIM-1) ^ m1;
        for(int s = m1; s; s = (s-1)&m1){
            int m2 = cm | s;
            if(cost_cycle[m2] == INF) continue;
            ans = min(ans, cost_cycle[m1] + cost_cycle[m2]);
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 4168
final_rank: 8

code_snippet: #include <bits/stdc++.h>

int main() {
    int N, M;
    std::cin >> N >> M;
    std::vector<std::vector<std::pair<int, int>>> g(N);
    for (int i{} ; i < M ; i++) {
        int u, v, w;
        std::cin >> u >> v >> w;
        u--; v--;
        g[u].push_back(std::make_pair(v, w));
    }
    const int INF{(int)2e9};
    std::vector dp(N, std::vector(N, std::vector<int>(1 << N, INF)));
    for (int s{} ; s < N ; s++) {
        dp[s][s][1 << s] = 0;
        for (int bit{} ; bit < (1 << N) ; bit++) {
            for (int t{} ; t < N ; t++) if ((bit & (1 << t)) and dp[s][t][bit] != INF) {
                for (auto [v, w] : g[t]) {
                    dp[s][v][bit | (1 << v)] = std::min(dp[s][v][bit | (1 << v)], dp[s][t][bit] + w);
                }
            }
        }
    }
    std::vector<int> ep(1 << N, INF);
    ep[1 << 0] = 0;
    const int all{(1 << N) - 1};
    for (int bit{} ; bit < (1 << N) ; bit++) if (ep[bit] != INF) {
        for (int s{} ; s < N ; s++) if (bit & (1 << s)) {
            for (int t{} ; t < N ; t++) if (bit & (1 << t)) {
                for (int mask{all ^ bit} ; mask ; mask = (mask - 1) & (all ^ bit)) {
                    ep[bit | mask] = std::min(ep[bit | mask], ep[bit] + dp[s][t][mask | (1 << s) | (1 << t)]);
                }
            }
        }
    }
    std::cout << ep[all] << '\n';



















}
accuracy: 1
time_ms: 20
memory_kb: 8924
final_rank: 1

code_snippet: #include <bits/stdc++.h>

int main() {
    const int INF = 1e8;
    int n, m;
    std::cin >> n >> m;
    std::vector graph(n, std::vector<int>(n, INF));
    for (int i = 0; i < m; i++) {
        int u, v, w;
        std::cin >> u >> v >> w;
        u--; v--;
        graph[u][v] = w;
    }

    std::vector dp(1 << n, INF);
    for (int i = 0; i < n; i++) {
        dp[1 << i] = 0;
    }

    std::vector elem(1 << n, std::vector<int>());
    std::vector no_elem(1 << n, std::vector<int>());
    for (int state = 0; state < (1 << n); state++) {
        for (int i = 0; i < n; i++) {
            if (state & (1 << i)) elem[state].push_back(i);
            else no_elem[state].push_back(i);
        }
    }

    std::vector dp2(1 << n, std::vector(n, std::vector<int>(n, INF)));
    for (int i = 0; i < n; i++) {
        dp2[1 << i][i][i] = 0;
    }
    for (int state = 0; state < (1 << n); state++) {
        for (int i: elem[state]) {
            for (int j: elem[state]) {
                if (dp2[state][i][j] == INF) continue;
                for (int k: no_elem[state]) {
                    dp2[state | (1 << k)][i][k] = std::min(dp2[state | (1 << k)][i][k], dp2[state][i][j] + graph[j][k]);
                }
            }
        }
    }
    for (int state = 0; state < (1 << n); state++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[state] = std::min(dp[state], dp2[state][i][j] + graph[j][i]);
            }
        }
    }
    for (int state = 1; state < (1 << n); state++) {
        for (int lhs = (state - 1) & state; lhs > 0; lhs = (lhs - 1) & state) {
            int rhs = state ^ lhs;
            if (dp[lhs] == INF) continue;
            // lhs -> rhs
            int lhs_to_rhs = INF;
            int rhs_to_lhs = INF;
            for (int i: elem[lhs]) {
                for (int j: elem[rhs]) {
                    lhs_to_rhs = std::min(lhs_to_rhs, graph[i][j]);
                }
            }
            for (int i: elem[rhs]) {
                for (int j: elem[lhs]) {
                    rhs_to_lhs = std::min(rhs_to_lhs, graph[i][j]);
                }
            }
            dp[state] = std::min(dp[state], dp[lhs] + dp[rhs] + lhs_to_rhs + rhs_to_lhs);
            for (int i: elem[lhs]) {
                for (int j: elem[lhs]) {
                    dp[state] = std::min(dp[state], dp[lhs] + dp2[rhs | (1 << i) | (1 << j)][i][j]);
                    dp[state] = std::min(dp[state], dp[lhs] + dp2[rhs | (1 << i) | (1 << j)][j][i]);
                }
            }

            for (int i: elem[lhs]) {
                for (int j: elem[rhs]) {
                    dp[state] = std::min(dp[state], dp[lhs] + dp2[rhs | (1 << i)][i][j] + graph[j][i]);
                }
            }
        }
    }
    // for (int i = 0; i < (1 << n); i++) {
    //     std::cerr << i << ": " << dp[i] << '\n';
    // }
    std::cout << dp.back() << '\n';
}
accuracy: 1
time_ms: 180
memory_kb: 13632
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(a) for(ll _=0;_<a;++_)
#define rep2(i,a) for(ll i=0;i<a;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,a) for(ll i=(ll)a-1;i>=0;--i)
#define rrep2(i,a,b) for(ll i=(ll)b-1;i>=(ll)a;--i)
#define rrep(...) overload(__VA_ARGS__,rrep2,rrep1)(__VA_ARGS__)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define eb emplace_back
#define pb push_back
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
template<typename T,typename U>inline bool chmin(T&a,const U&b){return (a>b?a=b,true:false);}
template<typename T,typename U>inline bool chmax(T&a,const U&b){return (a<b?a=b,true:false);}
static constexpr ll inf=numeric_limits<ll>::max()/2;
int main(){
    ll n,m;
    cin>>n>>m;
    vector<ll>dp(1<<n,inf);
    rep(i,n)dp[1<<i]=0;
    vector<vector<ll>>cost(n,vector<ll>(n,inf));
    rep(i,m){
        ll u,v,c;
        cin>>u>>v>>c;
        u--,v--;
        cost[u][v]=c;
    }
    vector<vector<vector<ll>>>ndp(1<<n,vector<vector<ll>>(n,vector<ll>(n,inf)));
    rep(i,n)rep(j,n)if(cost[i][j]!=inf)ndp[1<<i|1<<j][i][j]=cost[i][j];
    rep(bit,1,1<<n){
        rep(i,n)rep(j,n)if(ndp[bit][i][j]!=inf){
            if(cost[j][i]!=inf)chmin(ndp[bit][i][i],ndp[bit][i][j]+cost[j][i]);
            rep(k,n)if(!((bit>>k)&1))if(cost[j][k]!=inf){
                chmin(ndp[bit|1<<k][i][k],ndp[bit][i][j]+cost[j][k]);
            }
        }
    }
    rep(bit,1<<n){
        rep(i,n)chmin(dp[bit],ndp[bit][i][i]);
    }
    //for(auto&i:dp)cerr<<i<<endl;
    rep(bit,1,1<<n){
        ll nowbit=bit-1;
        while(nowbit){
            //cerr<<nowbit<<endl;
            nowbit&=bit;
            if(nowbit<(bit-nowbit))break;
            ll nxtbit=bit^nowbit;
            if(dp[nowbit]==inf||dp[nxtbit]==inf){
                nowbit--;
                continue;
            }
            ll atob=inf,btoa=inf;
            rep(i,n)if((nowbit>>i)&1){
                rep(j,n)if((nxtbit>>j)&1){
                    chmin(atob,cost[i][j]);
                    chmin(btoa,cost[j][i]);
                }
            }
            if(atob==inf||btoa==inf){
                nowbit--;
                continue;
            }
            chmin(dp[bit],dp[nowbit]+dp[nxtbit]+atob+btoa);
            nowbit--;
        }
    }
    cout<<dp.back()<<endl;
}
accuracy: 1
time_ms: 140
memory_kb: 17712
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);i++)

constexpr int inf=1e9+10;
constexpr char newl='\n';
constexpr int dx[]={0,1,0,-1};
constexpr int dy[]={1,0,-1,0};
using ll=long long;

bool valid(int i,int j,int h,int w){
    return 0<=i && i<h && 0<=j && j<w;
}

template<typename T>
bool chmin(T &a,T b){
    return b<a ? a=b,true:false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n,m;
    cin >> n >> m;
    vector<vector<pair<int,int>>> g(n);
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        a--,b--;
        g[a].emplace_back(b,c);
    }
    vector<vector<vector<int>>> dist(1<<n,vector<vector<int>>(n,vector<int>(n,inf)));
    //dp1[i][j][T]->dp[i][k][T+{j}]
    using P=pair<int,pair<pair<int,int>,int>>;
    priority_queue<P,vector<P>,greater<P>> q;
    rep(i,n){
        dist[0][i][i]=0;
        for(auto [j,c]:g[i]){
            dist[0][i][j]=c;
            q.push({c,{{i,j},0}});
        }
    }
    while(!q.empty()){
        auto [d,pp]=q.top();q.pop();
        auto [p,T]=pp;
        auto [i,j]=p;
        if(d!=dist[T][i][j])continue;
        for(auto [k,c]:g[j]){
            if(chmin(dist[T | (1<<j)][i][k],d+c)){
                q.push({d+c,{{i,k},T|(1<<j)}});
            }
        }
    }

    const int all_mask=(1<<n)-1;
    vector<int> dp(1<<n,inf);
    rep(i,n)dp[1<<i]=0;
    for(int S=1;S<(1<<n);S++){
        const int nxt_mask=all_mask^S;
        const int now=dp[S];
        rep(i,n)if((S>>i)&1)rep(j,n)if((S>>j)&1){//i->j
            for(int T=nxt_mask;;T=(T-1)&(nxt_mask)){
                chmin(dp[S|T],now+dist[T][i][j]);
                if(T==0)break;
            }
        }
    }
    cout << dp.back() << endl;
}
accuracy: 1
time_ms: 700
memory_kb: 45268
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define fi first
#define se second
#define pb emplace_back
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
const ll mod=998244353;
const ll inf=1001001001001001;
int main(){
    ll n,m;cin>>n>>m;
    vvi g(n,vi(n,inf));
    rep(i,m){
        ll a,b,c;cin>>a>>b>>c;
        g[a-1][b-1]=c;
    }
    ll t=1;rep(i,n-1)t*=3;
    vvi dp(t,vi(n-1,inf));
    dp[0][0]=0;
    vi fac(n+1);
    fac[0]=1;rep(i,n)fac[i+1]=fac[i]*3;
    auto encode=[&](vi v){
        ll res=0;
        for(ll i=n-2;i>=0;i--){
            res*=3;res+=v[i];
        }
        return res;
    };
    auto decode=[&](ll a){
        vi res(n-1);
        rep(i,n-1){
            res[i]=a%3;a/=3;
        }
        return res;
    };
    rep(bit,t){
        vi v=decode(bit);
        bool tmp=false;
        rep(i,n-1)if(v[i]==1)tmp=true;
        if(tmp){
            rep(i,n-1)if(v[i]==1){
                ll nbit=bit;
                rep(j,n-1)if(v[j]==1)nbit+=fac[j];
                rep(j,n){
                    if(j==0||v[j-1]==2)chmin(dp[nbit][0],dp[bit][i]+g[i+1][j]);
                    else if(v[j-1]==0)chmin(dp[bit+fac[j-1]][j-1],dp[bit][i]+g[i+1][j]);
                }
            }
        }
        else{
            rep(i,n)if(i==0||v[i-1]==2)REP(j,1,n)if(v[j-1]==0){
                chmin(dp[bit+fac[j-1]][j-1],dp[bit][0]+g[i][j]);
            }
        }
    }
    ll tmp=0;
    rep(i,n-1)tmp+=2*fac[i];
    out(dp[tmp][0]);
    // out(dp[27][0]);
    // out(dp[29][1]);
}
accuracy: 1
time_ms: 190
memory_kb: 73696
final_rank: 18

code_snippet: #line 1 "a.cpp"
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>
#include <cassert>
#include <bitset>
#include <chrono>
#include <random>
#include <memory>
 
/* macro */
 
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n-1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i< (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n-i64(1))); i>=(i64)(a); i--)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define Bit(n) (1LL<<(n))
 
/* macro end */
 
/* template */
 
namespace ebi {
 
#ifdef LOCAL
#define debug(...)                                                    \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
 
void debug_out() { std::cerr << std::endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
    return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
    for (std::size_t i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}
 
using size_t = std::size_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
 
template<class T> void fill(std::vector<T> &v) { for(T &e: v) std::cin >> e; }
 
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
 
template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    else return gcd(b, a%b);
}
 
template <class T>
T lcm(T a, T b) {
    return (a*b)/gcd(a,b);
}
 
template<class T>
T Pow(T x, i64 n) {
    T res = 1;
    while(n>0){
        if(n&1) res = res *x;
        x = x*x;
        n>>=1;
    }
    return res;
}
 
template<class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}
 
constexpr i64 LNF = std::numeric_limits<i64>::max()/4;
 
constexpr int INF = std::numeric_limits<int>::max()/2;
 
const long double PI = acos(-1);
 
const std::vector<int> dy = {1,0,-1,0,1,1,-1,-1};
const std::vector<int> dx = {0,1,0,-1,1,-1,1,-1};
 
}
 
/* template end */

#line 2 "graph/template.hpp"

#line 4 "graph/template.hpp"

namespace ebi {

template<class T>
struct Edge {
    int to;
    T cost;
    Edge(int to, T cost=1) : to(to), cost(cost) { }
};

template<class T>
struct Graph : std::vector<std::vector<Edge<T>>> {
    using std::vector<std::vector<Edge<T>>>::vector;
    void add_edge(int u, int v, T w, bool directed = false) {
        (*this)[u].emplace_back(v, w);
        if(directed) return; 
        (*this)[v].emplace_back(u, w);
    }
};

struct graph : std::vector<std::vector<int>> {
    using std::vector<std::vector<int>>::vector;
    void add_edge(int u, int v, bool directed = false) {
        (*this)[u].emplace_back(v);
        if(directed) return;
        (*this)[v].emplace_back(u);
    }
};

} // namespace ebi
#line 136 "a.cpp"

namespace ebi {

using ld = long double;

void main_() {
    int n,m;
    std::cin >> n >> m;
    std::vector w(n, std::vector<i64>(n, LNF));
    graph g(n);
    rep(i,0,m) {
        int u,v;
        i64 c;
        std::cin >> u >> v >> c;
        u--; v--;
        g.add_edge(u, v);
        w[u][v] = c;
    }
    std::vector dist(n, std::vector(n, std::vector<i64>(1<<n, LNF)));
    rep(i,0,n) {
        for(auto nv: g[i]) {
            dist[i][nv][0] = w[i][nv];
        }
        rep(bit,0,1<<n) {
            rep(v,0,n) {
                if(dist[i][v][bit] == LNF) continue;
                for(auto nv: g[v]) {
                    chmin(dist[i][nv][bit ^ (1<<v)], dist[i][v][bit] + w[v][nv]);
                }
            }
        }
    }
    std::vector dp(1<<n, LNF);
    std::vector seen(1<<n, -1);
    auto f = [&](auto &&self, int S) -> i64 {
        if(seen[S] > 0) return dp[S];
        if(S == 1) {
            seen[S] = 1;
            return dp[S] = 0;
        }
        for(int T = (S-1)&S; T > 0; T = (T-1)&S) {
            rep(i,0,n) {
                if(!(S & (1<<i))) continue;
                rep(j,0,n) {
                    if(!(S & (1<<j))) continue;
                    if((T & (1<<i)) || (T & (1<<j))) continue;
                    chmin(dp[S], self(self, S^T) + dist[i][j][T]);
                }
            }
        }
        seen[S] = 1;
        return dp[S];
    };
    std::cout << f(f, Bit(n)-1) << std::endl;
}
 
}

 
int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    // std::cin >> t;
    while(t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 14960
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tuplis = array<ll, 3>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF=0x1fffffffffffffff;
const ll MINF=0x7fffffffffff;
const int INF=0x3fffffff;
const int MOD=1000000007;
const int MODD=998244353;
const ld DINF=numeric_limits<ld>::infinity();
const ld EPS=1e-9;
const ld PI=3.1415926535897932;
const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
#define overload5(a,b,c,d,e,name,...) name
#define overload4(a,b,c,d,name,...) name
#define overload3(a,b,c,name,...) name
#define rep1(n) for(ll i=0;i<n;++i)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i<b;++i)
#define rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i=n;i--;)
#define rrep2(i,n) for(ll i=n;i--;)
#define rrep3(i,a,b) for(ll i=b;i-->(a);)
#define rrep4(i,a,b,c) for(ll i=(a)+((b)-(a)-1)/(c)*(c);i>=(a);i-=c)
#define rrep(...) overload4(__VA_ARGS__,rrep4,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define each1(i,a) for(auto&&i:a)
#define each2(x,y,a) for(auto&&[x,y]:a)
#define each3(x,y,z,a) for(auto&&[x,y,z]:a)
#define each4(w,x,y,z,a) for(auto&&[w,x,y,z]:a)
#define each(...) overload5(__VA_ARGS__,each4,each3,each2,each1)(__VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(),(i).rend()
#define rall2(i,k) (i).rbegin(),(i).rbegin()+k
#define rall3(i,a,b) (i).rbegin()+a,(i).rbegin()+b
#define rall(...) overload3(__VA_ARGS__,rall3,rall2,rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
#define dsum(...) accumulate(all(__VA_ARGS__),0.0L)
#define Msum(...) accumulate(all(__VA_ARGS__),0_M)
#define elif else if
#define unless(a) if(!(a))
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;in(__VA_ARGS__)
#define Sort(a) sort(all(a))
#define Rev(a) reverse(all(a))
#define Uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define vec(type,name,...) vector<type>name(__VA_ARGS__)
#define VEC(type,name,size) vector<type>name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
#define vvv(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
template<class T> auto min(const T& a){ return *min_element(all(a)); }
template<class T> auto max(const T& a){ return *max_element(all(a)); }
inline ll popcnt(ull a){ return __builtin_popcountll(a); }
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
vector<ll> iota(ll n, ll begin = 0){ vector<ll> a(n); iota(a.begin(), a.end(), begin); return a; }
vector<pll> factor(ull x){ vector<pll> ans; for(ull i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<ll> divisor(ull x){ vector<ll> ans; for(ull i = 1; i * i <= x; i++) if(x % i == 0) ans.push_back(i); rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]); return ans; }
template<class T> unordered_map<T, ll> press(vector<T>& a){ Uniq(a); unordered_map<T, ll> ans; rep(a.size()) ans[a[i]] = i; return ans; }
template<class T> auto run_press(const T& a){ vector<pair<remove_const_t<remove_reference_t<decltype(a[0])>>, ll>> ans; each(x, a){ if(ans.empty() || ans.back().first != x) ans.emplace_back(x, 1); else ans.back().second++; } return ans; }
int scan(){ return getchar(); }
void scan(int& a){ scanf("%d", &a); }
void scan(unsigned& a){ scanf("%u", &a); }
void scan(long& a){ scanf("%ld", &a); }
void scan(long long& a){ scanf("%lld", &a); }
void scan(unsigned long long& a){ scanf("%llu", &a); }
void scan(char& a){ do a = getchar(); while(a == ' ' || a == '\n'); }
void scan(float& a){ scanf("%f", &a); }
void scan(double& a){ scanf("%lf", &a); }
void scan(long double& a){ scanf("%Lf", &a); }
void scan(vector<bool>& a){ for(unsigned i = 0; i < a.size(); i++){ int b; scan(b); a[i] = b; } }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto&& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto&& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto&& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto&& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void in(){}
template <class... T> void in(T&... a){ (void)initializer_list<int>{ (scan(a), 0)... }; }
void print(){ putchar(' '); }
void print(bool a){ printf("%d", a); }
void print(int a){ printf("%d", a); }
void print(unsigned a){ printf("%u", a); }
void print(long a){ printf("%ld", a); }
void print(long long a){ printf("%lld", a); }
void print(unsigned long long a){ printf("%llu", a); }
void print(char a){ printf("%c", a); }
void print(char a[]){ printf("%s", a); }
void print(const char a[]){ printf("%s", a); }
void print(float a){ printf("%.15f", a); }
void print(double a){ printf("%.15f", a); }
void print(long double a){ printf("%.15Lf", a); }
void print(const string& a){ for(auto&& i : a) print(i); }
template<class T> void print(const complex<T>& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); }
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ print(p.first); putchar(' '); print(p.second); }
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
#ifdef DEBUG
inline ll __lg(ull x){ return 63 - __builtin_clzll(x); }
#define debug(...) { print(#__VA_ARGS__); print(":"); out(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
int first(bool i = true){ return out(i?"first":"second"); }
int First(bool i = true){ return out(i?"First":"Second"); }
int yes(bool i = true){ return out(i?"yes":"no"); }
int Yes(bool i = true){ return out(i?"Yes":"No"); }
int No(){ return out("No"); }
int YES(bool i = true){ return out(i?"YES":"NO"); }
int NO(){ return out("NO"); }
int Yay(bool i = true){ return out(i?"Yay!":":("); }
int possible(bool i = true){ return out(i?"possible":"impossible"); }
int Possible(bool i = true){ return out(i?"Possible":"Impossible"); }
int POSSIBLE(bool i = true){ return out(i?"POSSIBLE":"IMPOSSIBLE"); }
void Case(ll i){ printf("Case #%lld: ", i); }


#define ll int
struct WeightedEdge{
    ll to, cost;
    WeightedEdge(){}
    WeightedEdge(ll to, ll cost): to(to), cost(cost){}
    operator ll() const { return to; }
};
struct WeightedGraph{
    using E = WeightedEdge;
    vector<vector<E>> g;
    WeightedGraph(){}
    WeightedGraph(ll n): g(n){}
    vector<E>& operator[](ll at){ return g[at]; }
    operator vector<vector<E>>&(){ return g; }
    auto begin(){ return g.begin(); }
    auto end(){ return g.end(); }
    auto begin() const { return g.cbegin(); }
    auto end() const { return g.cend(); }
    ll size() const { return g.size(); }
    void resize(ll n){ g.resize(n); }
    const vector<E>& operator[](ll at) const { return g[at]; }
    operator const vector<vector<E>>&() const { return g; }
    void add_edge(ll a, ll b, ll cost){
        g[a].emplace_back(b, cost);
        g[b].emplace_back(a, cost);
    }
    void add_directed_edge(ll from, ll to, ll cost){
        g[from].emplace_back(to, cost);
    }
    template<ll start_index = 1> void input_graph(ll m){
        while(m--){
            ll a, b, c;
            scanf("%lld%lld%lld", &a, &b, &c);
            a -= start_index;
            b -= start_index;
            g[a].emplace_back(b, c);
            g[b].emplace_back(a, c);
        }
    }
    template<ll start_index = 1> void input_directed_graph(ll m){
        while(m--){
            ll a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            a -= start_index;
            b -= start_index;
            g[a].emplace_back(b, c);
        }
    }
    template<ll start_index = 1> void input_tree(ll n = -1){ if(n == -1) n = g.size(); input_graph<start_index>(n - 1); }
};
int main(){
    LL(n,m);
    WeightedGraph g(n);
    g.input_directed_graph(m);
    vec(ll,cost,1<<n,INF);
    cost[1]=0;
    rep(bit,1,1<<n){
        unordered_map<ll,ll>cost2;
        pq<array<int,3>>q;
        auto update=[&](ll cost,ll bit,ll i){
            const ll key=i<<16|bit;
            if(chmin(cost2.try_emplace(key,INF).first->second,cost))q.push({cost,bit,i});
        };
        rep(n)if(bit&1<<i)update(cost[bit],bit,i);
        while(q.size()){
            auto[c,bit1,at]=q.top();
            q.pop();
            if(cost2[at<<16|bit1]!=c)continue;
            each(i,g[at]){
                if(bit&1<<i)chmin(cost[bit1],c+i.cost);
                else update(c+i.cost,bit1|1<<i,i);
            }
        }
    }
    out(cost.back());
}
accuracy: 1
time_ms: 720
memory_kb: 4504
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0; i<(n); i++)

template<class T>
void chmin(T& l,T r){ if(r<l) l=r; }

int N,M;
ll E[13][13];
ll dp[13][13][1<<13];
ll dp2[1<<13];

int main(){
  cin>>N>>M;
  rep(i,N) rep(j,N) E[i][j]=1000000000;
  rep(i,N) E[i][i]=0;
  rep(i,M){ int u,v,c; cin>>u>>v>>c; u--; v--; E[u][v]=c; }
  rep(s,N) rep(t,N) rep(b,1<<N) dp[s][t][b]=1000000000;
  rep(i,N) rep(j,N) rep(k,N) chmin(E[j][k],E[j][i]+E[i][k]);
  
  rep(s,N){
    dp[s][s][1<<s]=0;
    rep(b,1<<N){
      rep(p,N) rep(e,N) chmin(dp[s][e][b],dp[s][p][b]+E[p][e]);
      rep(p,N) rep(e,N){
        if((b&(1<<p))==0) continue;
        if((b&(1<<e))!=0) continue;
        chmin(dp[s][e][b|(1<<e)],dp[s][p][b]+E[p][e]);
      }
    }
    rep(d,N) rep(e,N) rep(b,1<<N) chmin(dp[s][e][b],dp[s][e][b|(1<<d)]);
  }

  rep(b,1<<N) dp2[b]=1000000000;
  dp2[1]=0;
  for(int b=1; b<(1<<N); b++){
    rep(u,N) rep(v,N){
      if((b&(1<<u))==0) continue;
      if((b&(1<<v))==0) continue;
      for(int e=((1<<N)-1)&(~b); e!=0; e=(e-1)&(~b)) chmin(dp2[b|e],dp2[b]+dp[u][v][e]);
    }
  }

  cout<<dp2[(1<<N)-1]<<endl;
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 13992
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
//#define MOD 998244353
#define EPS 0.000000001
using namespace std;



#define SIZE 14

struct Edge{
	Edge(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

int N,E;
int POW[SIZE];
int dp[SIZE][SIZE][1024*64],dp2[1024*64];
vector<Edge> G[SIZE];


int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d",&N,&E);

	int from,to,cost;

	for(int i = 0; i < E; i++){

		scanf("%d %d %d",&from,&to,&cost);
		from--;
		to--;

		G[from].push_back(Edge(to,cost));
	}

	for(int s = 0; s < N; s++){
		for(int t = 0; t < N; t++){
			for(int state = 0; state < POW[N]; state++){

				dp[s][t][state] = BIG_NUM;
			}
		}
	}

	for(int s = 0; s < N; s++){

		dp[s][s][0] = 0;
		for(int i = 0; i < G[s].size(); i++){ //sから行けるノード

			dp[s][G[s][i].to][0] = G[s][i].cost;
		}

		for(int state = 0; state < POW[N]; state++){
			for(int last = 0; last < N; last++){
				if(dp[s][last][state] == BIG_NUM)continue;

				for(int i = 0; i < G[last].size(); i++){

					int next_node = G[last][i].to;
					int next_dist = dp[s][last][state]+G[last][i].cost;
					int next_state = state|POW[last];

					if(dp[s][next_node][next_state] > next_dist){

						dp[s][next_node][next_state] = next_dist;
					}
				}
			}
		}
	}

	for(int state = 0; state < POW[N]; state++){

		dp2[state] = BIG_NUM;
	}

	dp2[0] = 0;
	for(int i = 0; i < N; i++){

		dp2[POW[i]] = 0;
	}

	for(int state = 0; state < POW[N]-1; state++){
		if(dp2[state] == BIG_NUM)continue;

		int rest = (POW[N]-1)-state;

		for(int s = 0; s < N; s++){
			if((state&POW[s]) == 0)continue;

			for(int t = 0; t < N; t++){
				if((state&POW[t]) == 0)continue;

				for(int add = rest; add > 0; add = (add-1)&rest){
					if(dp[s][t][add] == BIG_NUM)continue;

					dp2[state|add] = min(dp2[state|add],dp2[state]+dp[s][t][add]);
				}
			}
		}
	}

	printf("%d\n",dp2[POW[N]-1]);

	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 9328
final_rank: 4

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
//#define MOD 998244353
#define EPS 0.000000001
using namespace std;



#define SIZE 15

struct Edge{
	Edge(int arg_to,ll arg_cost){
		to = arg_to;
		cost = arg_cost;
	}

	int to;
	ll cost;
};

ll POW[SIZE];
ll dp[SIZE][SIZE][1024*32],dp2[1024*32];
vector<Edge> G[SIZE];

int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	int N,E;
	scanf("%d %d",&N,&E);

	int from,to;
	ll cost;

	for(int i = 0; i < E; i++){

		scanf("%d %d %lld",&from,&to,&cost);
		from--;
		to--;

		G[from].push_back(Edge(to,cost));
	}

	for(int s = 0; s < N; s++){
		for(int t = 0; t < N; t++){
			for(int state = 0; state < POW[N]; state++){

				dp[s][t][state] = HUGE_NUM;
			}
		}
	}

	for(int i = 0; i < N; i++){

		dp[i][i][0] = 0; //dp[始点][終点][始点と終点以外の、頂点集合] := 最小訪問コスト
		for(int k = 0; k < G[i].size(); k++){

			dp[i][G[i][k].to][0] = G[i][k].cost;
 		}
		for(int state = 0; state < POW[N]; state++){
			for(int last = 0; last < N; last++){

				if(dp[i][last][state] == HUGE_NUM)continue;

				for(int k = 0; k < G[last].size(); k++){

					int next = G[last][k].to;
					ll next_cost = dp[i][last][state]+G[last][k].cost;

					int next_state = state|POW[last];

					if(dp[i][next][next_state] > next_cost){

						dp[i][next][next_state] = next_cost;
					}
				}
			}
		}
	}

	for(int state = 0; state < POW[N]; state++){

		dp2[state] = HUGE_NUM;
	}

	dp2[0] = 0; //dp2[集合] = 最小コスト

	for(int i = 0; i < N; i++){

		dp2[POW[i]] = 0;
	}

	for(int state = 0; state < POW[N]; state++){
		if(dp2[state] == HUGE_NUM)continue;

		int rest = (POW[N]-1)-state; //補集合

		for(int s = 0; s < N; s++){
			if((state&POW[s]) == 0)continue;
			for(int t = 0; t < N; t++){
				if((state&POW[t]) == 0)continue;

				for(int add = rest; add != 0; add = (add-1)&rest){

					if(dp[s][t][add] == HUGE_NUM)continue;

					dp2[state+add] = min(dp2[state+add],dp2[state]+dp[s][t][add]);
				}
			}
		}
	}

	printf("%lld\n",dp2[POW[N]-1]);

	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 14768
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 15

struct Edge{
	Edge(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};


int POW[SIZE];
int dp[1024*32][SIZE][SIZE],dp2[1024*32];
vector<Edge> G[SIZE];


int main(){

	POW[0] = 1;
	for(int i = 1; i < SIZE; i++){

		POW[i] = POW[i-1]*2;
	}

	int N,M;
	scanf("%d %d",&N,&M);


	int from,to,cost;

	for(int i = 0; i < M; i++){

		scanf("%d %d %d",&from,&to,&cost);
		from--;
		to--;
		G[from].push_back(Edge(to,cost));
	}

	for(int state = 0; state < POW[N]; state++){

		dp2[state] = BIG_NUM;
		for(int s = 0; s < N; s++){
			for(int t = 0; t < N; t++){

				dp[state][s][t] = BIG_NUM;
			}
		}
	}

	for(int s = 0; s < N; s++){
		for(int i = 0; i < G[s].size(); i++){

			dp[0][s][G[s][i].to] = G[s][i].cost; //dp[s,t以外の訪問頂点集合][s][t] := 最小コスト
		}
		for(int state = 0; state < POW[N]; state++){
			for(int node = 0; node < N; node++){
				if(dp[state][s][node] == BIG_NUM)continue;

				for(int k = 0; k < G[node].size(); k++){

					int next_node = G[node][k].to;
					int next_cost = dp[state][s][node]+G[node][k].cost;
					int next_state = state|POW[node];


					dp[next_state][s][next_node] = min(dp[next_state][s][next_node],next_cost);
				}
			}
		}
	}

	dp2[0] = 0;

	for(int i = 0; i < N; i++){

		dp2[POW[i]] = 0;
	}

	for(int state = 0; state < POW[N]; state++){
		if(dp2[state] == BIG_NUM)continue;

		int rest = (POW[N]-1)-state;

		//printf("\nstate:%d dp2[state]:%d\n",state,dp2[state]);

		for(int other = rest; other != 0; other = (other-1) & rest){
			//printf("other:%d\n",other);
			for(int s = 0; s < N; s++){
				if(!(state & POW[s]))continue;
				for(int t = 0; t < N; t++){
					if(!(state & POW[t]))continue;
					if(dp[other][s][t] == BIG_NUM)continue;

					//printf("s:%d t:%d dp[other][s][t]:%d\n",s,t,dp[other][s][t]);

					int next_state = state+other;
					dp2[next_state] = min(dp2[next_state],dp2[state]+dp[other][s][t]);
				}
			}
		}
	}

	printf("%d\n",dp2[POW[N]-1]);

	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 10452
final_rank: 5

code_snippet: #include<iostream>
#include<vector>

using namespace std;

constexpr int inf = 1<<29;

int main(){
    int N, M;
    vector<vector<vector<int>>> dp;
    vector<vector<int>> cost;
    vector<int> dp2;
    
    cin>>N>>M;
    
    dp.resize(N, vector<vector<int>>(N, vector<int>(1<<N, inf)));
    dp2.resize(1<<N, inf);
    cost.resize(N, vector<int>(N, inf));
    
    for(int i = 0; i < M; i++){
       int u, v, c;
       
       cin>>u>>v>>c;
       
       u--, v--;
       
       cost[u][v] = c;
    }
    
    for(int i = 0; i < N; i++){
        cost[i][i] = 0;
    }
    
    for(int l = 0; l < N; l++) { 
        dp[l][l][0] = 0;
        
        for(int i = 0; i < 1<<N; i++){
            for(int j = 0; j < N; j++){
                for(int k = 0; k < N; k++){
                    if((i>>k)&1) continue;
                    dp[l][k][i|(1<<k)] = min(dp[l][k][i|(1<<k)], dp[l][j][i] + cost[j][k]);
                }
            }
        }
    }
    
    for(int i = 0; i < N; i++){
        dp2[1<<i] = 0;
    }
    
    for(int i = 0; i < 1<<N; i++){
        int t = i^((1<<N)-1);
        for(int j = t; j > 0; j = (j - 1) & t){
            for(int k = 0; k < N; k++){
                if(!((i>>k)&1)) continue;
                for(int l = 0; l < N; l++){
                    if(!((i>>l)&1)) continue;
                    dp2[i|j] = min(dp2[i|j], dp2[i] + dp[k][l][j|(1<<k)|(1<<l)]);
                }
            }
        }
    }
    
    cout<<dp2[(1<<N)-1]<<endl;
    
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 8620
final_rank: 6

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define MAX 15

struct Edge{
	Edge(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

int POW[MAX];
int dp[MAX][MAX][1024*32],dp2[1024*32];
vector<Edge> G[MAX];


int main(){

	POW[0] = 1;
	for(int i = 1; i < MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	int N,E;
	scanf("%d %d",&N,&E);

	int from,to,cost;
	for(int loop = 0; loop < E; loop++){

		scanf("%d %d %d",&from,&to,&cost);
		from--;
		to--;
		G[from].push_back(Edge(to,cost));
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k < N; k++){
			for(int state = 0; state < POW[N]; state++){

				dp[i][k][state] = BIG_NUM;
			}
		}
	}


	for(int s = 0; s < N; s++){
		for(int a = 0; a < G[s].size(); a++){

			dp[s][G[s][a].to][0] = G[s][a].cost;
		}
		for(int state = 0; state < POW[N]; state++){
			for(int last = 0; last < N; last++){
				if(dp[s][last][state] == BIG_NUM)continue;

				for(int i = 0; i < G[last].size(); i++){

					int next_node = G[last][i].to;
					int next_state = state|POW[last];

					dp[s][next_node][next_state] = min(dp[s][next_node][next_state],dp[s][last][state]+G[last][i].cost);
				}
			}
		}
	}

	for(int state = 0; state < POW[N]; state++){

		dp2[state] = BIG_NUM;
	}

	dp2[POW[0]] = 0;
	for(int state = 1; state < POW[N]-1; state++){
		if(dp2[state] == BIG_NUM)continue;
		for(int other = 0; other < POW[N]; other++){
			if((state & other) != 0)continue;

			for(int s = 0; s < N; s++){
				if((state & POW[s]) == 0)continue;
				for(int t = 0; t < N; t++){
					if((state & POW[t]) == 0)continue;

					dp2[state+other] = min(dp2[state+other],dp2[state]+dp[s][t][other]);
				}
			}
		}
	}

	printf("%d\n",dp2[POW[N]-1]);

	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 9344
final_rank: 9

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define MAX 15

struct Edge{
	Edge(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

int POW[MAX];
int dp[MAX][MAX][1024*32],dp2[1024*32];
vector<Edge> G[MAX];


int main(){

	POW[0] = 1;
	for(int i = 1; i < MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	int N,E;
	scanf("%d %d",&N,&E);

	int from,to,cost;
	for(int loop = 0; loop < E; loop++){

		scanf("%d %d %d",&from,&to,&cost);
		from--;
		to--;
		G[from].push_back(Edge(to,cost));
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k < N; k++){
			for(int state = 0; state < POW[N]; state++){

				dp[i][k][state] = BIG_NUM;
			}
		}
	}


	for(int s = 0; s < N; s++){
		for(int a = 0; a < G[s].size(); a++){

			dp[s][G[s][a].to][0] = G[s][a].cost;
		}
		for(int state = 0; state < POW[N]; state++){
			for(int last = 0; last < N; last++){
				if(dp[s][last][state] == BIG_NUM)continue;

				for(int i = 0; i < G[last].size(); i++){

					int next_node = G[last][i].to;
					int next_state = state|POW[last];

					dp[s][next_node][next_state] = min(dp[s][next_node][next_state],dp[s][last][state]+G[last][i].cost);
				}
			}
		}
	}

	for(int state = 0; state < POW[N]; state++){

		dp2[state] = BIG_NUM;
	}

	dp2[POW[0]] = 0;
	for(int state = 1; state < POW[N]-1; state++){
		if(dp2[state] == BIG_NUM)continue;
		for(int other = 0; other < POW[N]; other++){
			if((state & POW[other]) != 0)continue;

			for(int s = 0; s < N; s++){
				if((state & POW[s]) == 0)continue;
				for(int t = 0; t < N; t++){
					if((state & POW[t]) == 0)continue;

					dp2[state+other] = min(dp2[state+other],dp2[state]+dp[s][t][other]);
				}
			}
		}
	}

	printf("%d\n",dp2[POW[N]-1]);

	return 0;
}
accuracy: 0.3055555555555556
time_ms: 10
memory_kb: 3484
final_rank: 20

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define MAX 15

struct Edge{
	Edge(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

int N,M;
int POW[MAX];
int dp[MAX][MAX][1024*16],dp2[1024*16];
vector<Edge> G[MAX];


int main(){

	POW[0] = 1;
	for(int i = 1; i < MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d %d",&N,&M);

	int from,to,cost;

	for(int i = 0; i < M; i++){

		scanf("%d %d %d",&from,&to,&cost);
		from--;
		to--;

		G[from].push_back(Edge(to,cost));
	}

	for(int s = 0; s < MAX; s++){
		for(int t = 0; t < MAX; t++){
			for(int state = 0; state < POW[N]; state++){

				dp[s][t][state] = BIG_NUM;
			}
		}
	}

	for(int s = 0; s < N; s++){
		for(int i = 0; i < G[s].size(); i++){

			dp[s][G[s][i].to][0] = G[s][i].cost;
		}
		for(int state = 0; state < POW[N]; state++){
			for(int last = 0; last < N; last++){
				if(dp[s][last][state] == BIG_NUM)continue;

				for(int i = 0; i < G[last].size(); i++){

					int next_node = G[last][i].to;
					int next_cost = G[last][i].cost+dp[s][last][state];
					int next_state = state|POW[last];

					dp[s][next_node][next_state] = min(dp[s][next_node][next_state],next_cost);
				}
			}
		}
	}

	for(int state = 2; state < POW[N]; state++){

		dp2[state] = BIG_NUM;
	}

	dp2[POW[0]] = 0;

	for(int state = 1; state < POW[N]; state++){
		if(dp2[state] == BIG_NUM)continue;
		for(int rest = 1; rest < POW[N]; rest++){
			if((state & rest) > 0)continue;

			for(int a = 0; a < N; a++){
				if((state & POW[a]) == 0)continue;
				for(int b = 0; b < N; b++){
					if((state & POW[b]) == 0)continue;


					dp2[state|rest] = min(dp2[state|rest],dp2[state]+dp[a][b][rest]);
				}
			}
		}
	}

	printf("%d\n",dp2[POW[N]-1]);

	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 11376
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N,M;
vector<pair<int,int> >G[13];
int dist[1<<13][13][13];
int loop[1<<13];
main()
{
	cin>>N>>M;
	for(int i=0;i<M;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		G[a-1].push_back(make_pair(b-1,c));
	}
	for(int i=0;i<1<<N;i++)
	{
		for(int j=0;j<N;j++)for(int k=0;k<N;k++)dist[i][j][k]=1e9;
		loop[i]=1e9;
	}
	for(int i=0;i<N;i++)dist[1<<i][i][i]=0;
	for(int i=1;i<1<<N;i++)for(int j=0;j<N;j++)for(int k=0;k<N;k++)if(dist[i][j][k]<(int)1e9)
	{
		if(j==k&&i>1<<j)loop[i]=min(loop[i],dist[i][j][k]);
		for(pair<int,int>e:G[k])
		{
			dist[i|1<<e.first][j][e.first]=min(dist[i|1<<e.first][j][e.first],dist[i][j][k]+e.second);
		}
	}
	loop[0]=0;
	for(int i=0;i<1<<N;i++)for(int j=0;j<N;j++)for(int k=0;k<N;k++)if(dist[i][j][k]<(int)1e9)
	{
		if(j==k&&i==1<<j)continue;
		int T=(1<<N)-1&~i;
		int t=T;
		do{
			loop[t|i]=min(loop[t|i],loop[t|1<<j|1<<k]+dist[i][j][k]);
			t=t-1&T;
		}while(t!=T);
	}
	cout<<loop[(1<<N)-1]<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 8564
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> G(n);
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--; b--;
        G[a].emplace_back(b, c);
    }
    auto dp = make_v(n, n, 1 << n, INF<int>);
    for (int i = 0; i < n; ++i) {
        dp[i][i][1 << i] = 0;
    }
    for (int S = 0; S < (1 << n); ++S) {
        for (int i = 0; i < n; ++i) {
            if(!(S & (1 << i))) continue;
            for (int j = 0; j < n; ++j) {
                if(dp[i][j][S] == INF<int>) continue;
                for (auto [k, c] : G[j]) {
                    chmin(dp[i][k][S|(1 << k)], dp[i][j][S]+c);
                }
            }
        }
    }
    vector<int> ans(1 << n, INF<int>);
    for (int i = 0; i < n; ++i) ans[1 << i] = 0;
    for (int S = 0; S < (1 << n); ++S) {
        int k = (1 << n)-1-S;
        for (int T = k; T != 0; T = (T-1)&k) {
            for (int i = 0; i < n; ++i) {
                if(!(S & (1 << i))) continue;
                for (int j = 0; j < n; ++j) {
                    if(!(S & (1 << j))) continue;
                    chmin(ans[S|T], ans[S]+dp[i][j][T|(1 << i)|(1 << j)]);
                }
            }
        }
    }
    cout << ans.back() << "\n";
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 8944
final_rank: 3

code_snippet: #include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <cassert>
#include <stack>
#include <climits>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <functional>
#include <cfloat>
#include <numeric>
#include <random>
#include <sstream>
#include <bitset>
using namespace std;
static std::vector<std::vector<std::vector<int>>> path_memo(13, std::vector<std::vector<int>>(13, std::vector<int>(1 << 13, -2)));
struct Road {
	int dest, cost;
};
static std::vector<std::vector<Road>> roads;

int min_cost_path(const int from, const int to, const int pattern) {
	if (from == to && (pattern & (1<<from)) == 0) return pattern == 0 ? 0 : -1;
	if (path_memo[from][to][pattern] >= -1) return path_memo[from][to][pattern];
	int result{ INT_MAX };
	for (const auto next : roads[from]) {
		if (((1 << next.dest) & pattern) != 0) {
			const auto res = min_cost_path(next.dest, to, pattern ^ (1 << next.dest));
			if (res >= 0) {
				result = std::min(result, res + next.cost);
			}
		}
	}
	if (result == INT_MAX) return path_memo[from][to][pattern] = -1;
	else return path_memo[from][to][pattern] = result;
}
int min_cost_cycle(const int pattern) {
	int result{ INT_MAX };
	for (auto i = 0; i < roads.size(); ++i) {
		if ((pattern >> i) & 1) {
			const auto res = min_cost_path(i, i, pattern);
			if (res >= 0) result = std::min(result, res);
		}
	}
	return result == INT_MAX ? -1 : result;
}
static std::vector<int> memo(1 << 13, -2);
int min_cost_strong_connection(const int pattern) {
	if (memo[pattern] >= -1) return memo[pattern];
	int result{ INT_MAX };
	for (auto from = 0; from < roads.size(); ++from) {
		if (((pattern >> from) & 1) == 0) continue;
		for (auto to = 0; to < roads.size(); ++to) {
			if (((pattern >> to) & 1) == 0) continue;
			for (auto add = (pattern + 1) & ~pattern; add < (1 << roads.size()); add = ((add + pattern) + 1) & (~pattern)) {
				const auto path = min_cost_path(from, to, add | (1 << to));
				if (path == -1) continue;
				const auto rest = min_cost_strong_connection(pattern + add);
				if (rest == -1) continue;
				result = std::min(result, path + rest);
			}
		}
	}
	return result == INT_MAX ? memo[pattern] = -1 : memo[pattern] = result;
}
int solve() {
	memo[(1 << roads.size()) - 1] = 0;
	int result{ INT_MAX };
	for (auto outer = 0; outer < (1 << roads.size()); ++outer) {
		const auto cost = min_cost_cycle(outer);
		if (cost == -1) continue;
		const auto all = min_cost_strong_connection(outer);
		if (all == -1) continue;
		result = std::min(result, all + cost);
	}
	return result;
}
int main() {
	int n, m; std::cin >> n >> m;
	roads = std::vector<std::vector<Road>>(n);
	for (auto i = 0; i < m; ++i) {
		int u, v, c; std::cin >> u >> v >> c; --u; --v;
		roads[u].push_back(Road{ v, c });
	}
	std::cout << solve() << std::endl;
}
accuracy: 1
time_ms: 200
memory_kb: 8792
final_rank: 12