Problem:
Chinese Postman Problem
For a given weighted undirected graph
G(V, E)
, find the distance of the shortest route that meets the following criteria:
It is a closed cycle where it ends at the same point it starts.
The route must go through every edge at least once.
Input
|V|
|E|
s
0
t
0
d
0
s
1
t
1
d
1
:
s
|E|-1
t
|E|-1
d
|E|-1
, where
|V|
is the number of vertices and
|E|
is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,...,
|V|
-1 respectively.
s
i
and
t
i
represent source and target verticess of
i
-th edge (undirected) and
d
i
represents the distance between
s
i
and
t
i
(the
i
-th edge).
Note that there can be multiple edges between a pair of vertices.
Output
Print the shortest distance in a line.
Constraints
2 ≤
|V|
≤ 15
0 ≤
|E|
≤ 1,000
0 ≤
d
i
≤ 1,000
s
i
≠
t
i
The graph is connected
Sample Input 1
4 4
0 1 1
0 2 2
1 3 3
2 3 4
Sample Output 1
10
Sample Input 2
4 5
0 1 1
0 2 2
1 3 3
2 3 4
1 2 5
Sample Output 2
18
Sample Input 3
2 3
0 1 1
0 1 2
0 1 3
Sample Output 3
7

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std ;
#define MAX 1000000001
#define NIL -2000000002
#define INF 1e9
#define MAXN 20
typedef long long int ll;
const int debug = 0 ; 
const int debug1 = 0 ; 
#define REP(i,n) for(int i=0; i<(int)n; i++)

int WF[1<<MAXN][MAXN] ; // DP[Sの部分集合][最後に訪問する頂点]
int G[MAXN][MAXN];
int edge[MAXN];

int V, E;
int ans;
int S;
vector<int> oddlist ; 
int M;

void init() {
    REP (i,1<<MAXN) REP (j,MAXN) { WF[i][j] = INF; }
    REP (i,MAXN) REP (j,MAXN) {G[i][j]=INF;}
    // G[0][0]=0;
    REP (i,MAXN) WF[i][i] = 0 ;
}


int calc_cost(){
    // DPに各点間の距離
    for (int k=0; k< V ; k++) { 
        for (int i=0 ; i<V ; i++) {
            for (int j=0; j<V; j++) { 
                if (WF[i][k] ==INF || WF[k][j] ==INF ) continue;
                WF[i][j] = min (WF[i][j] , WF[i][k] + WF[k][j] ) ; 
            }
        }
    } 
    return 0;
}   

int main(){
    cin >> V >> E ;
    //頂点の番号は 0からV-1; 
    int tmp ,tmp2 , tmp3; 
    int edgetotal=0;
    init();
    for (int i=1 ; i<= E ; i++){
        cin >> tmp >> tmp2 >> tmp3 ;
        // VS[tmp].edge.push_back(make_pair(tmp3, tmp2));
        G[tmp][tmp2]=tmp3;
        G[tmp2][tmp]=tmp3;
        if( tmp3 < WF[tmp][tmp2] ) {
            WF[tmp][tmp2]=tmp3;
            WF[tmp2][tmp]=tmp3; }
        edge[tmp]++;  edge[tmp2]++;
        edgetotal += tmp3 ;
    }
    calc_cost(); // WF法
    REP(i,MAXN){
        if (edge[i]%2 != 0) { oddlist.push_back(i); }
    }
    M = oddlist.size();
    const int siz = 1<<M ;
    int combi_min[siz] ;
    REP (i, (1<<M)) { combi_min[i] = INF;}
    combi_min[0] = 0;
    REP (k,(1<<M)) {
        REP (i, M) {
            REP(j, M) {
                if ( ! (k & (1<<i)) || !(k &(1<<j) ))   { //  
                    //printf ("here k=%d i=%d j=%d",k,i,j);  
                    continue;}
                if (i <= j) {continue;}
                int k_ = k ^ ( (1<<i) | (1<<j) );
                combi_min[k] = min(combi_min[k], combi_min[k_]+WF[oddlist.at(i)][oddlist.at(j)] );
                //printf("combi_min[k] = %d , min(combi_min[%d], combi_min[%d]=%d+WF[%d][%d]=%d ) \n", combi_min[k], k, k_, combi_min[k_],\
                    oddlist.at(i),oddlist.at(j),WF[oddlist.at(i)][oddlist.at(j)] );
            }
        }
    }
    ans = ( combi_min[(1<<M)-1] >= INF? INF :combi_min[(1<<M)-1] + edgetotal );

    if (ans == INF) {ans = -1;}
    printf("%d\n",ans ) ;
    
    /*REP (i, V) {
        cout << i << " : " ;
        REP (j, V) { cout << WF[i][j]<< " "; }
        cout << endl; }*/
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 85488
final_rank: 6

