結果
問題 | No.1690 Power Grid |
ユーザー | LayCurse |
提出日時 | 2021-08-31 06:22:32 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 22,414 bytes |
コンパイル時間 | 3,465 ms |
コンパイル使用メモリ | 239,788 KB |
実行使用メモリ | 43,520 KB |
最終ジャッジ日時 | 2024-07-05 09:51:27 |
合計ジャッジ時間 | 20,136 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | AC | 50 ms
43,520 KB |
testcase_11 | AC | 51 ms
43,520 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 6 ms
7,680 KB |
testcase_15 | RE | - |
testcase_16 | AC | 50 ms
43,520 KB |
testcase_17 | AC | 24 ms
22,528 KB |
testcase_18 | AC | 12 ms
12,544 KB |
testcase_19 | AC | 49 ms
43,520 KB |
testcase_20 | AC | 52 ms
43,520 KB |
testcase_21 | AC | 49 ms
43,520 KB |
testcase_22 | AC | 49 ms
43,392 KB |
testcase_23 | AC | 48 ms
43,520 KB |
testcase_24 | AC | 48 ms
43,520 KB |
testcase_25 | AC | 51 ms
43,392 KB |
testcase_26 | AC | 49 ms
43,392 KB |
testcase_27 | AC | 2 ms
5,376 KB |
ソースコード
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include<bits/stdc++.h> using namespace std; template<class T> struct cLtraits_identity{ using type = T; } ; template<class T> using cLtraits_try_make_signed = typename conditional< is_integral<T>::value, make_signed<T>, cLtraits_identity<T> >::type; template <class S, class T> struct cLtraits_common_type{ using tS = typename cLtraits_try_make_signed<S>::type; using tT = typename cLtraits_try_make_signed<T>::type; using type = typename common_type<tS,tT>::type; } ; void*wmem; char memarr[96000000]; template<class S, class T> inline auto min_L(S a, T b) -> typename cLtraits_common_type<S,T>::type{ return (typename cLtraits_common_type<S,T>::type) a <= (typename cLtraits_common_type<S,T>::type) b ? a : b; } template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ int i; pair<T1, T2>*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second; } } template<class T1, class T2, class T3> void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){ int i; pair<T1, pair<T2, T3> >*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second.first = b[i]; arr[i].second.second = c[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second.first; c[i] = arr[i].second.second; } } struct Rand{ unsigned x; unsigned y; unsigned z; unsigned w; Rand(void){ x=123456789; y=362436069; z=521288629; w=(unsigned)time(NULL); } Rand(unsigned seed){ x=123456789; y=362436069; z=521288629; w=seed; } inline unsigned get(void){ unsigned t; t = (x^(x<<11)); x=y; y=z; z=w; w = (w^(w>>19))^(t^(t>>8)); return w; } inline double getUni(void){ return get()/4294967296.0; } inline int get(int a){ return (int)(a*getUni()); } inline int get(int a, int b){ return a+(int)((b-a+1)*getUni()); } inline long long get(long long a){ return(long long)(a*getUni()); } inline long long get(long long a, long long b){ return a+(long long)((b-a+1)*getUni()); } inline double get(double a, double b){ return a+(b-a)*getUni(); } inline int getExp(int a){ return(int)(exp(getUni()*log(a+1.0))-1.0); } inline int getExp(int a, int b){ return a+(int)(exp(getUni()*log((b-a+1)+1.0))-1.0); } } ; inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void my_putchar_unlocked(const int k){ putchar_unlocked(k); } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned x){ int s=0; char f[10]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned long long x){ int s=0; char f[21]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int WRITER_DOUBLE_DIGIT = 15; inline int writerDigit_double(){ return WRITER_DOUBLE_DIGIT; } inline void writerDigit_double(int d){ WRITER_DOUBLE_DIGIT = d; } inline void wt_L(double x){ const int d = WRITER_DOUBLE_DIGIT; int k; int r; double v; if(x!=x || (x==x+1 && x==2*x)){ my_putchar_unlocked('E'); my_putchar_unlocked('r'); my_putchar_unlocked('r'); return; } if(x < 0){ my_putchar_unlocked('-'); x = -x; } x += 0.5 * pow(0.1, d); r = 0; v = 1; while(x >= 10*v){ v *= 10; r++; } while(r >= 0){ r--; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; v *= 0.1; my_putchar_unlocked(k + '0'); } if(d > 0){ my_putchar_unlocked('.'); v = 1; for(r=(0);r<(d);r++){ v *= 0.1; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; my_putchar_unlocked(k + '0'); } } } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } inline void wt_L(string &x){ int i=0; for(i=0;x[i]!='\0';i++){ my_putchar_unlocked(x[i]); } } inline int BIT_popcount_L(const int x){ return __builtin_popcount(x); } inline int BIT_popcount_L(const long long x){ return __builtin_popcountll(x); } template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } a[k] = aval; } template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } a[k] = aval; b[k] = bval; } template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; } template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } for(i=sz-1;i>k;i--){ d[i] = d[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; d[k] = dval; } template<class S, class T> inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } struct unionFind{ int*d; int N; int M; inline void malloc(const int n){ d = (int*)std::malloc(n*sizeof(int)); M = n; } inline void malloc(const int n, const int fg){ d = (int*)std::malloc(n*sizeof(int)); M = n; if(fg){ init(n); } } inline void free(void){ std::free(d); } inline void walloc(const int n, void **mem=&wmem){ walloc1d(&d, n, mem); M = n; } inline void walloc(const int n, const int fg, void **mem=&wmem){ walloc1d(&d, n, mem); M = n; if(fg){ init(n); } } inline void init(const int n){ int i; N = n; for(i=(0);i<(n);i++){ d[i] = -1; } } inline void init(void){ init(M); } inline int get(int a){ int t = a; int k; while(d[t]>=0){ t=d[t]; } while(d[a]>=0){ k=d[a]; d[a]=t; a=k; } return a; } inline int connect(int a, int b){ if(d[a]>=0){ a=get(a); } if(d[b]>=0){ b=get(b); } if(a==b){ return 0; } if(d[a] < d[b]){ d[a] += d[b]; d[b] = a; } else{ d[b] += d[a]; d[a] = b; } return 1; } inline int operator()(int a){ return get(a); } inline int operator()(int a, int b){ return connect(a,b); } inline int& operator[](const int a){ return d[a]; } inline int size(int a){ a = get(a); return -d[a]; } inline int sizeList(int res[]){ int i; int sz=0; for(i=(0);i<(N);i++){ if(d[i]<0){ res[sz++] = -d[i]; } } return sz; } inline int comp(int res[], void *mem = wmem){ int i; int sz=0; int*cnt; walloc1d(&cnt, N, &mem); for(i=(0);i<(N);i++){ cnt[i] = 0; } for(i=(0);i<(N);i++){ cnt[get(i)] = 1; } for(i=(0);i<(N);i++){ if(cnt[i]){ cnt[i] = sz++; } } for(i=(0);i<(N);i++){ res[i] = cnt[get(i)]; } return sz; } } ; long long cReader_ll(long long mn, long long mx, char nx){ int i; int fg = 0; int m = 1; int f = -1; long long res = 0; double tmp = 0; for(;;){ i = my_getchar_unlocked(); if(fg==0 && i=='-'){ fg++; m = -1; } else if('0' <= i && i <= '9'){ fg++; if(f == -1){ f = i - '0'; } res = 10 * res + i - '0'; tmp = 10 * tmp + i - '0'; assert(tmp < 1e20); } else{ break; } } assert(tmp / 2 <= res); assert((m==1 && fg >= 1) || (m==-1 && fg >= 2)); assert(mn <= m * res && m * res <= mx); assert(!(res == 0 && m == -1)); assert(!(res != 0 && f == 0)); assert(!(res == 0 && fg >= 2)); assert(i == nx); return m * res; } void cReader_eof(){ int i; i = my_getchar_unlocked(); assert(i == EOF); } int N; int M; int K; int A[20]; int X[200]; int Y[200]; int Z[200]; long long baka(){ int i; int j; int k; int ind[20]; long long res = 4611686016279904256LL; long long tmp; long long tmp2; long long mat[20][20]; for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } int hCmBdyQB; for(hCmBdyQB=(0);hCmBdyQB<(N);hCmBdyQB++){ ind[hCmBdyQB] = hCmBdyQB; } do{ { tmp = 0; for(i=(0);i<(K);i++){ tmp += A[ind[i]]; } for(i=(1);i<(K);i++){ tmp2 = 4611686016279904256LL; for(j=(0);j<(i);j++){ chmin(tmp2, mat[ind[j]][ind[i]]); } tmp += tmp2; } chmin(res, tmp); } } while(next_permutation(ind,ind+N)); return res; } long long dist1[20][1200000]; long long dp1[1200000]; int bt[1200000]; int bc[1200000]; long long solve1(){ int i; int j; int k; long long mat[20][20]; long long res = 4611686016279904256LL; for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } for(i=(0);i<(N);i++){ bt[1<<i] = i; } for(i=(1);i<(1<<N);i++){ bc[i] = bc[i&(i-1)] + 1; } for(i=(0);i<(N);i++){ dist1[i][0] = 4611686016279904256LL; for(j=(1);j<(1<<N);j++){ k = (j & (-j)); dist1[i][j] =min_L(dist1[i][j^k], mat[bt[k]][i]); } } for(i=(0);i<(N);i++){ dist1[i][0] = 0; } dp1[0] = 0; for(i=(1);i<(1<<N);i++){ dp1[i] = 4611686016279904256LL; } for(i=(0);i<(1<<N);i++){ for(j=(0);j<(N);j++){ if(!((i) &(1<<(j)))){ chmin(dp1[i|(1<<j)], dp1[i] + dist1[j][i] + A[j]); } } } for(i=(0);i<(1<<N);i++){ if(bc[i]==K){ chmin(res, dp1[i]); } } return res; } long long solve1_2(){ int i; int j; int k; int xs; int ys; int xx[20]; int yy[20]; long long mat[20][20]; long long res = 4611686016279904256LL; for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } dp1[0] = 0; for(i=(1);i<(1<<N);i++){ dp1[i] = 4611686016279904256LL; } for(i=(0);i<(N);i++){ dp1[1<<i] = A[i]; } for(i=(0);i<(1<<N);i++){ xs = ys = 0; for(j=(0);j<(N);j++){ if(!((i) &(1<<(j)))){ xx[xs++] = j; } else{ yy[ys++] = j; } } for(j=(0);j<(xs);j++){ for(k=(0);k<(ys);k++){ chmin(dp1[i|(1<<xx[j])], dp1[i] + mat[xx[j]][yy[k]] + A[xx[j]]); } } } for(i=(0);i<(1<<N);i++){ if(BIT_popcount_L(i)==K){ chmin(res, dp1[i]); } } return res; } unionFind uf2; long long solve2(){ int i, k; int m = 0; int xx[200]; int yy[200]; int p[20]; long long zz[200]; long long res = 4611686016279904256LL; long long tmp; long long mat[20][20]; for(i=(0);i<(N);i++){ int j; for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ int j; for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } for(i=(0);i<(N);i++){ int j; for(j=(i+1);j<(N);j++){ arrInsert(m, m, xx, i, yy, j, zz, mat[i][j]); } } sortA_L(m,zz,xx,yy); for(i=(0);i<(N);i++){ p[i] = 0; } for(i=(0);i<(K);i++){ p[N-1-i] = 1; } do{ uf2.init(N); tmp = 0; for(i=(0);i<(m);i++){ if(p[xx[i]] && p[yy[i]] && uf2(xx[i],yy[i])){ tmp += zz[i]; } } for(i=(0);i<(N);i++){ if(p[i]){ tmp += A[i]; } } chmin(res, tmp); } while(next_permutation(p,p+N)); return res; } long long uso1(){ int s; int i; int j; int k; int cnt; long long mat[20][20]; long long res = 4611686016279904256LL; long long tmp; long long tmp2; int use[20]; long long mn[20]; for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ mat[i][j] = 4611686016279904256LL; } } for(i=(0);i<(N);i++){ mat[i][i] = 0; } for(i=(0);i<(M);i++){ chmin(mat[X[i]][Y[i]], Z[i]); chmin(mat[Y[i]][X[i]], Z[i]); } for(k=(0);k<(N);k++){ for(i=(0);i<(N);i++){ for(j=(0);j<(N);j++){ chmin(mat[i][j], mat[i][k] + mat[k][j]); } } } for(s=(0);s<(N);s++){ for(i=(0);i<(N);i++){ use[i] = 0; mn[i] = 4611686016279904256LL; } mn[s] = A[s]; cnt = 0; tmp = 0; while(cnt < K){ tmp2 = 4611686016279904256LL; for(i=(0);i<(N);i++){ if(!use[i] && tmp2 > mn[i]){ k = i; tmp2 = mn[i]; } } tmp += tmp2; use[k] = 1; cnt++; for(i=(0);i<(N);i++){ if(!use[i]){ chmin(mn[i], A[i] + mat[k][i]); } } } chmin(res, tmp); } return res; } int main(){ wmem = memarr; int i; int j; int k; int m; long long r0; long long r1; long long u1; N = cReader_ll(1, 18, ' '); M = cReader_ll(N-1, N*(N-1)/2-1, ' '); K = cReader_ll(1, N, '\n'); for(i=(0);i<(N);i++){ A[i] = cReader_ll(1, 1000000000, i==N-1?'\n':' '); } for(i=(0);i<(M);i++){ X[i] = cReader_ll(1, N, ' '); Y[i] = cReader_ll(X[i], N, ' '); Z[i] = cReader_ll(1, 1000000000, '\n'); X[i]--; Y[i]--; } cReader_eof(); for(i=(0);i<(M);i++){ for(j=(i+1);j<(M);j++){ assert(X[i] != X[j] || Y[i] != Y[j]); } } uf2.walloc(20,1); for(i=(0);i<(M);i++){ uf2(X[i],Y[i]); } for(i=(0);i<(N);i++){ assert(uf2(0)==uf2(i)); } wt_L(solve1()); wt_L('\n'); return 0; Rand rnd; unionFind uf; uf.walloc(20); puts("hoge"); for(;;){ int con = 0; N = rnd.get(1,8); K = rnd.get(1,N); M = 0; uf.init(N); while(con < N-1){ i = rnd.get(N); j = rnd.get(N); k = rnd.get(1, 1000000000); if(i==j){ continue; } for(m=(0);m<(M);m++){ if(X[m]==i && Y[m]==j){ goto ydh_YK0A; } } for(m=(0);m<(M);m++){ if(X[m]==j && Y[m]==i){ goto ydh_YK0A; } } con += uf(i,j); arrInsert(M,M,X,i,Y,j,Z,k); ydh_YK0A:; } for(i=(0);i<(N);i++){ A[i] = rnd.get(1, 1000000000); } r0 = baka(); r1 = solve1(); u1 = uso1(); wt_L(r0); wt_L(' '); wt_L(r1); wt_L(' '); wt_L(":"); wt_L(' '); wt_L(u1); wt_L('\n'); assert(r0==r1); } return 0; } // cLay version 20210819-1 [beta] // --- original code --- // int N, M, K, A[20], X[200], Y[200], Z[200]; // // ll baka(){ // int i, j, k, ind[20]; // ll res = ll_inf, tmp, tmp2; // ll mat[20][20]; // // rep(i,N) rep(j,N) mat[i][j] = ll_inf; // rep(i,N) mat[i][i] = 0; // rep(i,M){ // mat[X[i]][Y[i]] <?= Z[i]; // mat[Y[i]][X[i]] <?= Z[i]; // } // rep(k,N) rep(i,N) rep(j,N) mat[i][j] <?= mat[i][k] + mat[k][j]; // // rep_perm(ind,N){ // tmp = 0; // rep(i,K) tmp += A[ind[i]]; // rep(i,1,K){ // tmp2 = ll_inf; // rep(j,i) tmp2 <?= mat[ind[j]][ind[i]]; // tmp += tmp2; // } // res <?= tmp; // } // // return res; // } // // // ll dist1[20][12d5]; // ll dp1[12d5]; // int bt[12d5], bc[12d5]; // ll solve1(){ // int i, j, k; // ll mat[20][20], res = ll_inf; // // rep(i,N) rep(j,N) mat[i][j] = ll_inf; // rep(i,N) mat[i][i] = 0; // rep(i,M){ // mat[X[i]][Y[i]] <?= Z[i]; // mat[Y[i]][X[i]] <?= Z[i]; // } // rep(k,N) rep(i,N) rep(j,N) mat[i][j] <?= mat[i][k] + mat[k][j]; // // rep(i,N) bt[1<<i] = i; // rep(i,1,1<<N) bc[i] = bc[i&(i-1)] + 1; // rep(i,N){ // dist1[i][0] = ll_inf; // rep(j,1,1<<N){ // k = (j & (-j)); // dist1[i][j] = min(dist1[i][j^k], mat[bt[k]][i]); // } // } // rep(i,N) dist1[i][0] = 0; // // dp1[0] = 0; // rep(i,1,1<<N) dp1[i] = ll_inf; // rep(i,1<<N) rep(j,N) if(!BIT_ith(i,j)){ // dp1[i|(1<<j)] <?= dp1[i] + dist1[j][i] + A[j]; // } // // rep(i,1<<N) if(bc[i]==K) res <?= dp1[i]; // return res; // } // // ll solve1_2(){ // int i, j, k; // int xs, ys, xx[20], yy[20]; // ll mat[20][20], res = ll_inf; // // rep(i,N) rep(j,N) mat[i][j] = ll_inf; // rep(i,N) mat[i][i] = 0; // rep(i,M){ // mat[X[i]][Y[i]] <?= Z[i]; // mat[Y[i]][X[i]] <?= Z[i]; // } // rep(k,N) rep(i,N) rep(j,N) mat[i][j] <?= mat[i][k] + mat[k][j]; // // dp1[0] = 0; // rep(i,1,1<<N) dp1[i] = ll_inf; // rep(i,N) dp1[1<<i] = A[i]; // rep(i,1<<N){ // xs = ys = 0; // rep(j,N){ // if(!BIT_ith(i,j)) xx[xs++] = j; // else yy[ys++] = j; // } // rep(j,xs) rep(k,ys) dp1[i|(1<<xx[j])] <?= dp1[i] + mat[xx[j]][yy[k]] + A[xx[j]]; // } // // rep(i,1<<N) if(BIT_popcount(i)==K) res <?= dp1[i]; // return res; // } // // unionFind uf2; // ll solve2(){ // int m = 0, xx[200], yy[200], p[20]; ll zz[200]; // ll res = ll_inf, tmp; // ll mat[20][20]; // // rep(i,N) rep(j,N) mat[i][j] = ll_inf; // rep(i,N) mat[i][i] = 0; // rep(i,M){ // mat[X[i]][Y[i]] <?= Z[i]; // mat[Y[i]][X[i]] <?= Z[i]; // } // rep(k,N) rep(i,N) rep(j,N) mat[i][j] <?= mat[i][k] + mat[k][j]; // // rep(i,N) rep(j,i+1,N) arrInsert(m, m, xx, i, yy, j, zz, mat[i][j]); // sortA(m,zz,xx,yy); // // rep(i,N) p[i] = 0; // rep(i,K) p[N-1-i] = 1; // do{ // uf2.init(N); // tmp = 0; // rep(i,m) if(p[xx[i]] && p[yy[i]] && uf2(xx[i],yy[i])) tmp += zz[i]; // rep(i,N) if(p[i]) tmp += A[i]; // res <?= tmp; // // wt(tmp,":",p(N)); // }while(next_permutation(p,p+N)); // // return res; // } // // // ll uso1(){ // int i, j, k, cnt; // ll mat[20][20]; // ll res = ll_inf, tmp, tmp2; // int use[20]; // ll mn[20]; // // rep(i,N) rep(j,N) mat[i][j] = ll_inf; // rep(i,N) mat[i][i] = 0; // rep(i,M){ // mat[X[i]][Y[i]] <?= Z[i]; // mat[Y[i]][X[i]] <?= Z[i]; // } // rep(k,N) rep(i,N) rep(j,N) mat[i][j] <?= mat[i][k] + mat[k][j]; // // rep(s,N){ // rep(i,N) use[i] = 0, mn[i] = ll_inf; // mn[s] = A[s]; // cnt = 0; // tmp = 0; // while(cnt < K){ // tmp2 = ll_inf; // rep(i,N) if(!use[i] && tmp2 > mn[i]){ // k = i; // tmp2 = mn[i]; // } // tmp += tmp2; // use[k] = 1; // cnt++; // rep(i,N) if(!use[i]) mn[i] <?= A[i] + mat[k][i]; // } // res <?= tmp; // } // // return res; // } // // // // { // int i, j, k, m; // ll r0, r1, u1; // // rd(N,M,K,A(N),(X--,Y--,Z)(M)); // // N = cReader_ll(1, 18, ' '); // M = cReader_ll(N-1, N*(N-1)/2-1, ' '); // K = cReader_ll(1, N, '\n'); // rep(i,N) A[i] = cReader_ll(1, 1d9, i==N-1?'\n':' '); // rep(i,M){ // X[i] = cReader_ll(1, N, ' '); // Y[i] = cReader_ll(X[i], N, ' '); // Z[i] = cReader_ll(1, 1d9, '\n'); // X[i]--; // Y[i]--; // } // cReader_eof(); // rep(i,M) rep(j,i+1,M) assert(X[i] != X[j] || Y[i] != Y[j]); // // uf2.walloc(20,1); // rep(i,M) uf2(X[i],Y[i]); // rep(i,N) assert(uf2(0)==uf2(i)); // // wt(solve1()); // return 0; // // Rand rnd; // unionFind uf; // uf.walloc(20); // puts("hoge"); // for(;;){ // int con = 0; // N = rnd.get(1,8); // K = rnd.get(1,N); // M = 0; // uf.init(N); // while(con < N-1){ // i = rnd.get(N); // j = rnd.get(N); // k = rnd.get(1, 1d9); // if(i==j) continue; // rep(m,M) if(X[m]==i && Y[m]==j) break_continue; // rep(m,M) if(X[m]==j && Y[m]==i) break_continue; // con += uf(i,j); // arrInsert(M,M,X,i,Y,j,Z,k); // } // rep(i,N) A[i] = rnd.get(1, 1d9); // // r0 = baka(); // r1 = solve1(); // u1 = uso1(); // wt(r0,r1,":",u1); // assert(r0==r1); // // assert(r0==u1); // } // }