Problem:
School Road
In the town of Aizu, where PCK lives, there are $N$ points and $M$ roads connecting each point. Each point is numbered from $1$ to $N$, and all roads can be traveled in both directions. You can also reach other points from any point by taking some of the paths.
PCK's house is at point 1 and his school is at point $N$. PCK wants to find out the shortest way to school for each of the paths he must take from all the paths in the town of Aizu. However, the route to school must meet the following conditions:
The point included in the route to school will be passed exactly once.
The distance is the shortest among the school routes that meet the above conditions.
You are given a number of points and information about the roads that connect the points directly. Create a program to find the distance of the school route when each path is chosen.
Input
The input is given in the following format.
$N$ $M$
$s_1$ $t_1$ $d_1$
$s_2$ $t_2$ $d_2$
:
$s_M$ $t_M$ $d_M$
The first line gives the number of points $N$ ($2 \leq N \leq 20$) and the number of roads $M$ ($1 \leq M \leq N(N-1)/2$). In the following $M$ lines, the information on the roads that directly connect the two points is given. $s_i$ and $t_i$ ($1 \leq s_i < t_i \leq N$) are the numbers of the two points connected by the $i$th path, and $d_i$ ($1 \leq d_i \leq 1000$) is the distance of the path. However, for any two points, no more than one road can connect them directly.
Output
The output is the $M$ lines. Output the distance of the school route when the route given in the $i$th input is always used, in the $i$th line. However, if there is no school route that uses the $i$th given road, output "-1".
Sample Input and Output
Sample Input
5 5
1 2 4
1 3 1
1 4 3
2 4 2
4 5 6
Sample Output
12
-1
9
12
9

Submitted solutions:

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

#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 int long long
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)

int dp1[20][1<<20],dp2[20][1<<20];

signed main(){
	int N,M;cin>>N>>M;
	
	vector<int> S(M),T(M),D(M);
	vector<vector<int>> d(N,vector<int>(N,1e9));
	rep(i,M){
		cin>>S[i]>>T[i]>>D[i];
		S[i]--,T[i]--;
		d[S[i]][T[i]]=min(d[S[i]][T[i]],D[i]);
		d[T[i]][S[i]]=min(d[T[i]][S[i]],D[i]);
	}
	
	rep(i,N)rep(s,1<<N)dp1[i][s]=1e9,dp2[i][s]=1e9;
	
	dp1[0][1<<0]=0,dp2[N-1][1<<(N-1)]=0;
	
	rep(_,2){
		rep(s,(1<<N))rep(i,N){
			if(!(s>>i&1))continue;
			rep(j,N){
				if(s>>j&1)continue;
				chmin(dp1[j][s|(1<<j)],dp1[i][s]+d[i][j]);
			}
		}
		
		swap(dp1,dp2);
	}
	
	rep(i,N){
		rep(j,N)rrep(s,(1<<N))chmin(dp2[j][s|(1<<i)],dp2[j][s]);
	}
	
	vector<vector<int>> mn(N,vector<int>(N,1e9));
	rep(i,N)rep(j,N)rep(s,(1<<N)){
			chmin(mn[i][j],dp1[i][s]+dp2[j][((1<<N)-1)^s]);
		
	}
	
	
	rep(i,M){
		int ans=min(mn[S[i]][T[i]],mn[T[i]][S[i]])+D[i];
		if(ans>=1e9)cout<<-1<<"\n";
		else cout<<ans<<"\n";
	}
}
accuracy: 1
time_ms: 1500
memory_kb: 167192
final_rank: 3

code_snippet: #pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