code_snippet: // #include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <deque>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <numeric>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VS = vector<string>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using VVD = vector<VD>;
using VVVI = vector<VVI>;
using VVVL = vector<VVL>;
using VVVD = vector<VVD>;
using PII = std::pair<int, int>;
using VPII = std::vector<std::pair<int, int>>;
using PLL = std::pair<ll, ll>;
using VPLL = std::vector<std::pair<ll, ll>>;
using TI3 = std::tuple<int, int, int>;
using TI4 = std::tuple<int, int, int, int>;
using TL3 = std::tuple<ll, ll, ll>;
using TL4 = std::tuple<ll, ll, ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repr2(i, g, n) for (int i = (int)(n)-1; i >= (g); i--)
#define rep3(i, s, n, d) for (int i = (s); i < (int)(n); i += (d))
#define repr3(i, g, n, d) for (int i = (int)(n)-1; i >= (g); i -= (d))
#define allpt(v) (v).begin(), (v).end()
#define allpt_c(v) (v).cbegin(), (v).cend()
#define allpt_r(v) (v).rbegin(), (v).rend()
#define allpt_cr(v) (v).crbegin(), (v).crend()

const int mod1 = 1e9 + 7, mod2 = 998244353, mod3 = 1e9 + 9;
const int mod = mod1;
const ll inf = 1e18;
const int infint = 1e9;

const string wsp = " ";
const string tb = "\t";
const string rt = "\n";
const string alphabets = "abcdefghijklmnopqrstuvwxyz";

template <typename T>
void show1dvec(const vector<T> &v)
{
    if (v.size() == 0)
        return;
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << wsp;
    cout << v[n] << rt;
    return;
}

int get1dcoodinate(int w, int i, int j) { return i * w + j; }

template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &v)
{
    is >> v.first >> v.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    if (v.size() == 0)
        return os;
    int n = (int)v.size() - 1;
    rep(i, n) os << v[i] << wsp;
    os << v[n] << rt;
    return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    if (v.size() == 0)
        return is;
    int n = v.size();
    rep(i, n) is >> v[i];
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v)
{
    if (v.size() == 0)
        return os;
    int n = (int)v.size();
    rep(i, n) os << v[i];
    return os;
}

template <typename T>
istream &operator>>(istream &is, vector<vector<T>> &v)
{
    if (v.size() == 0)
        return is;
    int n = v.size();
    rep(i, n) is >> v[i];
    return is;
}

template <typename T, typename S>
istream &operator>>(istream &is, vector<pair<T, S>> &v)
{
    if (v.size() == 0)
        return is;
    int n = v.size();
    rep(i, n) is >> v[i].first >> v[i].second;
    return is;
}

template <typename T, typename S>
ostream &operator<<(ostream &os, const vector<pair<T, S>> &v)
{
    if (v.size() == 0)
        return os;
    int n = v.size();
    rep(i, n) os << v[i].first << wsp << v[i].second << rt;
    return os;
}

void show2dvec(const vector<string> &v)
{
    int n = (int)v.size();
    rep(i, n) cout << v[i] << rt;
}

template <typename T>
void show2dvec(const vector<vector<T>> &v)
{
    int n = v.size();
    rep(i, n) show1dvec(v[i]);
}

template <typename T>
void range_sort(vector<T> &arr, int l, int r)
{
    sort(arr.begin() + l, arr.begin() + r);
}

template <typename T, typename S>
void show1dpair(const vector<pair<T, S>> &v)
{
    int n = v.size();
    rep(i, n) cout << v[i].first << v[i].second << rt;
    return;
}

template <typename T, typename S>
void pairzip(const vector<pair<T, S>> &v, vector<T> &t, vector<T> &s)
{
    int n = v.size();
    rep(i, n)
    {
        t.push_back(v[i].first);
        s.push_back(v[i].second);
    }
    return;
}

template <typename T>
void maxvec(vector<T> &v)
{
    T s = v[0];
    int n = v.size();
    rep(i, n - 1)
    {
        if (s > v[i + 1])
        {
            v[i + 1] = s;
        }
        s = v[i + 1];
    }
}

template <typename T, typename S>
bool myfind(T t, S s)
{
    return find(t.cbegin(), t.cend(), s) != t.cend();
}

bool check(int y, int x, int h, int w)
{
    return 0 <= y && y < h && 0 <= x && x < w;
}

bool check(int y, int x, int h1, int h2, int w1, int w2)
{
    return h1 <= y && y < h2 && w1 <= x && x < w2;
}

bool iskadomatsu(int a, int b, int c)
{
    return (a != b && b != c && c != a) &&
           ((a > b && b < c) || (a < b && b > c));
}

template <typename T>
bool iskadomatsu(vector<T> v)
{
    T a = v[0], b = v[1], c = v[2];
    return (a != b && b != c && c != a) &&
           ((a > b && b < c) || (a < b && b > c));
}

double euc_dist(PII a, PII b)
{
    return sqrt(pow(a.first - b.first, 2) + pow(a.second - b.second, 2));
}

// VS split(string s, char c) {
//     VS ret;
//     string part;
//     s += c;
//     rep(i, s.length()) {
//         if (s[i] == c) {
//             if (part != "") ret.emplace_back(part);
//             part = "";
//         } else if (s[i] != c) {
//             part += s[i];
//         }
//     }
//     return ret;
// }

VS split(string s, char c, char d = ' ')
{
    VS ret;
    // reverse(allpt(s));
    string part;
    s += c;
    rep(i, s.length())
    {
        if (s[i] == c)
        {
            if (part != "")
            {
                // string t;
                // t += c;
                // ret.emplace_back(t);
                ret.emplace_back(part);
            }
            part = "";
        }
        else
        {
            part += s[i];
        }
    }
    return ret;
}

template <typename T, typename S, typename R>
ll pow_mod(T p, S q, R _mod = 1ll)
{
    ll ret = 1, r = p;
    while (q)
    {
        if (q % 2)
            ret *= r, ret %= _mod;
        r = (r * r) % _mod, q /= 2;
    }
    return ret % _mod;
}

template <typename T, typename S>
ll pow_no_mod(T p, S q)
{
    ll ret = 1, r = p;
    while (q)
    {
        if (q % 2)
            ret *= r;
        r = (r * r), q /= 2;
    }
    return ret;
}

void make_frac_tables(VL &frac_list, VL &frac_inv_list)
{
    rep(i, frac_list.size() - 1)
    {
        frac_list[i + 1] *= frac_list[i] * (i + 1);
        frac_list[i + 1] %= mod;
        frac_inv_list[i + 1] *= frac_inv_list[i] * pow_mod(i + 1, mod - 2, mod);
        frac_inv_list[i + 1] %= mod;
    }
}

pair<VL, VL> make_frac_tables(int n)
{
    VL frac_list(n + 1, 1), frac_inv_list(n + 1, 1);
    rep(i, n)
    {
        frac_list[i + 1] *= frac_list[i] * (i + 1);
        frac_list[i + 1] %= mod;
        frac_inv_list[i + 1] *= frac_inv_list[i] * pow_mod(i + 1, mod - 2, mod);
        frac_inv_list[i + 1] %= mod;
    }
    return make_pair(frac_list, frac_inv_list);
}

ll perm(int a, int b, const VL &frac_list, const VL &frac_inv_list)
{
    if (a < b)
        return 0;
    if (b < 0)
        return 0;
    ll ret = frac_list[a];
    ret *= frac_inv_list[a - b];
    ret %= mod;
    return ret;
}

ll comb(int a, int b, const VL &frac_list, const VL &frac_inv_list)
{
    if (a < b)
        return 0;
    if (b < 0)
        return 0;
    ll ret = frac_list[a];
    ret *= frac_inv_list[b];
    ret %= mod;
    ret *= frac_inv_list[a - b];
    ret %= mod;
    return ret;
}

struct vec2d
{
    ll x;
    ll y;
    vec2d(ll _x, ll _y)
    {
        x = _x;
        y = _y;
    }
    ll dot(vec2d p) { return x * p.x + y * p.y; }
    vec2d diff(vec2d p) { return vec2d(x - p.x, y - p.y); }
};

struct node
{
    int parent = -1;
    ll weight = 0;
    int depth = 0;
    int degree = -1;
    int subtree = 1;
    int check = 0;
    int scc = -1;
    VPLL children;
    VI parent_list;
    VPLL connect;
    ll visitfee;
    vector<TL3> max_cost_from_children;
    ll max_cost_from_parent;

    node()
    {
        parent = -1;
        weight = 0;
        depth = 0;
        degree = -1;
        subtree = 1;
        check = 0;
        children;
        parent_list;
        connect;
        max_cost_from_parent = 0;
    }
};

struct graph
{
    int _n;
    int root = 0;
    vector<node> nodes;
    graph(int n)
    {
        _n = n;
        rep(i, _n) nodes.emplace_back(node());
    }
    void getconnect_nocost()
    {
        ll a, b;
        cin >> a >> b;
        nodes[a].connect.emplace_back(b, 1);
        nodes[b].connect.emplace_back(a, 1);
    }
    void getconnect_cost()
    {
        ll a, b, c;
        cin >> a >> b >> c;
        nodes[a].connect.emplace_back(b, c);
        nodes[b].connect.emplace_back(a, c);
    }
    void getconnect_increment_nocost()
    {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        nodes[a].connect.emplace_back(b, 1);
        nodes[b].connect.emplace_back(a, 1);
    }
    void getconnect_increment_cost()
    {
        ll a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        nodes[a].connect.emplace_back(b, c);
        nodes[b].connect.emplace_back(a, c);
    }
    void showparent()
    {
        rep(i, _n - 1) cout << nodes[i].parent << wsp;
        cout << nodes[_n - 1].parent << rt;
    }
    void showweight()
    {
        rep(i, _n - 1) cout << nodes[i].weight << wsp;
        cout << nodes[_n - 1].weight << rt;
    }
    void showsubtree()
    {
        rep(i, _n - 1) cout << nodes[i].subtree << wsp;
        cout << nodes[_n - 1].subtree << rt;
    }
    void showdepth()
    {
        rep(i, _n - 1) cout << nodes[i].depth << wsp;
        cout << nodes[_n - 1].depth << rt;
    }
};

struct point
{
    int x;
    int y;
    point()
    {
        x = 0;
        y = 0;
    }
    point(int _x, int _y)
    {
        x = _x;
        y = _y;
    }
    void pointinput()
    {
        int _x, _y;
        cin >> _x >> _y;
        x = _x;
        y = _y;
    }
    void pointinv() { swap(x, y); }
};