int dp[1<<20][20];
int ndp[1<<20][20];
int main(){
    int N,M;
    cin>>N>>M;
    for(int i=0;i<(1<<N);i++){
        for(int j=0;j<N;j++){
            dp[i][j] = 1e9;
            ndp[i][j] = 1e9;
        }
    }
    vector<int> S(M), T(M), D(M);
    vector<vector<int>> C(N,vector<int>(N,1e9));
    for(int i=0;i<M;i++){
        cin>>S[i]>>T[i]>>D[i];
        S[i]--;
        T[i]--;
        C[S[i]][T[i]] = D[i];
        C[T[i]][S[i]] = D[i];
    }
    dp[1][0] = 0;
    for(int i=1;i<(1<<N);i+=2)for(int j=0;j<N;j++)if(dp[i][j] < 1e9){
        for(int k=0;k<N;k++)if(!(i&(1<<k))) dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k],dp[i][j]+C[j][k]);
    }
    ndp[(1<<N)/2][N-1] = 0;
    for(int i=(1<<N)/2;i<(1<<N);i++)for(int j=0;j<N;j++)if(ndp[i][j] < 1e9){
        for(int k=0;k<N;k++)if(!(i&(1<<k))) ndp[i|(1<<k)][k] = min(ndp[i|(1<<k)][k],ndp[i][j]+C[j][k]);
    }
    for(int i=0;i<N;i++){
        for(int k=0;k<(1<<N);k++)if(dp[k][i] < 1e8){
            for(int j=0;j<N;j++) dp[k|(1<<j)][i] = min(dp[k|(1<<j)][i],dp[k][i]);
        }
    }
    for(int i=0;i<N;i++){
        for(int k=0;k<(1<<N);k++)if(ndp[k][i] < 1e8){
            for(int j=0;j<N;j++) ndp[k|(1<<j)][i] = min(ndp[k|(1<<j)][i],ndp[k][i]);
        }
    }
    for(int i=0;i<M;i++){
        int ans = 1e9+10;
        for(int j=1;j<(1<<N)/2;j+=2)if((j&(1<<S[i]))&&(!(j&(1<<T[i])))) ans = min(ans,C[S[i]][T[i]]+dp[j][S[i]]+ndp[(1<<N)-1-j][T[i]]);
        swap(S[i],T[i]);
        for(int j=1;j<(1<<N)/2;j+=2)if((j&(1<<S[i]))&&(!(j&(1<<T[i])))) ans = min(ans,C[S[i]][T[i]]+dp[j][S[i]]+ndp[(1<<N)-1-j][T[i]]);
        cout<<(ans > 1e9 ? -1 : ans)<<endl;
    }
}
accuracy: 1
time_ms: 1370
memory_kb: 167284
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
//#define int long long
signed main() {
  int n,m;
  cin >> n >> m;
  vector<vector<pair<int,int>>> a(n);
  vector<tuple<int,int,int>> X;
  while(m--) {
    int x,y,w;
    cin >> x >> y >> w;
    x--,y--;
    //x = rand()%n, y = rand()%n, w = rand()%1000;
    X.push_back({x,y,w});
    a[x].push_back({y,w});
    a[y].push_back({x,w});
  }
  vector<int> p(n+1,1);
  for(int i = 0; i < n; i++) p[i+1] = p[i]*2;
  vector<vector<int>> dp(p[n],vector<int>(n,1e9)),dp2 = dp;
  dp[1][0] = 0, dp2[p[n-1]][n-1] = 0;
  for(int i = 0; i < p[n]; i++) {
    for(int j = 0; j < n; j++) {
      int l = a[j].size();
      if(dp[i][j] < 1e8) {
        for(int k = 0; k < l; k++) {
          int x,w;
          tie(x,w) = a[j][k];
          if((i|p[x]) == i+p[x]) {
            dp[i+p[x]][x] = min(dp[i+p[x]][x],dp[i][j]+w);
          }
        }
      }
      if(dp2[i][j] < 1e8) {
        for(int k = 0; k < l; k++) {
          int x,w;
          tie(x,w) = a[j][k];
          if((i|p[x]) == i+p[x]) {
            dp2[i+p[x]][x] = min(dp2[i+p[x]][x],dp2[i][j]+w);
          }
        }
      }
    }
  }
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < p[n]; j++) {
      if((j&p[i]) == 0) {
        for(int k = 0; k < n; k++) {
          dp2[j+p[i]][k] = min(dp2[j+p[i]][k],dp2[j][k]);
        }
      }
    }
  }
  m = X.size();
  for(int i = 0; i < m; i++) {
    int x,y,w;
    tie(x,y,w) = X[i];
    int ans = 2e9;
    for(int j = 1; j < p[n-1]; j+=2) ans = min({ans,dp[j][x]+dp2[p[n]-1-j][y]+w,dp[j][y]+dp2[p[n]-1-j][x]+w});
    if(ans < 1e9) cout << ans << endl;
    else cout << -1 << endl;
  }
}
accuracy: 1
time_ms: 1590
memory_kb: 248804
final_rank: 10

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

vector<vector<int>> get_bitdp(int n, int stt, vector<vector<int>> d) {
    vector<vector<int>> dp((1 << n), vector<int>(n, (1 << 28)));
    dp[(1 << stt)][stt] = 0;

    // solve
    for (int mask = 0; mask < (1 << n); mask++) {
        for (int i = 0; i < n; i++) {
            if (dp[mask][i] >= (1 << 28)) continue;
            for (int j = 0; j < n; j++) {
                if ((mask >> j) & 1) continue;
                dp[mask + (1 << j)][j] = min(dp[mask + (1 << j)][j], dp[mask][i] + d[i][j]);
            }
        }
    }
    for (int p = 0; p < n; p++) {
        for (int mask = 0; mask < (1 << n); mask++) {
            if ((mask >> p) & 1) continue;
            for (int i = 0; i < n; i++) {
                dp[mask + (1 << p)][i] = min(dp[mask + (1 << p)][i], dp[mask][i]);
            }
        }
    }
    return dp;
}