istream &operator>>(istream &is, point &p)
{
    is >> p.x >> p.y;
    return is;
}

ostream &operator<<(ostream &os, point &p)
{
    os << p.x << wsp << p.y << rt;
    return os;
}

double pointseuc(point a, point b)
{
    ll ax = a.x, bx = b.x, ay = a.y, by = b.y;
    return sqrt(pow(ax - bx, 2) + pow(ay - by, 2));
}

ll pointseucsquare(point a, point b)
{
    ll ax = a.x, bx = b.x, ay = a.y, by = b.y;
    return (ax - bx) * (ax - bx) + (ay - by) * (ay - by);
}

int pointsmanhattan(point a, point b)
{
    return abs(a.x - b.x) + abs(a.y - b.y);
}

double dist_segment_point(TL3 segment, point p)
{
    double a = get<0>(segment);
    double b = get<1>(segment);
    double c = get<2>(segment);
    return abs(a * p.x + b * p.y - c) / sqrt(a * a + b * b);
}

TL3 segment_parameter(point p, point q)
{
    ll a, b, c;
    a = q.y - p.y;
    b = p.x - q.x;
    c = a * p.x + b * p.y;
    TL3 ret = (TL3){a, b, c};
    // cout << a << b << c << rt;
    return ret;
}

int cross_check(TL3 segment, point p)
{
    ll a = get<0>(segment);
    ll b = get<1>(segment);
    ll c = get<2>(segment);

    auto f = a * p.x + b * p.y - c;
    int ret;
    if (f > 0)
        ret = 1;
    if (f == 0)
        ret = 0;
    if (f < 0)
        ret = -1;
    return ret;
}

VI shave(int n)
{
    VI v;
    if (n <= 1)
        return v;
    vector<bool> w(n + 1, true);
    int x;
    w[0] = w[1] = false;
    rep2(i, 2, n + 1)
    {
        if (w[i])
        {
            x = i * 2;
            while (x <= n)
            {
                w[x] = false;
                x += i;
            }
        }
    }
    rep(i, n + 1) if (w[i]) v.emplace_back(i);
    return v;
}

bool iscross(point p1, point p2, point p3, point p4)
{
    auto segment_1 = segment_parameter(p1, p2);
    auto segment_2 = segment_parameter(p3, p4);
    const double eps = 1e-6;
    bool ans{false};
    if (cross_check(segment_1, p3) * cross_check(segment_1, p4) == 0 &&
        cross_check(segment_2, p1) * cross_check(segment_2, p2) == 0)
    {
        auto xmin_1 = min(p1.x, p2.x) - eps;
        auto xmin_2 = min(p3.x, p4.x) - eps;
        auto xmax_1 = max(p1.x, p2.x) + eps;
        auto xmax_2 = max(p3.x, p4.x) + eps;
        auto ymin_1 = min(p1.y, p2.y) - eps;
        auto ymin_2 = min(p3.y, p4.y) - eps;
        auto ymax_1 = max(p1.y, p2.y) + eps;
        auto ymax_2 = max(p3.y, p4.y) + eps;

        if (xmin_1 <= p3.x && p3.x <= xmax_1 && ymin_1 <= p3.y && p3.y <= ymax_1)
            ans = true;
        if (xmin_1 <= p4.x && p4.x <= xmax_1 && ymin_1 <= p4.y && p4.y <= ymax_1)
            ans = true;
        if (xmin_2 <= p1.x && p1.x <= xmax_2 && ymin_2 <= p1.y && p1.y <= ymax_2)
            ans = true;
        if (xmin_2 <= p2.x && p2.x <= xmax_2 && ymin_2 <= p2.y && p2.y <= ymax_2)
            ans = true;
    }
    else if (cross_check(segment_1, p3) * cross_check(segment_1, p4) <= 0 &&
             cross_check(segment_2, p1) * cross_check(segment_2, p2) <= 0)
    {
        ans = true;
    }
    return ans;
}

int on_the_segment(point p, point q1, point q2)
{
    auto [a, b, c] = segment_parameter(q1, q2);

    if (p.x < min(q1.x, q2.x) || max(q1.x, q2.x) < p.x)
        return 0;
    if (p.y < min(q1.y, q2.y) || max(q1.y, q2.y) < p.y)
        return 0;

    auto f = a * p.x + b * p.y - c;
    return f == 0;
}

void shave(vector<int> &v, int n)
{
    if (n <= 1)
        return;
    vector<bool> w(n + 1, true);
    int x;
    w[0] = w[1] = false;
    rep2(i, 2, n + 1)
    {
        if (w[i])
        {
            x = i * 2;
            while (x <= n)
            {
                w[x] = false;
                x += i;
            }
        }
    }
    rep(i, n + 1) if (w[i]) v.emplace_back(i);
}