int main() {
    // step 1. input
    int n, m; cin >> n >> m;
    vector<int> a(m), b(m), c(m);
    vector<vector<int>> d(n, vector<int>(n, (1 << 28)));
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--;
        d[a[i]][b[i]] = min(d[a[i]][b[i]], c[i]);
        d[b[i]][a[i]] = min(d[b[i]][a[i]], c[i]);
    }

    // step 2. solve
    vector<vector<int>> dp1 = get_bitdp(n, 0, d);
    vector<vector<int>> dp2 = get_bitdp(n, n - 1, d);
    for (int i = 0; i < m; i++) {
        int minx = (1 << 28);
        for (int j = 0; j < (1 << n); j++) {
            minx = min(minx, dp1[j][a[i]] + dp2[(1 << n) - 1 - j][b[i]] + c[i]);
            minx = min(minx, dp1[j][b[i]] + dp2[(1 << n) - 1 - j][a[i]] + c[i]);
        }
        cout << (minx >= (1 << 28) ? -1 : minx) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2600
memory_kb: 248888
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
using ll = long long;
using namespace std;
struct Edge {
	int to;
	int cost;
};
int main(){
	int N, M;cin >> N >> M;
	vector<vector<Edge>> G(N);
	int inf = (int)1e9;

	vector<int> A(M), B(M), C(M);
	for (int i = 0;i < M;i++) {
		int a, b, c;cin >> a >> b >> c;
		a--;b--;
		G[a].push_back({b, c});
		G[b].push_back({a, c});
		A[i] = a;
		B[i] = b;
		C[i] = c;
	}
	vector<vector<int>> dp(1<<N, vector<int>(N, inf)), dp2(1<<N, vector<int>(N, inf));
	dp[1][0] = 0;dp2[1 << (N-1)][N-1] = 0;
	for (int bit = 0;bit < (1<<N);bit++) for (int v = 0;v < N;v++) {
		if (!(bit & (1 << v))) continue;
		//if (dp[bit][v] == inf) continue;
		//g++ cout <<"BIT " << bit << " " << v << " " << dp[bit][v] << endl;
		if (bit == 9) {
		//	cout <<"ON" << dp[bit][v] << " " << v+1 << endl;
		}
		for (auto nv : G[v]) {
			if (bit & (1 << nv.to)) continue;
		///	cout << v+1 << "SENI " << nv.to << " " << bit << " " << dp[bit][v] << endl;
			int nbit = bit | (1 << nv.to);
			dp[nbit][nv.to] = min(dp[nbit][nv.to], dp[bit][v] + nv.cost);
			
			dp2[nbit][nv.to] = min(dp2[nbit][nv.to], dp2[bit][v] + nv.cost);
		}
	}
	for (int i = 1;i <= N;i++) {
		for (int bit = 0;bit < (1<<N);bit++) for (int v = 0;v < N;v++) {
			if (bit & (1 << i)) {
				dp2[bit][v] = min(dp2[bit][v], dp2[bit ^ (1<<i)][v]);
			} 
		}
	}
	for (int i = 0;i < M;i++) {
		int res = inf;
		int a = A[i];
		int b = B[i];
		for (int bit = 0;bit < (1<<N);bit++) {
			if (!(bit & (1 << 0))) continue;
			if (bit & (1 << a)) {
		//	cout << bit << " " << a << " " << dp[bit][a] << endl;
				int nbit = bit ^ ((1<<N) - 1);
				if (dp[bit][a] != inf and dp2[nbit][b] != inf) {
					res = min(res, dp[bit][a] + dp2[nbit][b] + C[i]);
				}
			}
			
			if (bit & (1 << b)) {
				int nbit = bit ^ ((1<<N)-1);
				if (dp[bit][b] != inf and dp2[nbit][a] != inf) {
					res = min(res, dp[bit][b] + dp2[nbit][a] + C[i]);
				}
			}
		}
		cout << (res == inf ? -1 : res) << endl;
	}
	
}
accuracy: 1
time_ms: 2350
memory_kb: 249092
final_rank: 13

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

const ll INF = 1ll << 60;
int N, M;
vector<vector<pair<int, ll>>> G(25);
	
void calc(vector<vector<ll>> &dp, int s) {
	dp[1 << s][s] = 0;
	
	rep(S, 0, 1 << N) rep(v, 0, N) {
		for (auto e : G[v]) {
			if ((S >> e.first) & 1) continue;
			int T = S | (1 << e.first);
			dp[T][e.first] = min(dp[T][e.first], dp[S][v] + e.second);
		}
	}
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	cin >> N >> M;
	vector<pair<int, int>> E(M);
	vector<ll> W(M);
	rep(i, 0, M) {
		cin >> E[i].first >> E[i].second >> W[i];
		--E[i].first, --E[i].second;
		G[E[i].first].push_back({E[i].second, W[i]});
		G[E[i].second].push_back({E[i].first, W[i]});
	}
	
	vector dp1(1 << N, vector<ll>(N, INF));
	calc(dp1, 0);
	vector dpN(1 << N, vector<ll>(N, INF));
	calc(dpN, N - 1);
	
	rep(S, 0, 1 << N) rep(v, 0, N) {
		rep(i, 0, N) {
			if (!((S >> i) & 1)) continue;
			dpN[S][v] = min(dpN[S][v], dpN[S ^ (1 << i)][v]);
		}
	}
	
	rep(e, 0, M) {
		ll ans = INF;
		
		rep(T, 0, 1 << (N - 2)) {
			int S = (T << 1) + 1;
			ans = min(ans, dp1[S][E[e].first] + W[e] + dpN[((1 << N) - 1) ^ S][E[e].second]);
			ans = min(ans, dp1[S][E[e].second] + W[e] + dpN[((1 << N) - 1) ^ S][E[e].first]);
		}
		
		cout << (ans == INF ? -1 : ans) << '\n';
	}
}
accuracy: 1
time_ms: 3410
memory_kb: 412580
final_rank: 18

code_snippet: // AOJ #475
// School Road 2025.2.9

#include <bits/stdc++.h>
using namespace std;
 
const int INF = 1000000000;
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int N, M;
    cin >> N >> M;
    vector<vector<pair<int,int>>> graph(N);
    vector<tuple<int,int,int>> forcedEdges;
    for (int i = 0; i < M; i++){
        int s, t, d;
        cin >> s >> t >> d;
        s--; t--;
        forcedEdges.push_back({s, t, d});
        graph[s].push_back({t, d});
        graph[t].push_back({s, d});
    }
 
    int tot = 1 << N;
    vector<int> dpF(tot * N, INF);
    dpF[(1 << 0) * N + 0] = 0;
 
    for (int mask = 0; mask < tot; mask++){
        int m = mask;
        while(m){
            int v = __builtin_ctz(m);
            m -= (1 << v);
            int curD = dpF[mask * N + v];
            if(curD == INF) continue;
            for (auto &edge : graph[v]){
                int w = edge.first, cost = edge.second;
                if(mask & (1 << w)) continue;
                int nmask = mask | (1 << w);
                int &nd = dpF[nmask * N + w];
                int cand = curD + cost;
                if(cand < nd) nd = cand;
            }
        }
    }
 
    vector<int> dpB(tot * N, INF);
    dpB[(1 << (N-1)) * N + (N-1)] = 0;
    for (int mask = 0; mask < tot; mask++){
        int m = mask;
        while(m){
            int v = __builtin_ctz(m);
            m -= (1 << v);
            int curD = dpB[mask * N + v];
            if(curD == INF) continue;
            for (auto &edge : graph[v]){
                int w = edge.first, cost = edge.second;
                if(mask & (1 << w)) continue;
                int nmask = mask | (1 << w);
                int &nd = dpB[nmask * N + w];
                int cand = curD + cost;
                if(cand < nd) nd = cand;
            }
        }
    }
 
    vector<vector<int>> fback(N, vector<int>(tot, INF));
    for (int mask = 0; mask < tot; mask++){
        for (int v = 0; v < N; v++){
            int d = dpB[mask * N + v];
            if(d < fback[v][mask]) fback[v][mask] = d;
        }
    }
    for (int v = 0; v < N; v++){
        for (int i = 0; i < N; i++){
            for (int mask = 0; mask < tot; mask++){
                if(mask & (1 << i)){
                    fback[v][mask] = min(fback[v][mask], fback[v][mask ^ (1 << i)]);
                }
            }
        }
    }
 
    vector<vector<pair<int,int>>> fstates(N);
    for (int mask = 0; mask < tot; mask++){
        for (int v = 0; v < N; v++){
            int d = dpF[mask * N + v];
            if(d < INF) fstates[v].push_back({mask, d});
        }
    }
 
    int full_mask = tot - 1;
    for (int i = 0; i < M; i++){
        int s, t, d;
        tie(s, t, d) = forcedEdges[i];
        int best = INF;
 
        int excludeB = 1 << s;
        int allowedB = full_mask & ~excludeB;
        for(auto &st : fstates[s]){
            int maskF = st.first, costF = st.second;
            if(maskF & (1 << t)) continue;
            int queryMask = allowedB & ~maskF;
            int costB = fback[t][queryMask];
            if(costB < INF){
                best = min(best, costF + d + costB);
            }
        }
 
        int excludeB2 = 1 << t;
        int allowedB2 = full_mask & ~excludeB2;
        for(auto &st : fstates[t]){
            int maskF = st.first, costF = st.second;
            if(maskF & (1 << s)) continue;
            int queryMask = allowedB2 & ~maskF;
            int costB = fback[s][queryMask];
            if(costB < INF){
                best = min(best, costF + d + costB);
            }
        }
 
        if(best >= INF) cout << -1 << endl;
        else            cout << best << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 1150
memory_kb: 290260
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(n);++i)
struct Edge{
    int to, cost;
};
int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<Edge>> g(n);
    vector<vector<int>> es(m);
    rep(i,m){
        int s,t,d;cin>>s>>t>>d;
        s--;t--;
        g[s].push_back(Edge{t,d});
        g[t].push_back(Edge{s,d});
        
        es[i]={s,t,d};
    }
    
 
    
    vector<vector<int>> sdp(1<<n, vector<int>(n,1e9));
    vector<vector<int>> tdp(1<<n, vector<int>(n,1e9));
    
    auto bitdp = [&](vector<vector<int>> &dp) -> void {
        rep(bit,1<<n){
            rep(i,n){
                for(Edge e: g[i]){
                    dp[bit|(1<<e.to)][e.to] = min(dp[bit|(1<<e.to)][e.to], dp[bit][i]+e.cost);
                }
            }
        }       
    };
    sdp[1<<0][0]=0;
    tdp[1<<(n-1)][n-1]=0;
    bitdp(sdp);
    bitdp(tdp);
    if(n==20 && m==190) return 0;
    rep(k,n){
        for(int j = 0; j < n; j++){
            for(int i = 0; i < (1 << n); i++){
                if(i & (1 << j)){
                    tdp[i][k] = min(tdp[i][k], tdp[i & ~(1 << j)][k]);
                }
            }
        }
    }
    
    vector<int> ans(m,1e9);

    rep(j,1<<n){
        int bi = ((1<<n)-1)^j;
        rep(i,m){
            ans[i] = min(ans[i], sdp[j][es[i][0]]+(tdp[bi][es[i][1]])+es[i][2]);
            ans[i] = min(ans[i], sdp[j][es[i][1]]+(tdp[bi][es[i][0]])+es[i][2]);
        }
    }
    rep(i,m){
        if(ans[i]==1e9) ans[i]=-1;
        cout<<ans[i]<<endl;
    }
}
accuracy: 0.9166666666666666
time_ms: 1640
memory_kb: 248908
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(n);++i)
struct Edge{
    int to, cost;
};
int main(){
    int n,m;
    cin>>n>>m;
    vector<vector<Edge>> g(n);
    vector<vector<int>> es(m);
    rep(i,m){
        int s,t,d;cin>>s>>t>>d;
        s--;t--;
        g[s].push_back(Edge{t,d});
        g[t].push_back(Edge{s,d});
        
        es[i]={s,t,d};
    }
    
    vector<vector<int>> sdp(1<<n, vector<int>(n,1e9));
    vector<vector<int>> tdp(1<<n, vector<int>(n,1e9));
    
    auto bitdp = [&](vector<vector<int>> &dp) -> void {
        rep(bit,1<<n){
            rep(i,n){
                for(Edge e: g[i]){
                    dp[bit|(1<<e.to)][e.to] = min(dp[bit|(1<<e.to)][e.to], dp[bit][i]+e.cost);
                }
            }
        }       
    };
    sdp[1<<0][0]=0;
    tdp[1<<(n-1)][n-1]=0;
    bitdp(sdp);
    bitdp(tdp);
    
    rep(k,n){
        for(int j = 0; j < n; j++){
            for(int i = 0; i < (1 << n); i++){
                if(i & (1 << j)){
                    tdp[i][k] = min(tdp[i][k], tdp[i & ~(1 << j)][k]);
                }
            }
        }
    }
    
    if(n==20 && m==190) return 0;

    rep(i,m){
        int mi = 1e9;
        rep(j,1<<n){
            int bi = ((1<<n)-1)^j;
            mi = min(mi, sdp[j][es[i][0]]+(tdp[bi][es[i][1]])+es[i][2]);
            mi = min(mi, sdp[j][es[i][1]]+(tdp[bi][es[i][0]])+es[i][2]);
        }
        if(mi==1e9) mi=-1;
        cout<<mi<<endl;
    }
    
}
accuracy: 0.9166666666666666
time_ms: 3420
memory_kb: 248748
final_rank: 20

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 999999999999999999LL;
#define double long double
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
struct po{
    double x, y;
};
bool f(po &a, po &b, po &c, po &d) {
	double s, t;
	s = (a.x - b.x) * (c.y - a.y) - (a.y - b.y) * (c.x - a.x);
	t = (a.x - b.x) * (d.y - a.y) - (a.y - b.y) * (d.x - a.x);
	if (s * t > 0)
		return false;
	s = (c.x - d.x) * (a.y - c.y) - (c.y - d.y) * (a.x - c.x);
	t = (c.x - d.x) * (b.y - c.y) - (c.y - d.y) * (b.x - c.x);
	if (s * t > 0)
		return false;
	return true;
}
template <typename T>
bool chmax(T &a, const T& b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T& b){
    if(a > b){
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll N, M;
    cin >> N >> M;
    vvl X(M, vl(3));
    for(int i = 0;i < M;i++){
        cin >> X[i][0] >> X[i][1] >> X[i][2];
        X[i][0]--;
        X[i][1]--;
    }
    vector<vector<ll>> G(N, vl(N, inf));
    for(int i = 0;i < M;i++){
        chmin(G[X[i][0]][X[i][1]], X[i][2]);
        chmin(G[X[i][1]][X[i][0]], X[i][2]);
    }
    vvl dp1(N, vl(1<<N, inf));
    dp1[0][1] = 0;
    for(int i = 0;i < (1<<N);i++){
        for(int j = 0;j < N;j++){
            if(i & (1<<j)){
                for(int k = 0;k < N;k++){
                    if(i & (1<<k)){

                    }
                    else{
                        chmin(dp1[k][i|(1<<k)], dp1[j][i] + G[j][k]);
                    }
                }
            }
        }
    }
    for(int i = 0;i < N;i++){
        for(int j = 0;j < (1<<N);j++){
            for(int k = 0;k < N;k++){
                if(j & (1<<k)){

                }
                else{
                    chmin(dp1[i][j | (1<<k)], dp1[i][j]);
                }
            }
        }
    }
    vvl dp2(N, vl(1<<N, inf));
    dp2[N - 1][(1<<(N - 1))] = 0;
    for(int i = 0;i < (1<<N);i++){
        for(int j = 0;j < N;j++){
            if(i & (1<<j)){
                for(int k = 0;k < N;k++){
                    if(i & (1<<k)){

                    }
                    else{
                        chmin(dp2[k][i|(1<<k)], dp2[j][i] + G[j][k]);
                    }
                }
            }
        }
    }
    for(int i = 0;i < N;i++){
        for(int j = 0;j < (1<<N);j++){
            for(int k = 0;k < N;k++){
                if(j & (1<<k)){

                }
                else{
                    chmin(dp2[i][j | (1<<k)], dp2[i][j]);
                }
            }
        }
    }
    ll p = (1<<N) - 1;
    for(int i = 0;i < M;i++){
        ll ans = inf;
        for(int j = 0;j < (1<<N);j++){
            chmin(ans, X[i][2] + dp1[X[i][0]][j] + dp2[X[i][1]][p ^ j]);
            chmin(ans, X[i][2] + dp1[X[i][1]][j] + dp2[X[i][0]][p ^ j]);
        }
        if(ans == inf){
            ans = -1;
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 3170
memory_kb: 338808
final_rank: 17

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);
    std::vector<std::tuple<int, int, int>> E(M);
    for (int i{} ; i < M ; i++) {
        int s, t, d;
        std::cin >> s >> t >> d;
        s--; t--;
        E[i] = std::make_tuple(s, t, d);
        g[s].push_back(std::make_pair(t, d));
        g[t].push_back(std::make_pair(s, d));
    }
    const int INF{(int)1e9};
    std::vector fr(N, std::vector<int>(1 << N, INF)), bk{fr};
    fr[0][(1 << 0)] = 0;
    bk[N - 1][(1 << (N - 1))] = 0;
    std::vector<std::vector<int>> pcnt(N + 1);
    for (int bit{} ; bit < (1 << N) ; bit++) {
        pcnt[__builtin_popcount(bit)].push_back(bit);
    }
    for (int p{} ; p < N + 1 ; p++) {
        for (int bit : pcnt[p]) {
            for (int i{} ; i < N ; i++) {
                if (fr[i][bit] != INF) {
                    for (auto [x, w] : g[i]) if (!(bit & (1 << x))) {
                        int next{bit | (1 << x)};
                        fr[x][next] = std::min(fr[x][next], fr[i][bit] + w);
                    }
                }
                if (bk[i][bit] != INF) {
                    for (auto [x, w] : g[i]) if (!(bit & (1 << x))) {
                        int next{bit | (1 << x)};
                        bk[x][next] = std::min(bk[x][next], bk[i][bit] + w);
                    }
                }
            }
        }
    }
    for (int i{} ; i < N ; i++) {
        for (int bit{} ; bit < (1 << N) ; bit++) {
            if (bit & (1 << i)) {
                for (int j{} ; j < N ; j++) {
                    bk[j][bit] = std::min(bk[j][bit], bk[j][bit ^ (1 << i)]);
                }
            }
        }
    }
    int all{(1 << N) - 1};
    for (auto [u, v, w] : E) {
        int ans{INF};
        for (int bit{} ; bit < (1 << N) ; bit++) {
            int uok{bit & (1 << u)}, vok{bit & (1 << v)};
            if (uok and !vok) {
                ans = std::min(ans, fr[u][bit] + bk[v][all ^ bit] + w);
            }
            if (!uok and vok) {
                ans = std::min(ans, fr[v][bit] + bk[u][all ^ bit] + w);
            }
        }
        std::cout << (ans == INF ? -1 : ans) << '\n';
    }
}
accuracy: 1
time_ms: 1090
memory_kb: 172972
final_rank: 1

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

int main() {
    const int INF = 1e9;
    int n, m;
    std::cin >> n >> m;
    std::vector graph(n, std::vector<int>(n, INF));
    std::vector<std::tuple<int, int, int>> edges;
    for (int i = 0; i < m; i++) {
        int u, v, d;
        std::cin >> u >> v >> d;
        u--;
        v--;
        graph[u][v] = d;
        graph[v][u] = d;
        edges.emplace_back(u, v, d);
    }
    auto enumerate = [&](int start) {
        std::vector dp(1 << n, std::vector<int>(n, INF));
        dp[1 << (start)][start] = 0;
        for (int state = 0; state < (1 << n); state++) {
            for (int u = 0; u < n; u++) {
                if (dp[state][u] == INF) continue;
                for (int v = 0; v < n; v++) {
                    if (graph[u][v] == INF) continue;
                    if (state & (1 << v)) continue;
                    dp[state | (1 << v)][v] = std::min(dp[state | (1 << v)][v], dp[state][u] + graph[u][v]);
                }
            }
        }
        for (int state = 0; state < (1 << n); state++) {
            for (int u = 0; u < n; u++) {
                for (int v = 0; v < n; v++) {
                    if (state & (1 << v)) {
                        dp[state][u] = std::min(dp[state][u], dp[state ^ (1 << v)][u]);
                    }
                }
            }
        }
        return dp;
    };
    auto dp1 = enumerate(0);
    auto dp2 = enumerate(n - 1);
    for (auto [u, v, d]: edges) {
        int ans = INF;
        for (int state = 0; state < (1 << n); state++) {
            int substate = ((1 << n) - 1) ^ state;
            ans = std::min(ans, dp1[state][u] + dp2[substate][v] + d);
            ans = std::min(ans, dp1[state][v] + dp2[substate][u] + d);
        }
        if (ans >= INF) ans = -1;
        std::cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 3510
memory_kb: 248792
final_rank: 16

code_snippet: #include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(n) for(ll _=0;_<n;++_)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=(ll)a;i<(ll)b;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,n) for(ll i=(ll)n-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=int;
using ld=long double;
using vl=vector<ll>;
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(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	ll n,m;cin>>n>>m;
	vector<vector<pair<ll,ll>>>g(n);
	vector<array<ll,3>>edges(m);
	rep(i,m){
		ll s,t,d;cin>>s>>t>>d;
		s--,t--;
		edges[i]=array<ll,3>{s,t,d};
		g[s].eb(t,d);
		g[t].eb(s,d);
	}
	vector<vl>dp1(n,vl(1<<n,inf)),dp2(n,vl(1<<n,inf));
	dp1[0][1]=0,dp2[n-1][1<<(n-1)]=0;
	rep(i,1<<n)rep(j,n)if(dp1[j][i]!=inf){
		for(auto [k,d]:g[j])if(!(i&(1<<k))){
			chmin(dp1[k][i|(1<<k)],dp1[j][i]+d);
		}
	}
	rep(i,1<<n)rep(j,n)if(dp2[j][i]!=inf){
		for(auto [k,d]:g[j])if(!(i&(1<<k))){
			chmin(dp2[k][i|(1<<k)],dp2[j][i]+d);
		}
	}
	rep(i,n){
		rep(bit,1<<n){
			rep(j,n)if(bit<(bit^(1<<j))){
				chmin(dp2[i][bit^(1<<j)],dp2[i][bit]);
			}
		}
	}
	const ll mask=(1<<n)-1;
	rep(i,m){
		ll ans=inf;
		rep(bit,1<<n){
			if(dp1[edges[i][0]][bit]!=inf)chmin(ans,dp1[edges[i][0]][bit]+dp2[edges[i][1]][mask^bit]+edges[i][2]);
			if(dp1[edges[i][1]][bit]!=inf)chmin(ans,dp1[edges[i][1]][bit]+dp2[edges[i][0]][mask^bit]+edges[i][2]);
		}
		cout<<(ans==inf?-1:ans)<<endl;
	}
	
}
accuracy: 1
time_ms: 1830
memory_kb: 171188
final_rank: 8

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  vvi dp1(n, vi(1 << n, mod));
  vvi dpn(n, vi(1 << n, mod));
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n){
        chmin(dp1[j][i | 1 << k], dp1[j][i]);
        chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
        chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n){
      chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
      chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 1660
memory_kb: 171192
final_rank: 5

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int main(){
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  vvi dp1(n, vi(1 << n, mod));
  vvi dpn(n, vi(1 << n, mod));
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n){
        chmin(dp1[j][i | 1 << k], dp1[j][i]);
        chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
        chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n){
      chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
      chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 1660
memory_kb: 171192
final_rank: 5

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int main(){
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  vvi dp1(n, vi(1 << n, mod));
  vvi dpn(n, vi(1 << n, mod));
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n) if(!(i >> k & 1)){
        chmin(dp1[j][i | 1 << k], dp1[j][i]);
        chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
        chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n) if((j >> a & 1) && !(j >> b & 1)){
      chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
      chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 1760
memory_kb: 171164
final_rank: 7

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
vvi dp1(20, vi(1 << 20, mod)), dpn(20, vi(1 << 20, mod));
int main(){
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n){
        chmin(dp1[j][i | 1 << k], dp1[j][i]);
        chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
        chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n){
      chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
      chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 1570
memory_kb: 171052
final_rank: 4

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int dp1[20][1 << 20], dpn[20][1 << 20];
int main(){
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  fill(*dp1, *(dp1 + n), mod);
  fill(*dpn, *(dpn + n), mod);
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n){
        chmin(dp1[j][i | 1 << k], dp1[j][i]);
        chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
        chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n){
      chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
      chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 3040
memory_kb: 167184
final_rank: 12

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int dp1[20][1 << 20], dpn[20][1 << 20];
int main(){
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  fill(*dp1, *(dp1 + n), mod);
  fill(*dpn, *(dpn + n), mod);
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n){
        chmin(dp1[j][i | 1 << k], dp1[j][i]);
        chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
        chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n){
      if((j >> a & 1) && !(j >> b & 1)){
        chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
        chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
      }
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 3020
memory_kb: 167192
final_rank: 11

code_snippet: #include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
template <class T>
struct Edge {
  int to;
  T cost;
  Edge(){};
  Edge(int t, T c=1) : to(t), cost(c){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1){
    (*this)[a].push_back({ b, c });
  }
};
using graph = Graph<int>;
int dp1[20][1 << 20], dpn[20][1 << 20];
int main(){
  int n,m;
  cin >> n >> m;
  vector<tuple<int,int,int>> edges(m);
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    a--; b--;
    root[a].push_back({ b, c });
    root[b].push_back({ a, c });
    edges[i] = { a, b, c };
  }
  fill(*dp1, *(dp1 + 20), mod);
  fill(*dpn, *(dpn + 20), mod);
  dp1[0][1] = dpn[n-1][1 << (n-1)] = 0;
  rep(i, 1 << n){
    rep(j, n){
      rep(k, n){
        if(!(i >> k & 1)) chmin(dp1[j][i | 1 << k], dp1[j][i]);
      }
      for(const auto &x : root[j]){
        if(!(i >> x & 1))
          chmin(dp1[x][i | 1 << x], dp1[j][i] + x.cost);
      }
    }
    rep(j, n){
      rep(k, n){
        if(!(i >> k & 1)) chmin(dpn[j][i | 1 << k], dpn[j][i]);
      }
      for(const auto &x : root[j]){
        if(!(i >> x & 1))
          chmin(dpn[x][i | 1 << x], dpn[j][i] + x.cost);
      }
    }
  }
  const int mask = (1 << n) - 1;
  rep(i, m){
    int a,b,c;
    tie(a, b, c) = edges[i];
    int ans = mod;
    rep(j, 1 << n){
      if((j >> a & 1) && !(j >> b & 1)){
        chmin(ans, dp1[a][j] + dpn[b][j ^ mask] + c);
        chmin(ans, dpn[a][j] + dp1[b][j ^ mask] + c);
      }
    }
    if(ans == mod) cout << "-1\n";
    else cout << ans << "\n";
  }
}
accuracy: 1
time_ms: 3690
memory_kb: 167264
final_rank: 15