template <typename T>
void coordinate_compress(vector<T> &x, map<T, int> &zip, int &xs)
{
    sort(x.begin(), x.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    xs = x.size();
    for (int i = 0; i < xs; i++)
    {
        zip[x[i]] = i;
    }
}

// ll getpalindrome(int l, int r, const string &s, VL &memo) {
//     if (r - l <= 1) return 1;
//     if (memo[l] != -1) return memo[l];
//     ll ret{1};
//     int n = (r - l) / 2;
//     string sl, sr;
//     rep(i, n) {
//         sl += s[l + i];
//         sr += s[r - 1 - i];
//         string rsr(i + 1, '0');
//         reverse_copy(allpt_c(sr), rsr.begin());
//         // cout << sl << wsp << sr << wsp << rsr << rt;
//         if (sl == rsr) {
//             ret += getpalindrome(l + i + 1, r - i - 1, s, memo);
//             ret %= mod;
//         }
//     }
//     memo[l] = ret;
//     return ret;
// }

void getpalindrome(int l, int r, int n, const string &s, VVB &memo)
{
    if (r - l <= 1 || s[l] == s[r - 1])
    {
        memo[l][r] = true;
        if (l > 0 && r < n)
        {
            getpalindrome(l - 1, r + 1, n, s, memo);
        }
    }
    return;
}

// void djcstra(graph graphs, int s, int t)
// {
//     const int n = graphs._n;
//     VL shortest(n, inf);
//     shortest[s] = 0;
//     priority_queue<pair<ll, int>> pq;
//     pq.push(make_pair(0, s));
//     while (!pq.empty())
//     {
//         auto [t, v] = pq.top();
//         pq.pop();
//         t = -t;
//         if (shortest[v] != t)
//             continue;
//         for (auto [u, c] : graphs.nodes[v].connect)
//         {
//             if (t + c < shortest[u])
//             {
//                 shortest[u] = t + c;
//                 pq.push(make_pair(-(t + c), u));
//             }
//         }
//     }
// }

class Unionfind
{
    vector<int> p;

public:
    Unionfind(int n)
    {
        for (int i = 0; i < n; i++)
        {
            p.push_back(i);
        }
    }
    int find(int x)
    {
        while (p[x] != x)
        {
            p[x] = p[p[x]];
            x = p[x];
        }
        return x;
    }
    int getval(int x) { return p[x]; }
    void unite(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            p[x] = y;
        }
    }

    bool isunion(int x, int y) { return find(x) == find(y); }
    void showtree() { cout << p; }
};

template <typename T>
class RangeMinorMaxorSumQuery // 0-index
{
    T const intmax = infint;
    T const intmin = 0;
    vector<T> sgt;
    int n;
    int k;

public:
    RangeMinorMaxorSumQuery(int n1, T f)
    {
        int na = 1;
        int ka = 0;
        while (na < n1)
        {
            na *= 2;
            ka++;
        }
        for (int i = 0; i < 2 * na; i++)
            sgt.push_back(f);
        n = na;
        k = ka;
    }

    void update_min(int i, T x)
    {
        i += n;
        sgt[i] = x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = min(sgt[2 * i], sgt[2 * i + 1]);
        }
    }
    void update_max(int i, T x)
    {
        i += n;
        sgt[i] = x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = max(sgt[2 * i], sgt[2 * i + 1]);
        }
    }
    void update_sum(int i, T x)
    {
        i += n;
        sgt[i] = x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = sgt[2 * i] + sgt[2 * i + 1];
        }
    }

    void add_sum(int i, T x)
    {
        i += n;
        sgt[i] += x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = sgt[2 * i] + sgt[2 * i + 1];
        }
    }

    void add_min(int i, T x)
    {
        i += n;
        sgt[i] += x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = min(sgt[2 * i], sgt[2 * i + 1]);
        }
    }

    void add_max(int i, T x)
    {
        i += n;
        sgt[i] += x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = max(sgt[2 * i], sgt[2 * i + 1]);
        }
    }

    T get_min(int a, int b, int k = 1, int l = 0,
              int r = -1) //閉区間 l <= x < r とする
    {
        if (r == -1)
            r = n;
        if (r <= a || b <= l)
            return 0;
        if (a == l && b == r)
            return sgt[k];
        else
            return min(
                get_min(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2),
                get_min(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r));
    }
    T get_max(int a, int b, int k = 1, int l = 0,
              int r = -1) //閉区間 l <= x < r とする
    {
        if (r == -1)
            r = n;
        if (r <= a || b <= l)
            return 0;
        if (a == l && b == r)
            return sgt[k];
        else
            return max(
                get_max(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2),
                get_max(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r));
    }
    T get_sum(int a, int b, int k = 1, int l = 0,
              int r = -1) //閉区間 l <= x < r とする
    {
        if (r == -1)
            r = n;
        if (r <= a || b <= l)
            return intmin;
        if (a == l && b == r)
            return sgt[k];
        else
            return get_sum(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2) +
                   get_sum(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r);
    }

    T operator[](int i) { return sgt[i + n]; }

    void printsegtree()
    {
        for (int i = 0; i < 2 * n; i++)
        {
            cout << sgt[i] << " ";
        }
        cout << endl;
    }
};

template <typename T>
class Bit_Index_Tree // 1-index

{
    int n = 0;
    vector<T> v;

public:
    Bit_Index_Tree(int _n, T x = 0)
    {
        n = _n;
        v.resize(n + 1);
        fill(allpt(v), x);
    }
    Bit_Index_Tree(vector<T> _v)
    {
        n = _v.size();
        v = _v;
    }
    void add(int i, T x)
    {
        while (i <= n)
        {
            v[i] += x;
            i += i & -i;
        }
    }
    T get_sum(int i)
    {
        T ret = 0;
        while (i > 0)
        {
            ret += v[i];
            i -= i & -i;
        }
        return ret;
    }
    T get_range_sum(int l, int r)
    { // 半開区間　l <= x < r
        return get_sum(r) - get_sum(l);
    }
};

// #define DEBUG

VI smallest_prime_factors(int n)
{
    VI ret(n + 1);
    iota(allpt(ret), 0);
    for (int i = 2; i * i <= n; i++)
    {
        if (ret[i] == i)
        {
            for (int j = i * i; j <= n; j += i)
            {
                if (ret[j] == j)
                    ret[j] = i;
            }
        }
    }
    return ret;
}

template <typename T>
T mex(vector<T> v)
{
    sort(allpt(v));
    v.erase(unique(v.begin(), v.end()), v.end());
    int ret = 0;
    for (auto x : v)
    {
        if (ret < x)
            return ret;
        ret++;
    }
    return ret;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    // リアクティブ問題のときはコメントアウト

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    // ifstream in("input.txt");                             // for debug
    // cin.rdbuf(in.rdbuf());                                // for debug
    chrono::system_clock::time_point timestart, timeend; // for debug
    timestart = std::chrono::system_clock::now();        // for debug

#endif

    int n, m, s, t, d;
    ll ans{0};
    cin >> n >> m;
    VI deg(n, 0), odd_node;
    VVL dist_list(n, VL(n, inf));
    rep(i, n) dist_list[i][i] = 0;
    rep(_, m)
    {
        cin >> s >> t >> d;
        dist_list[s][t] = dist_list[t][s] = min<ll>(d, dist_list[s][t]);
        deg[s]++, deg[t]++;
        ans += d;
    }

    rep(k, n) rep(i, n) rep(j, n) if (dist_list[i][j] >= dist_list[i][k] + dist_list[k][j])
        dist_list[i][j] = dist_list[i][k] + dist_list[k][j];

    rep(i, n) if (deg[i] % 2 == 1)
    {
        odd_node.emplace_back(i);
    }

    n = odd_node.size();
    VVL dist_list_2(n, VL(n));
    rep(i, n) rep(j, n) dist_list_2[i][j] = dist_list[odd_node[i]][odd_node[j]];

    // cout << deg << dist_list_2;

    VVL dp(n / 2 + 1, VL(1 << n, inf));
    dp[0][0] = 0;

    rep(i, n / 2) rep(x, 1 << n) rep2(j, 0, n - 1) rep2(k, j + 1, n)
    {
        int y = (1 << j) + (1 << k);
        if ((x & y) == 0) {
            dp[i + 1][x | y] = min(dp[i + 1][x | y], dp[i][x] + dist_list_2[j][k]);
        }
    }

    cout << ans + dp[n / 2][(1 << n) - 1] << rt;

#ifdef DEBUG
    timeend = chrono::system_clock::now();
    auto elapsed =
        chrono::duration_cast<chrono::milliseconds>(timeend - timestart)
            .count();
    cout << "Time is " << elapsed << "ms" << std::endl;
#endif

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4404
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ull;
typedef pair<ll, ll> pil;
const ll ll_INF = 0x3f3f3f3f3f3f3f3f;
const ll ll_MAX = 0x7fffffffffffffff;
const int int_INF = 0x3f3f3f3f;
const int int_MAX = 0x7fffffff;
const double EPS = 1e-7;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
#define R register
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _abs(a) ((a) > 0 ? (a) : -(a))
#define _swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define _eql(x, y) (_abs((x) - (y)) < EPS)
const int MX = 1000100;
struct Node {
    int u, v, val;
    int _next;
} e[1000];
int dis[20][20] = {0};
int head[20], ind[20], cnt = 0;
int n, m, ans, sum;
void addedge(int u, int v, int val) {
    e[++cnt].v = v;
    e[cnt].u = u;
    e[cnt].val = val;
    e[cnt]._next = head[u];
    head[u] = cnt;
}
bool Init() {
    cin >> n >> m;
    if((n | m) == 0)
        return 0;
    int u, v, val;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            dis[i][j] = int_INF;
        }
        dis[i][i] = 0;
    }
    sum = 0;
    for(int i = 0; i < m; i++) {
        cin >> u >> v >> val;
        dis[u][v] = dis[v][u] = _min(val, dis[u][v]);
        sum += val;
        ind[u]++;
        ind[v]++;
    }

    return 1;
}
int odd[20] = {0};
int dp[1 << 16] = {0};
void DFS(int now, int sta, int last) {
    if(now > dp[(1 << odd[0]) - 1])
        return;
    if(sta == (1 << odd[0]) - 1) {
        dp[(1 << odd[0]) - 1] = now;
        return;
    }
    for(int i = last; i <= cnt; i++) {
        if((sta & (1 << e[i].u)) || (sta & (1 << e[i].v)))
            continue;
        sta ^= (1 << e[i].u);
        sta ^= (1 << e[i].v);
        DFS(now + e[i].val, sta, i + 1);
        sta ^= (1 << e[i].u);
        sta ^= (1 << e[i].v);
    }
}

void solve() {
    for(int k = 0; k < n; k++) {
        for(int i = 0; i < n; i++) {
            if(dis[i][k] ^ int_INF)
                for(int j = 0; j < n; j++) {
                    if((dis[k][j] ^ int_INF))
                        dis[i][j] = _min(dis[i][j], dis[i][k] + dis[k][j]);
                }
        }
    }
    odd[0] = 0;
    for(int i = 0; i < n; i++)
        if(ind[i] & 1)
            odd[++odd[0]] = i;
    for(int i = 1; i <= odd[0]; i++) {
        for(int j = i + 1; j <= odd[0]; j++) {
            addedge(i - 1, j - 1, dis[odd[i]][odd[j]]);
        }
    }
    for(int i = 1; i < (1 << odd[0]); i++)
        dp[i] = int_INF;

    DFS(0, 0, 0);
    // cout << "<<<<<<<<<<" << sum << " " << dp[(1 << odd[0]) - 1] << endl;
    cout << sum + dp[(1 << odd[0]) - 1] << endl;
}

int main() {
    std::ios::sync_with_stdio(false);
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    int t = 1;
    while(t--) {
        if(Init())
            solve();
        else
            break;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3464
final_rank: 3

code_snippet: #include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <numeric>
#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>
void split(std::vector<int>& source, std::vector<int>& left, std::vector<int>& right, const std::function<void(const std::vector<int>, std::vector<int>)>& func) {
	if (source.empty()) {
		return func(left, right);
	}
	const auto back = source.back(); source.pop_back();
	if (right.size() + source.size() > left.size()) {
		left.push_back(back);
		split(source, left, right, func);
		left.pop_back();
	}
	if (left.size() + source.size() > right.size()) {
		right.push_back(back);
		split(source, left, right, func);
		right.pop_back();
	}
	source.push_back(back);
}
int factorial(const int n) {
	return n <= 1 ? 1 : n * factorial(n - 1);
}
int main() {
	int v, e; std::cin >> v >> e;
	std::vector<std::vector<int>> min_distance(v, std::vector<int>(v, 15000));
	std::vector<int> degree(v, 0);
	int sum_distance{ 0 };
	for (auto i = 0; i < e; ++i) {
		int s, t, d; std::cin >> s >> t >> d;
		min_distance[s][t] = min_distance[t][s] = std::min(min_distance[s][t], d);
		sum_distance += d;
		++degree[s]; ++degree[t];
	}
	for (auto j = 0; j < v; ++j) {
		for (auto i = 0; i < v; ++i) {
			for (auto k = 0; k < v; ++k) {
				min_distance[i][k] = std::min(min_distance[i][j] + min_distance[j][k], min_distance[i][k]);
			}
		}
	}
	std::vector<int> odd;
	for (auto i = 0; i < v; ++i) {
		if ((degree[i] & 1) == 1) {
			odd.push_back(i);
		}
	}
	if (odd.empty()) {
		std::cout << sum_distance << '\n';
	}
	else {
		std::vector<int> left, right;
		int min_cost{ INT_MAX };
		split(odd, left, right, [&min_cost, &min_distance](const std::vector<int> a, std::vector<int> b) {
			for (auto i = factorial(b.size()); i > 0; --i) {
				std::next_permutation(b.begin(), b.end());
				int cost{ 0 };
				for (auto j = 0; j < a.size(); ++j) {
					cost += min_distance[b[j]][a[j]];
				}
				min_cost = std::min(min_cost, cost);
			}
			});
		std::cout << sum_distance + min_cost << '\n';
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3180
final_rank: 5

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

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}


template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  Int n,m;
  cin>>n>>m;
  
  auto G=make_v<Int>(n,n);
  const Int INF = 1e15;
  fill_v(G,INF);
  for(Int i=0;i<n;i++) G[i][i]=0;

  Int ans=0;
  vector<Int> c(n);
  for(Int i=0;i<m;i++){
    Int s,t,d;
    cin>>s>>t>>d;
    chmin(G[s][t],d);
    chmin(G[t][s],d);
    c[s]++;c[t]++;
    ans+=d;
  }
  
  for(Int k=0;k<n;k++)
    for(Int i=0;i<n;i++)
      for(Int j=0;j<n;j++)
	chmin(G[i][j],G[i][k]+G[k][j]);

  vector<Int> dp(1<<n,INF);
  Int x=0;
  for(Int i=0;i<n;i++) x|=(c[i]&1)<<i;
  dp[x]=0;
  for(Int b=(1<<n)-1;b>=0;b--){
    for(Int i=0;i<n;i++){
      for(Int j=0;j<i;j++){
	if((~b>>i)&1) continue;
	if((~b>>j)&1) continue;
	chmin(dp[b^(1<<i)^(1<<j)],dp[b]+G[i][j]);
      }
    }
  }
  
  cout<<ans+dp[0]<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3228
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int dp[16][65536];
int a[16], b[16], c[16], n, m;
int main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i] >> c[i];
	}
	int minx = 1000000000;
	for (int i = 0; i < n; i++) {
		for (int i = 0; i < 16; i++) {
			for (int j = 0; j < 65536; j++)dp[i][j] = 1000000000;
		}
		dp[i][0] = 0;
		for (int j = 0; j < 8; j++) {
			for (int k = 0; k < n; k++) {
				for (int l = 0; l < (1 << m); l++) {
					int bit[16];
					for (int o = 0; o < m; o++) {
						bit[o] = (l / (1 << o)) % 2;
					}
					for (int o = 0; o < m; o++) {
						if (a[o] != k)goto H;
						if (bit[o] == 1)dp[b[o]][l] = min(dp[b[o]][l], dp[k][l] + c[o]);
						else dp[b[o]][l + (1 << o)] = min(dp[b[o]][l + (1 << o)], dp[k][l] + c[o]);
					H:;
						if (b[o] != k)continue;
						if (bit[o] == 1)dp[a[o]][l] = min(dp[a[o]][l], dp[k][l] + c[o]);
						else dp[a[o]][l + (1 << o)] = min(dp[a[o]][l + (1 << o)], dp[k][l] + c[o]);
					}
				}
			}
		}
		minx = min(minx, dp[i][(1 << m) - 1]);
	}
	cout << minx << endl;
	return 0;
}
accuracy: 0.35
time_ms: 980
memory_kb: 7196
final_rank: 7

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int dp[16][65536];
int a[16], b[16], c[16], n, m;
int main() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i] >> c[i];
	}
	int minx = 1000000000;
	for (int i = 0; i < n; i++) {
		for (int i = 0; i < 16; i++) {
			for (int j = 0; j < 65536; j++)dp[i][j] = 1000000000;
		}
		dp[i][0] = 0;
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < n; k++) {
				for (int l = 0; l < (1 << m); l++) {
					int bit[16];
					for (int o = 0; o < m; o++) {
						bit[o] = (l / (1 << o)) % 2;
					}
					for (int o = 0; o < m; o++) {
						if (a[o] != k)goto H;
						if (bit[o] == 1)dp[b[o]][l] = min(dp[b[o]][l], dp[k][l] + c[o]);
						else dp[b[o]][l + (1 << o)] = min(dp[b[o]][l + (1 << o)], dp[k][l] + c[o]);
					H:;
						if (b[o] != k)continue;
						if (bit[o] == 1)dp[a[o]][l] = min(dp[a[o]][l], dp[k][l] + c[o]);
						else dp[a[o]][l + (1 << o)] = min(dp[a[o]][l + (1 << o)], dp[k][l] + c[o]);
					}
				}
			}
		}
		minx = min(minx, dp[i][(1 << m) - 1]);
	}
	cout << minx << endl;
	return 0;
}
accuracy: 0.15
time_ms: 160
memory_kb: 7200
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
int minimum_weight_matching(const vector<vector<int>>& weight) {
  const int INF = 1e9;
  vector<int> dp(1 << weight.size(), INF);
  dp[0] = 0;
  for(int bit = 0; bit < (1 << weight.size()); ++bit)
    for(int u = 0; u < weight.size(); ++u) if(!((bit >> u) & 1))
      for(int v = 0; v < weight.size(); ++v) if(!((bit >> v) & 1))
        dp[bit | (1 << u) | (1 << v)] = min(dp[bit | (1 << u) | (1 << v)], dp[bit] + weight[u][v]);
  return dp[(1 << weight.size()) - 1];
}
struct Edge {int from, to, weight;};
int chinese_postman_problem(const vector<vector<Edge>>& edge) {
  const int NIL = -1;
  const int INF = 1e9;
  int total = 0;
  vector<int> odd, odd_index(edge.size(), NIL);
  for(int v = 0; v < edge.size(); ++v) {
    for(const auto& e: edge[v]) total += e.weight;
    if(edge[v].size() & 1) {
      odd_index[v] = odd.size();
      odd.push_back(v);
    }
  }
  // Warshall-Floyd
  vector<vector<int>> wf(edge.size(), vector<int>(edge.size(), INF));
  for(int v = 0; v < edge.size(); ++v) for(const auto& e: edge[v]) wf[e.from][e.to] = min(wf[e.from][e.to], e.weight);
//   for(int v = 0; v < edge.size(); ++v) wf[v][v] = 0;
  for(int k = 0; k < edge.size(); ++k)
    for(int i = 0; i < edge.size(); ++i)
      for(int j = 0; j < edge.size(); ++j)
        wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);
  // make odd vertices graph
  vector<vector<int>> weight(odd.size(), vector<int>(odd.size(), INF));
  for(const auto& u: odd) for(const auto& v: odd) if(u != v) weight[odd_index[u]][odd_index[v]] = wf[u][v];
  // minimum weight matching
  return total / 2 + minimum_weight_matching(weight);
}
int main() {
  int V, E;
  cin >> V >> E;
  vector<vector<Edge>> edge(V);
  for(int i = 0; i < E; ++i) {
    int s, t, d;
    cin >> s >> t >> d;
    edge[s].push_back({s, t, d});
    edge[t].push_back({t, s, d});
  }
  cout << chinese_postman_problem(edge) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 1284
final_rank: 1