Problem:
D: Rescue a Postal Worker / 郵便局員を救え
物語
あなたは，この春に長年の夢だった郵便局に就職できました．担当する配達地域も決定し，ウキウキ気分で迎えた初仕事ですが，あまりに浮かれていたため郵便物を入れた袋に穴が空いているのに気づかず，入っていた郵便物を全部落としてしまいました．しかし，用意周到なあなたはすべての郵便物にGPSをつけていたため，どこに郵便物が落ちているかを知ることができます．
郵便物を拾いなおしていると予定していた配達時間を超えてしまうかもしれないので，あなたはできる限り短い時間で配達を終えたいと思っています．落としてしまったすべての郵便物を拾いなおして，それぞれの配達先に届ける最短の時間を求めなさい．
問題
あなたの担当する配達地域として無向グラフを考える．
n
頂点からなる無向グラフを考えたとき，それぞれの頂点には
1
から
n
の番号が付けられている．落とした郵便物の数と落ちている頂点，それぞれの郵便物の配達先の頂点が与えられたとき，すべての郵便物を回収して配達先に届ける最短時間を求めよ．このとき，ある郵便物をその郵便物の配達先に届ける場合に拾っていない他の郵便物が存在しても良い．また，配達終了時にはどの頂点にいても良いこととする．
ここで，1つの頂点には高々1つの郵便物か高々1つの配達先しかなく，出発する頂点には郵便物も配達先も存在しない．与えられる無向グラフは単純なグラフ，すなわち自己閉路や多重辺のないグラフである．
入力形式
入力データの形式は以下の通りである．
n
m
k
p
x_1
y_1
w_1
...
x_m
y_m
w_m
s_1
t_1
...
s_k
t_k
最初の1行には無向グラフの頂点数
n
(
3 ≤ n ≤ 1,000
)，辺数
m
(
1 ≤ m ≤ 2,000
)，落とした郵便物の数
k
(
1 ≤ k ≤ 6
)，始点
p
(
1 ≤ p ≤ n
) が与えられる．行中の入力項目は空白1個区切りで与えられる．
続く
m
行ではグラフ中の辺の情報が与えられる．
i
行目には
i
番目の辺の両端の頂点
x_i
(
1 ≤ x_i ≤ n
)，
y_i
(
1 ≤ y_i ≤ n
) とその辺の重み
w_i
(
1 ≤ w_i ≤ 1,000
) が与えられる．
続く
k
行の
j
行目には落とした郵便物のある頂点
s_j
(
1 ≤ s_j ≤ n
) とその郵便物の配達先の頂点
t_j
(
1 ≤ t_j ≤ n
) が与えられる．
ここで，与えられるグラフは自己閉路や多重辺がなく，始点，各々の郵便物が落ちている頂点，各々の配達先はすべてが互いに異なる．
出力形式
頂点
p
から出発して，すべての郵便物をそれぞれの配達先に届ける最短の時間を1行に表示せよ．ただし，届けられない場合は "Cannot deliver" を出力せよ．
入力例1
5 6 1 1
1 3 5
1 2 2
2 3 1
3 5 4
3 4 2
4 5 3
3 5
出力例1
7
入力例2
5 6 1 1
1 3 5
1 2 2
2 3 1
3 5 4
3 4 2
4 5 3
5 3
出力例2
11
入力例3
3 1 1 1
1 2 1
2 3
出力例3
Cannot deliver
入力例4
5 8 2 3
1 2 1
2 3 4
3 4 5
4 5 3
5 1 4
1 3 3
2 5 1
5 3 4
1 2
5 4
出力例3
8

Submitted solutions:

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstdint>
#include <limits>

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)

using i64 = std::int64_t;

int main() {
    int n,m,k,p;
    std::cin >> n >> m >> k >> p;
    p--;
    std::vector g(n, std::vector<std::pair<int,i64>>());
    rep(i,0,m) {
        int x,y,w;
        std::cin >> x >> y >> w;
        x--; y--;
        g[x].emplace_back(y, w);
        g[y].emplace_back(x, w);
    }
    std::vector<std::pair<int,int>> items(k);
    for(auto &[s, t]: items) {
        std::cin >> s >> t;
        s--; t--;
    }
    std::vector<int> pow_3(10, 1);
    rep(i,0,9) {
        pow_3[i+1] = pow_3[i] * 3;
    }
    using P = std::pair<i64,std::pair<int,int>>;
    const i64 LNF = std::numeric_limits<i64>::max() / 4;
    std::priority_queue<P, std::vector<P>, std::greater<P>> que;
    que.push({0,{p,0}});
    std::vector dp(n, std::vector<i64>(pow_3[k], LNF));
    dp[p][0] = 0;
    auto bit_k = [&](int bit, int k) -> int {
        return (bit / pow_3[k]) % 3;  
    };
    while(!que.empty()) {
        P pa = que.top();
        i64 ret = pa.first;
        int v = pa.second.first;
        int bits = pa.second.second;
        que.pop();
        if(dp[v][bits] < ret) continue;
        for(auto [nv, cost]: g[v]) {
            int next_bit = bits;
            rep(i,0,k) {
                auto [s, t] = items[i];
                int bit = bit_k(bits, i);
                if(s == nv && bit == 0) {
                    next_bit += pow_3[i];
                }
                if(t == nv && bit == 1) {
                    next_bit += pow_3[i];
                }
            }
            if(dp[v][bits] + cost < dp[nv][next_bit]) {
                dp[nv][next_bit] = dp[v][bits] + cost;
                que.push({dp[nv][next_bit], {nv, next_bit}});
            }
        }
    }
    i64 ans = LNF;
    rep(i,0,n) {
        ans = std::min(ans, dp[i][pow_3[k] - 1]);
    }
    if(ans == LNF) {
        std::cout << "Cannot deliver\n";
        return 0;
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 230
memory_kb: 10032
final_rank: 5

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (int i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}

using len_type=int;
struct edge{
    int to;
    len_type length;
};

len_type dijkstra_movement(edge e,int from,len_type t){
    return t+e.length;
}


vector<len_type> dijkstra(vector<vector<edge>> &G,int start,len_type inf){
    int M=G.size();
    assert(M>start&&start>=0);
    vector<len_type> seen(M,inf);
    seen[start]=0;
    priority_queue<pair<len_type,int>,vector<pair<len_type,int>>,greater<pair<len_type,int>>> pq;
    pq.push({0,start});
    while(!pq.empty()){
        len_type time;
        int ver;
        tie(time,ver)=pq.top();
        pq.pop();
        if(seen[ver]<time) continue;
        for(edge x:G[ver]){
            len_type T=dijkstra_movement(x,ver,time);
            if(T<seen[x.to]){
                pq.push({T,x.to});
                seen[x.to]=T;
            }
        }
    }
    return seen;
}


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int n,m,k,p;
	cin>>n>>m>>k>>p;
	vector<vector<edge>> G(n*(1<<(2*k)));
	rep(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		a--,b--;
		rep(j,(1<<(2*k))){
			G[a+j*n].push_back({b+j*n,c});
			G[b+j*n].push_back({a+j*n,c});
		}
	}
	rep(i,k){
		int s,t;
		cin>>s>>t;
		s--,t--;
		rep(j,(1<<(2*k))){
			if((j&(1<<i))==0&&(j&(1<<(i+k)))==0){
				G[s+j*n].push_back({s+(j+(1<<i))*n,0});
			}
			if((j&(1<<i))!=0&&(j&(1<<(i+k)))==0){
				G[t+j*n].push_back({t+(j+(1<<(i+k))-(1<<i))*n,0});
			}
		}
	}
	auto seen=dijkstra(G,p-1,INF);
	int ans=INF;
	int tmp=(1<<(2*k))-(1<<k);
	rep(i,n){
		chmin(ans,seen[i+n*tmp]);
	}
	if(ans==INF) cout<<"Cannot deliver\n";
	else cout<<ans<<"\n";
}
accuracy: 1
time_ms: 1380
memory_kb: 359516
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> pii;
const int inf = 1e9;

struct state{
    int p;
    int c;
    int get;
    int cpl;
    state(int p, int c, int g, int cp):
        p(p),c(c),get(g),cpl(cp){}
    state();
    bool operator <(const state &a) const{
        return c > a.c;
    }
};

int main(){
    int n,m,k,p;
    cin >> n >> m >> k >> p;
    p--;
    vector<vector<pii>> adj(n);
    for(int i=0; i<m; i++){
        int x,y,c;
        cin >> x >> y >> c;
        x--; y--;
        adj[x].emplace_back(y, c);
        adj[y].emplace_back(x, c);
    }
    vector<int> s(k), t(k);
    for(int i=0; i<k; i++){
        cin >> s[i] >> t[i];
        s[i]--; t[i]--;
    }

    priority_queue<state> wait;
    wait.push(state(p, 0, 0, 0));
    vector<vector<vector<int>>> dp(n, vector<vector<int>>(1<<k, vector<int>(1<<k, inf)));
    dp[p][0][0] = 0;
    while(!wait.empty()){
        int p = wait.top().p;
        int c = wait.top().c;
        int get = wait.top().get;
        int cpl = wait.top().cpl;
        wait.pop();
        if(c > dp[p][get][cpl]) continue;
        for(pii next: adj[p]){
            int np = next.first;
            int nc = c + next.second;
            int nget = get;
            int ncpl = cpl;
            for(int i=0; i<k; i++){
                if(np == s[i]){
                    nget |= 1<<i;
                }
                if(np == t[i] and (nget>>i&1)){
                    ncpl |= 1<<i;
                }
            }
            if(nc < dp[np][nget][ncpl]){
                dp[np][nget][ncpl] = nc;
                wait.push(state(np, nc, nget, ncpl));
            }
        }
    }
    int ans = inf;
    for(auto vv: dp){
        for(auto v: vv){
            ans = min(ans, v[(1<<k)-1]);
        }
    }
    if(ans == inf){
        cout << "Cannot deliver" << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 22572
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

vector<vector<ll>> dist(1010, vector<ll>(1010, 1e9));

void dijkstra(int s, vector<vector<P>> &G){
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s][s] = 0;
    que.emplace(0, s);

    while(que.size()){
        ll ccost, cv;
        tie(ccost, cv) = que.top();
        que.pop();

        if(dist[s][cv] < ccost) continue;

        for(auto x : G[cv]){
            ll nv, ncost;
            tie(nv, ncost) = x;
            
            if(dist[s][cv] + ncost < dist[s][nv]){
                dist[s][nv] = dist[s][cv] + ncost;
                que.emplace(dist[s][nv], nv);
            }
        }
    }
}

int main(){
    int n, m, k, p; cin>>n>>m>>k>>p;
    p--;
    vector<vector<P>> G(n);
    for(int i=0; i<m; i++){
        int x, y, w; cin>>x>>y>>w;
        x--, y--;
        G[x].emplace_back(y, w);
        G[y].emplace_back(x, w);
    }

    for(int i=0; i<n; i++){
        dijkstra(i, G);
    }

    vector<int> s(k), t(k);
    vector<int> v;
    for(int i=0; i<k; i++){
        cin>>s[i]>>t[i];
        s[i]--, t[i]--;
        v.emplace_back(i);
        v.emplace_back(i);
    }

    ll ans = 1e18;
    do{
        ll sum = dist[p][s[v[0]]];
        bool valid = true;
        if(sum == 1e9) valid = false;
        vector<int> visited(k, 0);
        visited[v[0]]++;
        for(int i=0; i<2*k-1; i++){
            ll cdist = 0;
            if(visited[v[i+1]] == 0){
                if(visited[v[i]] == 1){
                    cdist = dist[s[v[i]]][s[v[i+1]]];
                }
                else{
                    cdist = dist[t[v[i]]][s[v[i+1]]];
                }
            }
            else{
                if(visited[v[i]] == 1){
                    cdist = dist[s[v[i]]][t[v[i+1]]];
                }
                else{
                    cdist = dist[t[v[i]]][t[v[i+1]]];
                }
            }

            visited[v[i+1]]++;

            if(cdist == 1e9) valid = false;
            else sum += cdist;
        }

        if(valid) ans = min(ans, sum);

    }while(next_permutation(v.begin(), v.end()));

    if(ans == 1e18){
        cout << "Cannot deliver" << endl;
    }
    else{
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 530
memory_kb: 11192
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

vector<vector<ll>> dist(1010, vector<ll>(1010, 1e9));

void dijkstra(int s, vector<vector<P>> &G){
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s][s] = 0;
    que.emplace(0, s);

    while(que.size()){
        ll ccost, cv;
        tie(ccost, cv) = que.top();
        que.pop();

        if(dist[s][cv] < ccost) continue;

        for(auto x : G[cv]){
            ll nv, ncost;
            tie(nv, ncost) = x;
            
            if(dist[s][cv] + ncost < dist[s][nv]){
                dist[s][nv] = dist[s][cv] + ncost;
                que.emplace(dist[s][nv], nv);
            }
        }
    }
}

int main(){
    int n, m, k, p; cin>>n>>m>>k>>p;
    p--;
    vector<vector<P>> G(n);
    for(int i=0; i<m; i++){
        int x, y, w; cin>>x>>y>>w;
        x--, y--;
        G[x].emplace_back(y, w);
        G[y].emplace_back(x, w);
    }

    for(int i=0; i<n; i++){
        dijkstra(i, G);
    }

    vector<int> s(k), t(k);
    vector<int> v;
    for(int i=0; i<k; i++){
        cin>>s[i]>>t[i];
        s[i]--, t[i]--;
        v.emplace_back(i);
        v.emplace_back(i);
    }

    ll ans = 1e18;
    do{
        ll sum = dist[p][s[v[0]]];
        vector<int> visited(k, 0);
        visited[v[0]]++;
        bool valid = true;
        for(int i=0; i<2*k-1; i++){
            ll cdist = 0;
            if(visited[v[i+1]] == 0){
                if(visited[v[i]] == 1){
                    cdist = dist[s[v[i]]][s[v[i+1]]];
                }
                else{
                    cdist = dist[t[v[i]]][s[v[i+1]]];
                }
            }
            else{
                if(visited[v[i]] == 1){
                    cdist = dist[s[v[i]]][t[v[i+1]]];
                }
                else{
                    cdist = dist[t[v[i]]][t[v[i+1]]];
                }
            }

            visited[v[i+1]]++;

            if(cdist == 1e9) valid = false;
            else sum += cdist;
        }

        if(valid) ans = min(ans, sum);

    }while(next_permutation(v.begin(), v.end()));

    if(ans == 1e18){
        cout << "Cannot deliver" << endl;
    }
    else{
        cout << ans << endl;
    }
}
accuracy: 0.8
time_ms: 490
memory_kb: 11188
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <tuple>
using namespace std;

typedef pair<int, int> P;

vector<vector<int>> dist(1010, vector<int>(1010, 1e9));

void dijkstra(int s, vector<vector<P>> &G){
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s][s] = 0;
    que.emplace(0, s);

    while(que.size()){
        int ccost, cv;
        tie(ccost, cv) = que.top();
        que.pop();

        if(dist[s][cv] < ccost) continue;

        for(auto x : G[cv]){
            int nv, ncost;
            tie(nv, ncost) = x;
            
            if(dist[s][cv] + ncost < dist[s][nv]){
                dist[s][nv] = dist[s][cv] + ncost;
                que.emplace(dist[s][nv], nv);
            }
        }
    }
}

int main(){
    int n, m, k, p; cin>>n>>m>>k>>p;
    p--;
    vector<vector<P>> G(n);
    for(int i=0; i<m; i++){
        int x, y, w; cin>>x>>y>>w;
        x--, y--;
        G[x].emplace_back(y, w);
        G[y].emplace_back(x, w);
    }

    for(int i=0; i<n; i++){
        dijkstra(i, G);
    }

    vector<int> s(k), t(k);
    vector<int> v;
    for(int i=0; i<k; i++){
        cin>>s[i]>>t[i];
        s[i]--, t[i]--;
        v.emplace_back(i);
        v.emplace_back(i);
    }

    int ans = 1e9;
    do{
        int sum = dist[p][s[v[0]]];
        vector<int> visited(k, 0);
        visited[v[0]]++; 
        for(int i=0; i<2*k-1; i++){
            if(visited[v[i+1]] == 0){
                if(visited[v[i]] == 1){
                    sum += dist[s[v[i]]][s[v[i+1]]];
                }
                else{
                    sum += dist[t[v[i]]][s[v[i+1]]];
                }
            }
            else{
                if(visited[v[i]] == 1){
                    sum += dist[s[v[i]]][t[v[i+1]]];
                }
                else{
                    sum += dist[t[v[i]]][t[v[i+1]]];
                }
            }

            visited[v[i+1]]++;
        }

        ans = min(ans, sum);

    }while(next_permutation(v.begin(), v.end()));

    if(ans == 1e9){
        cout << "Cannot deliver" << endl;
    }
    else{
        cout << ans << endl;
    }
}
accuracy: 0.38181818181818183
time_ms: 390
memory_kb: 7068
final_rank: 19

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <tuple>
#include <queue>

using namespace std;

class Solver {
};
signed main() {
	int n, m, k, p;
	cin >> n >> m >> k >> p;
	p--;

	vector<vector<tuple<int, int>>> graph(n,
		vector<tuple<int, int>>(0));
	for (int i = 0; i < m; i++) {
		int x, y, w;
		cin >> x >> y >> w;
		x--;
		y--;
		graph[x].emplace_back(y, w);
		graph[y].emplace_back(x, w);
	}

	map<int, tuple<int, int>> posts;
	int pows = 1;
	for (int i = 0; i < k; i++) {
		int s, t;
		cin >> s >> t;
		s--;
		t--;
		posts[s] = make_tuple(pows, 0);
		posts[t] = make_tuple(pows, 1);
		pows *= 3;
	}

	int state_cnt = pow(3, k);
	vector<vector<int>> dp(n,
		vector<int>(state_cnt, 1e9));

	priority_queue<tuple<int, int, int>,
		vector<tuple<int, int, int>>,
		greater<tuple<int, int, int>>>que;
	que.emplace(make_tuple(0, 0, p));
	while (que.size() > 0) {
		int dist, c_bit, now;
		tie(dist, c_bit, now) = que.top();
		que.pop();
		if (dp[now][c_bit] <= dist)continue;
		dp[now][c_bit] = dist;
		for (int i = 0; i < graph[now].size(); i++) {
			int to = get<0>(graph[now][i]);
			int next_dist = get<1>(graph[now][i]) + dist;
			if (dp[to][c_bit] <= next_dist)continue;

			que.emplace(next_dist, c_bit, to);
		}

		int next_bit = c_bit;
		if (posts.find(now) != posts.end()) {
			int div = get<0>(posts[now]);
			int mode = get<1>(posts[now]);
			int bit_tmp = (c_bit / div) % 3;
			if (bit_tmp == mode)next_bit += div;
		}
		if (c_bit == next_bit || dp[now][next_bit] <= dist)continue;
		que.emplace(dist, next_bit, now);
	}

	int res = 1e9;
	for (int i = 0; i < n; i++) {
		res = min(res, dp[i][state_cnt - 1]);
	}
	if (res == 1e9)cout << "Cannot deliver" << endl;
	else cout << res << endl;
}
accuracy: 1
time_ms: 250
memory_kb: 8476
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <tuple>
#include <queue>

#define int long long

using namespace std;

class Solver {};

signed main() {
	int n, m, k, p;
	cin >> n >> m >> k >> p;
	p--;

	vector<vector<tuple<int, int>>> graph(n,
		vector<tuple<int, int>>(0));
	for (int i = 0; i < m; i++) {
		int x, y, w;
		cin >> x >> y >> w;
		x--;
		y--;
		graph[x].emplace_back(make_tuple(y, w));
		graph[y].emplace_back(make_tuple(x, w));
	}

	set<int> poses;
	poses.emplace(p);
	vector<tuple<int, int>> posts;
	for (int i = 0; i < k; i++) {
		int s, t;
		cin >> s >> t;
		s--;
		t--;
		posts.emplace_back(make_tuple(s, t));
		poses.emplace(s);
		poses.emplace(t);
	}

	vector<vector<int>> dists(n, vector<int>(n, 1e9));
	for (int i = 0; i < n; i++) {
		if (poses.find(i) == poses.end())continue;

		priority_queue<tuple<int, int>,
			vector<tuple<int,int>>,greater<tuple<int,int>>> que;
		que.emplace(0, i);
		while (que.size() > 0) {
			int pos, dist;
			pos = get<1>(que.top());
			dist = get<0>(que.top());
			//tie(dist,pos) = que.top();
			que.pop();
			if (dists[i][pos] <= dist)continue;

			dists[i][pos] = dist;
			for (int j = 0; j < graph[pos].size(); j++) {
				int to = get<0>(graph[pos][j]);
				int next_dist = dist + get<1>(graph[pos][j]);
				if (dists[i][to] <= next_dist)continue;

				que.emplace(next_dist, to);
			}
		}
	}

	vector<int> pat_perm;
	for (int i = 0; i < k; i++) {
		pat_perm.emplace_back(i);
		pat_perm.emplace_back(i);
	}

	int res = 1e9;
	do {
		vector<bool> first(k, true);
		int from = p;
		int tmp = 0;
		bool valid = true;
		for (int i = 0; i < pat_perm.size(); i++) {
			int to;
			if (first[pat_perm[i]]) {
				to = get<0>(posts[pat_perm[i]]);
				first[pat_perm[i]] = false;
			}
			else {
				to = get<1>(posts[pat_perm[i]]);
			}
			if (dists[from][to] == 1e9) {
				valid = false;
				break;
			}
			tmp += dists[from][to];
			from = to;
		}
		if (valid) res = min(res, tmp);
	} while (next_permutation(pat_perm.begin(), pat_perm.end()));

	if (res == 1e9)cout << "Cannot deliver" << endl;
	else cout << res << endl;
}
accuracy: 1
time_ms: 500
memory_kb: 10996
final_rank: 11

code_snippet: #include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <tuple>
#include <utility>
#include <queue>
using namespace std;
using AnsType = long long int;
const AnsType INF = 1LL << 60;

int pow3[10];
int get_digit(int val, int k) {
  val /= pow3[k];
  return val % 3;
}

string tri_to_string(int val, int lim) {
  string res = "";
  for(int i=0; i<lim; i++) {
    res += (char)('0' + get_digit(val, i));
  }
  return res;
}

int modify_digit(int bit, int k, int val) {
  int b = get_digit(bit, k);
  bit -= pow3[k] * b;
  bit += pow3[k] * val;
  return bit;
}

struct Elem {
  int pos, bit;
  AnsType cost;
  Elem() {}
  Elem(int a, int b, AnsType c) : pos(a), bit(b), cost(c) {}
  bool operator<(const Elem &e) const {
    return cost > e.cost;
  }
};

using Graph = vector< vector< pair<int, int> > >;

int main() {
  int N, M, K, P; cin >> N >> M >> K >> P; P--;
  Graph G(N);
  for(int i=0; i<M; i++) {
    int x, y, m; cin >> x >> y >> m;
    x--; y--;
    G[x].emplace_back(y, m);
    G[y].emplace_back(x, m);
  }

  vector< vector<int> > S(N), T(N);
  for(int i=0; i<K; i++) {
    int s, t; cin >> s >> t;
    s--; t--;
    S[s].emplace_back(i);
    T[t].emplace_back(i);
  }

  pow3[0] = 1;
  for(int i=1; i<=K; i++) pow3[i] = pow3[i-1] * 3;
  int fullbit = pow3[K] - 1;
  
  priority_queue<Elem> que;
  vector< vector<AnsType> > dist(N, vector<AnsType>(pow3[K], INF));
  dist[P][0] = 0;
  que.emplace(P, 0, 0);

  while(que.size()) {
    auto cur = que.top(); que.pop();
    int pos = cur.pos, bit = cur.bit;
    AnsType d = cur.cost;
    if(d > dist[pos][bit]) continue;
    // fprintf(stderr, "pos = %d, bit = %s, d = %lld\n", pos + 1, tri_to_string(bit, K).c_str(), d);
    
    {
      int nbit = bit;

      for(auto s : S[pos]) {
        int v = get_digit(nbit, s);
        if(v != 0) continue;
        nbit = modify_digit(nbit, s, v + 1);
      }

      for(auto t : T[pos]) {
        int v = get_digit(nbit, t);
        if(v != 1) continue;
        nbit = modify_digit(nbit, t, v + 1);
      }

      if(dist[pos][nbit] > d) {
        dist[pos][nbit] = d;
        que.emplace(pos, nbit, d);
      }
    }

    for(auto e : G[pos]) {
      int to, c; tie(to, c) = e;

      // fprintf(stderr, "e: to = %d, c = %d\n", to, c);
      int nbit = bit;
      if(dist[to][nbit] > d + c) {
        // fprintf(stderr, "cur = %d, to = %d\n", pos, to);
        dist[to][nbit] = d + c;
        que.emplace(to, nbit, d + c);
      }
    }
  }

  AnsType ans = INF;
  for(int i=0; i<N; i++) {
    ans = min(ans, dist[i][fullbit]);
  }

  if(ans == INF) cout << "Cannot deliver" << endl;
  else cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 9764
final_rank: 2

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

typedef pair<int, int> Pii;
typedef pair<Pii, string> Pis;

struct Edge {
  int to, cost;  
};

int N, M, K, P;
vector<int> s, t;
vector<int> ss[1000];
vector<int> tt[1000];
vector<Edge> G[1000];

int dij() {
  priority_queue<Pis, vector<Pis>, greater<Pis> > Q;
  map<string, bool> used[N];
  string ini = "";
  string ends = "";
  for ( int i = 0; i < K; i++ ) {
    ini += "0";
    ends += "2";
  }

  for ( int j = 0; j < (int)ss[P].size(); j++ ) {
    if ( ini[ss[P][j]] == '0' ) ini[ss[P][j]] = '1';    
  }
  for ( int j = 0; j < (int)tt[P].size(); j++ ) {
    if ( ini[tt[P][j]] == '1' ) ini[tt[P][j]] = '2';	
  }
  
  Q.push(Pis(Pii(0, P), ini));
  
  while ( !Q.empty() ) {    
    Pis p = Q.top(); Q.pop();    
    int c = p.first.first;
    int n = p.first.second;
    string sta = p.second;
    //cout << n << " " << c << " "  << sta << endl;
    //cout << n << endl;
    if ( sta == ends ) return c;

    if ( used[n][sta] ) continue;
    used[n][sta] = true;

    for ( int i = 0; i < (int)G[n].size(); i++ ) {
      Edge e = G[n][i];
      int to = e.to, cost = e.cost;
      string nxt = sta;      
      for ( int j = 0; j < (int)ss[to].size(); j++ ) {
	if ( nxt[ss[to][j]] == '0' ) nxt[ss[to][j]] = '1';	
      }
      for ( int j = 0; j < (int)tt[to].size(); j++ ) {
	if ( nxt[tt[to][j]] == '1' ) nxt[tt[to][j]] = '2';	
      }
      Q.push(Pis(Pii(c+cost, to), nxt));      
    }
    //cout << n << endl;
  }

  return -1;
}

int main() {
  cin >> N >> M >> K >> P;
  P--;
  
  for ( int i = 0; i < M; i++ ) {
    int x, y, w;
    cin >> x >> y >> w;
    x--; y--;
    G[x].push_back(Edge{y, w});
    G[y].push_back(Edge{x, w}); 
  }
 
  s = vector<int>(K);
  t = vector<int>(K);
  for ( int i = 0; i < K; i++ ) {
    cin >> s[i] >> t[i];
    s[i]--; t[i]--;
    ss[s[i]].push_back(i);
    tt[t[i]].push_back(i);    
  } 
  
  int ans = dij();
  if ( ans == -1 ) cout << "Cannot deliver" << endl;
  else cout << ans << endl;
  
  return 0;
}
accuracy: 1
time_ms: 1890
memory_kb: 49992
final_rank: 16

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	Info(){
		start = goal = 0;
	}
	Info(int arg_start,int arg_goal){
		start = arg_start;
		goal = arg_goal;
	}
	int start,goal;
};


int main(){

	int N,M,K,P;
	int POW[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
	scanf("%d %d %d %d",&N,&M,&K,&P);

	int** dist = new int*[N+1];
	for(int i = 1; i <= N; i++){
		dist[i] = new int[N+1];
		for(int k = 1; k <= N; k++){
			if(i != k)dist[i][k] = BIG_NUM;
			else{
				dist[i][k] = 0;
			}
		}
	}

	int from,to,cost;
	for(int i = 0; i < M; i++){
		scanf("%d %d %d",&from,&to,&cost);
		dist[from][to] = cost;
		dist[to][from] = cost;
	}

	for(int mid = 1; mid <= N; mid++){
		for(int st = 1; st <= N; st++){
			if(dist[st][mid] == BIG_NUM)continue;
			for(int gl = 1; gl <= N; gl++){
				if(dist[mid][gl] == BIG_NUM)continue;
				dist[st][gl] = min(dist[st][gl],dist[st][mid]+dist[mid][gl]);
			}
		}
	}

	int start,goal;
	vector<Info> V;

	for(int i = 0; i < K; i++){
		scanf("%d %d",&start,&goal);
		V.push_back(Info(start,goal));
	}

	bool FLG = true;

	for(int i = 0; i < K; i++){
		if(dist[P][V[i].start] == BIG_NUM || dist[P][V[i].goal] == BIG_NUM){
			FLG = false;
			break;
		}
	}

	if(!FLG){
		printf("Cannot deliver\n");
		return 0;
	}

	int digit = 2*K;
	int limit = POW[digit];

	int** dp = new int*[limit];
	for(int i = 0; i < limit; i++){
		dp[i] = new int[digit];
		for(int k = 0; k < digit; k++)dp[i][k] = BIG_NUM;
	}

	for(int i = 0; i < K; i++){
		dp[POW[2*i]][2*i] = dist[P][V[i].start];
	}

	bool check[digit];
	int next_state;

	for(int state = 0; state < limit; state++){
		for(int node = 0; node < digit; node++){
			if(dp[state][node] != BIG_NUM){

				for(int loop = 0; loop < digit; loop++){
					if(state & (1 << loop)){
						check[loop] = true;
					}else{
						check[loop] = false;
					}
				}

				from = (node%2 == 0)?V[node/2].start:V[node/2].goal;

				for(int i = 0; i < K; i++){
					if(check[2*i] == false){
						next_state = state + POW[2*i];
						dp[next_state][2*i] = min(dp[next_state][2*i],dp[state][node] + dist[from][V[i].start]);
					}
				}

				for(int i = 0; i < K; i++){
					if(check[2*i] == true && check[2*i+1] == false){
						next_state = state + POW[2*i+1];
						dp[next_state][2*i+1] = min(dp[next_state][2*i+1],dp[state][node]+dist[from][V[i].goal]);
					}
				}
			}
		}
	}

	int ans = BIG_NUM;
	for(int i = 0; i < digit; i++)ans = min(ans,dp[limit-1][i]);

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 7428
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1000*1000 * 2;

struct Cost{
  int pos;
  int state;
  int cost;
  Cost():pos(0),state(0),cost(0){}
  Cost(int pos, int state, int cost):pos(pos),state(state),cost(cost){}
  bool operator<(const Cost& a)const{
    return cost > a.cost;
  }
};

struct Node{
  int to;
  int cost;
  Node():to(0),cost(0){}
  Node(int to, int cost):to(to),cost(cost){}
};

int jIsS(const int j, const vector<int>& S){
  for(unsigned int i = 0; i < S.size(); i++){
    if(S[i] == j){
      return i;
    }
  }
  return -1;
}

int jIsT(const int j, const vector<int>& T){
  for(unsigned int i = 0; i < T.size(); i++){
    if(T[i] == j){
      return i;
    }
  }
  return -1;
}

void dijkstra(const int node, const int edge, const int env, const int start, const vector< vector<Node> >& G, vector<int>& S, const vector<int>& T){
  int n_state = 1 << (2*env);
  int d[n_state][node];
  for(int i = 0; i < n_state; i++){
    for(int j = 0; j < node; j++){
      d[i][j] = INF;
    }
  }
  d[0][start] = 0;

  priority_queue< Cost > que;
  que.push(Cost(start, 0, 0));
  while(!que.empty()){
    Cost q = que.top(); que.pop();
    int mini = q.state;
    int minj = q.pos;
    //if(q.cost > d[mini][minj]) continue;
    for(unsigned int j = 0; j < G[minj].size(); j++){
      int to = G[minj][j].to;
      int cost = G[minj][j].cost;
      int Sidx = jIsS(to, S);
      int Tidx = jIsT(to, T);
      int state = mini;
      if(Sidx != -1){
        state = mini | (1 << Sidx);
      }
      if(Tidx != -1 && ((mini >> Tidx)&1)){
        state = mini | (1 << (env + Tidx));
      }
      if( d[state][to] > d[mini][minj] + cost ){
        d[state][to] = d[mini][minj] + cost;
        que.push( Cost(to, state, d[state][to]) );
      }
      /*
      if( Sidx != -1 ){
        state = mini | (1 << Sidx);
        d[mini | (1 << Sidx)][to] = min(d[mini | (1 << Sidx)][to],  d[mini][minj] + cost );
        que.push(Cost(to, mini | (1 << Sidx), d[mini | (1 << Sidx)][to]));
      }else if( Tidx != -1 ){
        if( mini & (1 << Tidx) ){
          state = mini | (1 << (env + Tidx));
          d[mini | (1 << (env + Tidx))][to] = min(d[mini | (1 << (env + Tidx))][to], d[mini][minj] + cost);
          que.push( Cost(to, mini | (1 << (env + Tidx)), d[mini | (1 << (env + Tidx))][to]) );
        }
      }
      if( d[mini][to] > d[mini][minj] + cost ){
        d[mini][to] = d[mini][minj] + cost;
        que.push( Cost(to, mini, d[mini][to]) );
      }
      */
    }
  }

  /*
  for(int i = 0; i < n_state; i++) for(int j = 0; j < node; j++){
    cout << bitset<4>(i) << " " << j << " = ";
    cout << d[i][j] << endl;
  }
  */

  int ans = INF;
  for(int j = 0; j < node; j++){
    ans = min(ans, d[(1<<(2*env))-1][j]);
  }

  if(ans == INF) cout << "Cannot deliver" << endl;
  else cout << ans << endl;
}

int main(void){
  int n, m, k, p; cin >> n >> m >> k >> p;
  p--;
  vector< vector<Node> > G(n);
  for(int i = 0; i < m; i++){
    int x, y, w; cin >> x >> y >> w;
    x--; y--;
    G[x].push_back(Node(y, w));
    G[y].push_back(Node(x, w));
  }
  vector<int> S(k), T(k);
  for(int i = 0; i < k; i++){
    int s, t; cin >> s >> t;
    s--; t--;
    S[i] = s;
    T[i] = t;
  }

  dijkstra(n, m, k, p, G, S, T);
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 19908
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
int N, K;
ll dist[20][20];
ll f(int visited, int pos) {
	if (visited == (1 << (2 * K)) - 1) {
		return 0;
	}
	ll r = INF;
	for (int i = 0; i < K; ++i) {
		if (((visited >> i) & 1) == 0) {
			r = min(r, f(visited | (1 << i), i) + dist[pos][i]);
		} else if (((visited >> (K + i)) & 1) == 0) {
			r = min(r, f(visited | (1 << (K + i)), K + i) + dist[pos][K + i]);
		}
	}
	return r;
}
vector<vector<pair<int, int>>> G;
int main() {
	int M, P;
	cin >> N >> M >> K >> P;
	--P;
	G.resize(N);
	for (int i = 0; i < M; ++i) {
		int x, y, w;
		cin >> x >> y >> w;
		--x; --y;
		G[x].push_back({y, w});
		G[y].push_back({x, w});
	}
	vector<int> a(2 * K + 1);
	a[2 * K] = P;
	for (int i = 0; i < K; ++i) {
		int s, t;
		cin >> s >> t;
		--s; --t;
		a[i] = s;
		a[i + K] = t;
	}
	for (int i = 0; i < 2 * K; ++i) {
		vector<ll> D(N, INF);
		priority_queue<pair<ll, int>> Q;
		D[a[i]] = 0;
		Q.push({0, a[i]});
		while (!Q.empty()) {
			ll d = -Q.top().first;
			int v = Q.top().second;
			Q.pop();
			if (d != D[v]) continue;
			for (auto p : G[v]) {
				int vv = p.first;
				ll dd = d + p.second;
				if (dd < D[vv]) {
					D[vv] = dd;
					Q.push({-dd, vv});
				}
			}
		}
		for (int j = i + 1; j <= 2 * K; ++j) {
			if (D[a[j]] == INF) {
				cout << "Cannot deliver" << endl;
				return 0;
			}
			dist[i][j] = dist[j][i] = D[a[j]];
		}
	}
	cout << f(0, 2 * K) << endl;
}
accuracy: 1
time_ms: 140
memory_kb: 3232
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int INF = 1000*20000;

struct Edge{
  int to;
  int cost;
  Edge():to(0),cost(0){}
  Edge(int to, int cost):to(to),cost(cost){}
  bool operator<(const Edge& e)const{
    return cost < e.cost;
  }
};

void dijkstra(const int start, const vector< vector<Edge> >& G1, vector< vector<int> >& G2){
  for(int i = 0;  i < G2[start].size(); i++){
    G2[start][i] = INF;
  }

  G2[start][start] = 0;

  priority_queue< P > que; //cost, pos
  que.push(P(0, start));
  while(!que.empty()){
    P p = que.top(); que.pop();
    int pos = p.second;
    int cost = p.first;
    if(G2[start][pos] < cost) continue;
    for(int i = 0; i < G1[pos].size(); i++){
      int to = G1[pos][i].to;
      if(G2[start][to] > G2[start][pos] + G1[pos][i].cost){
        G2[start][to] = G2[start][pos] + G1[pos][i].cost;
        que.push(P(G2[start][to], to));
      }
    }
  }
}

int main(void){
  int verts, edge, envelop, start; cin >> verts>> edge>> envelop>> start; start--;
  vector< vector<Edge> > G1(verts);
  vector< vector<int> > G2(verts, vector<int>(verts, INF));
  vector<int> S(envelop);
  vector<int> T(envelop);
  for(int i = 0; i < edge; i++){
    int x, y, w; cin >> x >> y >> w;
    x--; y--;
    G1[x].push_back( Edge(y, w) );
    G1[y].push_back( Edge(x, w) );
  }
  for(int i = 0; i < envelop; i++){
    cin >> S[i] >> T[i];
    S[i]--; T[i]--;
  }

  dijkstra(start, G1, G2);
  for(int i = 0; i < envelop; i++){
    dijkstra(S[i], G1, G2);
    dijkstra(T[i], G1, G2);
  }

  /*
  for(int i = 0; i < verts; i++){
    for(int j = 0; j < verts; j++){
      cout << G2[i][j] << " ";
    }
    cout << endl;
  }
  */

  vector< vector<int> > G3(16, vector<int>(16, INF));
  for(int i = 0; i < envelop; i++){
    for(int j = i; j < envelop; j++){
      G3[i][j] = G3[j][i] = G2[S[i]][S[j]];
      G3[i][j+envelop] = G3[j+envelop][i] = G2[S[i]][T[j]];
      G3[i+envelop][j] = G3[j][i+envelop] = G2[T[i]][S[j]];
      G3[i+envelop][j+envelop] = G3[j+envelop][i+envelop] = G2[T[i]][T[j]];
    }
  }

  /*
  for(int i = 0; i < 16; i++){
    for(int j = 0; j < 16; j++){
      cout << G3[i][j] << " ";
    }
    cout << endl;
  }
  */

  vector< vector<int> > dp(1<<16, vector<int>(16, INF));
  for(int i = 0; i < envelop; i++){
    dp[1<<i][i] = G2[start][S[i]];
  }

  for(int i = 0; i < (1<<(2*envelop)); i++){
    for(int j = 0; j < 2*envelop; j++){
      if(dp[i][j] != INF){
        for(int k = 0; k < 2*envelop; k++){
          if(G3[j][k] != INF && (k < envelop || ((i>>(k-envelop))&1))){
            
              if(dp[i|(1<<k)][k] > dp[i][j] + G3[j][k]){
                dp[i|(1<<k)][k] = dp[i][j] + G3[j][k];
              }
          }
        }
      }
    }
  }

  /*
  for(int i = 0; i < (1<<16); i++){
    for(int j = 0; j < 16; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */

  int ans = INF;
  for(int i = 0; i < 2*envelop; i++){
    int v = dp[(1<<(2*envelop))-1][i];
    ans = min(ans, v);
  }
  if(ans == INF) cout << "Cannot deliver" << endl;
  else cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 13688
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
#define TO first
#define COST second
vector<P> G[1111];
int dp[1<<6][1<<6][1111];
struct st{
  int b1,b2,p,d;
  st(){}
  st(int b1,int b2,int p,int d):b1(b1),b2(b2),p(p),d(d){}
  bool operator<(st a)const{
    return d>a.d;
  }
};
signed main(){
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  p--;
  for(int i=0,x,y,w;i<m;i++){
    cin>>x>>y>>w;
    x--;y--;
    G[x].push_back(P(y,w));
    G[y].push_back(P(x,w));
  }
  int s[k],t[k];
  for(int i=0;i<k;i++){
    cin>>s[i]>>t[i];
    s[i]--;
    t[i]--;
  }
  memset(dp,-1,sizeof(dp));
  priority_queue<st> q;
  q.push(st(0,0,p,0));
  dp[0][0][p]=0;
  while(!q.empty()){
    st pp=q.top();q.pop();
    int b1=pp.b1,b2=pp.b2,v=pp.p,d=pp.d;
    if(dp[b1][b2][v]<d) continue;
    for(int i=0;i<(int)G[v].size();i++){
      int u=G[v][i].TO,c=G[v][i].COST;
      int nb1=b1,nb2=b2;
      for(int j=0;j<k;j++){
	if(s[j]==u) nb1|=(1<<j);
	if(((b1>>j)&1)&&(t[j]==u))
	  nb2|=(1<<j);
      }
      if(dp[nb1][nb2][u]<0||dp[nb1][nb2][u]>d+c){
	dp[nb1][nb2][u]=d+c;
	q.push(st(nb1,nb2,u,d+c));
      }
    }
  }
  int ans=-1;
  for(int i=0;i<n;i++){
    if(dp[(1<<k)-1][(1<<k)-1][i]<0) continue;
    if(ans<0||ans>dp[(1<<k)-1][(1<<k)-1][i])
      ans=dp[(1<<k)-1][(1<<k)-1][i];
  }
  if(ans<0) cout<<"Cannot deliver"<<endl;
  else cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 42632
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct edge {
  int to, cost;
  edge(){}
  edge(int to, int cost):to(to), cost(cost){}
};
#define MAX_N 1000

const int inf = 1<<25;

vector<edge> graph[MAX_N];
int mincost[1<<12][MAX_N];
int S[MAX_N], T[MAX_N];
int n, m, k, p;

int dijkstra() {
  fill(mincost[0], mincost[1<<12], inf);
  typedef tuple<int, int, int> Tpl;
  priority_queue<Tpl, vector<Tpl>, greater<Tpl> > que;
  mincost[0][p] = 0;
  que.emplace(0, p, 0);
  while(que.size()) {
    int cost, now, bit;
    tie(cost, now, bit) = que.top(); que.pop();
    if(bit == (1<<(2*k))-1) return cost;
    for(edge e : graph[now]) {
      int nbit = bit;
      if(S[e.to] != -1) nbit |= 1<<(S[e.to]*2);
      if(T[e.to] != -1 && (bit>>(T[e.to]*2))&1)	nbit |= 1<<(T[e.to]*2+1);
      if(e.cost + cost < mincost[nbit][e.to]) {
	mincost[nbit][e.to] = e.cost + cost;
	que.emplace(mincost[nbit][e.to], e.to, nbit);
      }
    }
  }
  return -1;
}
int main(){
  cin >> n >> m >> k >> p; --p;
  for(int i = 0; i < m; i++) {
    int x, y, w;
    cin >> x >> y >> w; --x, --y;
    graph[x].emplace_back(y, w);
    graph[y].emplace_back(x, w);
  }
  memset(S, -1, sizeof(S));
  memset(T, -1, sizeof(S));
  for(int i = 0; i < k; i++) {
    int s, t;
    cin >> s >> t; --s, --t;
    S[s] = i;
    T[t] = i;
  }
  int ans = dijkstra();
  if(ans == -1) cout << "Cannot deliver" <<endl;
  else cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 20632
final_rank: 3

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Edge { int to, cost; };
bool operator<(const Edge& e1, const Edge& e2) { return e1.cost < e2.cost; }
int N, M, Z, p, a, b, w, s[7], t[7], y[15], p1[7], p2[7], r[15], x[2][7], d[1111][1111]; vector<Edge> G[1111];
int main() {
	cin >> N >> M >> Z >> p; p--; r[0] = p;
	for (int i = 0; i < M; i++) {
		cin >> a >> b >> w; a--, b--;
		G[a].push_back(Edge{ b, w });
		G[b].push_back(Edge{ a, w });
	}
	for (int i = 0; i < Z; i++) {
		cin >> s[i] >> t[i]; s[i]--, t[i]--;
	}
	y[0] = p;
	for (int i = 0; i < Z; i++) y[i + 1] = s[i];
	for (int i = 0; i < Z; i++) y[i + Z + 1] = t[i];
	for (int g = 0; g < Z * 2 + 1; g++) {
		int i = y[g];
		priority_queue<Edge> que; que.push(Edge{ i, 0 });
		fill(d[i], d[i] + N, 99999999); d[i][i] = 0;
		while (!que.empty()) {
			int v = que.top().to; que.pop();
			for (auto &e : G[v]) {
				if (d[i][e.to] > d[i][v] + e.cost) {
					d[i][e.to] = d[i][v] + e.cost;
					que.push(Edge{ e.to, d[i][e.to] });
				}
			}
		}
	}
	int ret = 99999999;
	for (int i = 0; i < (1 << (2 * Z)); i++) {
		int dep = 0; bool flag = true;
		for (int j = 0; j < 2 * Z; j++) {
			if (i & (1 << j)) dep--;
			else dep++;
			if (dep < 0) flag = false;
		}
		if (dep != 0) flag = false;
		if (!flag) continue;
		for (int j = 0; j < Z; j++) p1[j] = p2[j] = j;
		do {
			int cr1 = 0;
			for (int j = 0; j < 2 * Z; j++) {
				if (!(i & (1 << j))) {
					x[0][p1[cr1]] = j;
					r[j + 1] = s[p1[cr1++]];
				}
			}
			do {
				int cr2 = 0;
				for (int j = 0; j < 2 * Z; j++) {
					if (i & (1 << j)) {
						x[1][p2[cr2]] = j;
						r[j + 1] = t[p2[cr2++]];
					}
				}
				bool e = true;
				for (int j = 0; j < Z; j++) {
					if (x[0][j] > x[1][j]) {
						e = false;
						break;
					}
				}
				if (!e) continue;
				bool ok = true;
				int res = 0;
				for (int j = 0; j < 2 * Z; j++) {
					res += d[r[j]][r[j + 1]];
				}
				ret = min(ret, res);
			} while (next_permutation(p2, p2 + Z));
		} while (next_permutation(p1, p1 + Z));
	}
	if (ret == 99999999) cout << "Cannot deliver" << endl;
	else cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 1360
memory_kb: 3308
final_rank: 14

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Edge { int to, cost; };
bool operator<(const Edge& e1, const Edge& e2) { return e1.cost < e2.cost; }
int N, M, Z, p, a, b, w, s[7], t[7], y[15], p1[7], p2[7], r[15], x[2][7], d[1111][1111]; vector<Edge> G[1111];
int main() {
	cin >> N >> M >> Z >> p; p--; r[0] = p;
	for (int i = 0; i < M; i++) {
		cin >> a >> b >> w; a--, b--;
		G[a].push_back(Edge{ b, w });
		G[b].push_back(Edge{ a, w });
	}
	for (int i = 0; i < Z; i++) {
		cin >> s[i] >> t[i]; s[i]--, t[i]--;
	}
	y[0] = p;
	for (int i = 0; i < Z; i++) y[i + 1] = s[i];
	for (int i = 0; i < Z; i++) y[i + Z + 1] = t[i];
	for (int g = 0; g < Z * 2 + 1; g++) {
		int i = y[g];
		priority_queue<Edge> que; que.push(Edge{ i, 0 });
		fill(d[i], d[i] + N, 999999999); d[i][i] = 0;
		while (!que.empty()) {
			int v = que.top().to; que.pop();
			for (auto &e : G[v]) {
				if (d[i][e.to] > d[i][v] + e.cost) {
					d[i][e.to] = d[i][v] + e.cost;
					que.push(Edge{ e.to, d[i][e.to] });
				}
			}
		}
	}
	long long ret = 999999999;
	for (int i = 0; i < (1 << (2 * Z)); i++) {
		int dep = 0; bool flag = true;
		for (int j = 0; j < 2 * Z; j++) {
			if (i & (1 << j)) dep--;
			else dep++;
			if (dep < 0) flag = false;
		}
		if (dep != 0) flag = false;
		if (!flag) continue;
		for (int j = 0; j < Z; j++) p1[j] = p2[j] = j;
		do {
			int cr1 = 0;
			for (int j = 0; j < 2 * Z; j++) {
				if (!(i & (1 << j))) {
					x[0][p1[cr1]] = j;
					r[j + 1] = s[p1[cr1++]];
				}
			}
			do {
				int cr2 = 0;
				for (int j = 0; j < 2 * Z; j++) {
					if (i & (1 << j)) {
						x[1][p2[cr2]] = j;
						r[j + 1] = t[p2[cr2++]];
					}
				}
				bool e = true;
				for (int j = 0; j < Z; j++) {
					if (x[0][j] > x[1][j]) {
						e = false;
						break;
					}
				}
				if (!e) continue;
				bool ok = true;
				long long res = 0;
				for (int j = 0; j < 2 * Z; j++) {
					res += d[r[j]][r[j + 1]];
				}
				ret = min(ret, res);
			} while (next_permutation(p2, p2 + Z));
		} while (next_permutation(p1, p1 + Z));
	}
	if (ret == 999999999) cout << "Cannot deliver" << endl;
	else cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 1360
memory_kb: 3316
final_rank: 15

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Edge { int to, cost; };
bool operator<(const Edge& e1, const Edge& e2) { return e1.cost < e2.cost; }
int N, M, Z, p, a, b, w, s[7], t[7], y[15], p1[7], p2[7], r[15], x[2][7], d[1111][1111]; vector<Edge> G[1111];
int main() {
	cin >> N >> M >> Z >> p; p--; r[0] = p;
	for (int i = 0; i < M; i++) {
		cin >> a >> b >> w; a--, b--;
		G[a].push_back(Edge{ b, w });
		G[b].push_back(Edge{ a, w });
	}
	for (int i = 0; i < Z; i++) {
		cin >> s[i] >> t[i]; s[i]--, t[i]--;
	}
	y[0] = p;
	for (int i = 0; i < Z; i++) y[i + 1] = s[i];
	for (int i = 0; i < Z; i++) y[i + Z + 1] = t[i];
	for (int g = 0; g < Z * 2 + 1; g++) {
		int i = y[g];
		priority_queue<Edge> que; que.push(Edge{ i, 0 });
		fill(d[i], d[i] + N, 999999999); d[i][i] = 0;
		while (!que.empty()) {
			int v = que.top().to; que.pop();
			for (auto &e : G[v]) {
				if (d[i][e.to] > d[i][v] + e.cost) {
					d[i][e.to] = d[i][v] + e.cost;
					que.push(Edge{ e.to, d[i][e.to] });
				}
			}
		}
	}
	long long ret = 999999999;
	for (int i = 0; i < (1 << (2 * Z)); i++) {
		int dep = 0; bool flag = true;
		for (int j = 0; j < 2 * Z; j++) {
			if (i & (1 << j)) dep--;
			else dep++;
			if (dep < 0) flag = false;
		}
		if (dep != 0) flag = false;
		if (!flag) continue;
		for (int j = 0; j < Z; j++) p1[j] = p2[j] = j;
		do {
			int cr1 = 0;
			for (int j = 0; j < 2 * Z; j++) {
				if (!(i & (1 << j))) {
					x[0][p1[cr1]] = j;
					r[j + 1] = s[p1[cr1++]];
				}
			}
			do {
				int cr2 = 0;
				for (int j = 0; j < 2 * Z; j++) {
					if (i & (1 << j)) {
						x[1][p2[cr2]] = j;
						r[j + 1] = t[p2[cr2++]];
					}
				}
				bool e = true;
				for (int j = 0; j < Z; j++) {
					if (x[0][j] > x[1][j]) {
						e = false;
						break;
					}
				}
				if (!e) continue;
				bool ok = true;
				long long res = 0;
				for (int j = 0; j < 2 * Z; j++) {
					if (d[r[j]][r[j] + 1] == 999999999) ok = false;
					res += d[r[j]][r[j + 1]];
				}
				ret = min(ret, res);
			} while (next_permutation(p2, p2 + Z));
		} while (next_permutation(p1, p1 + Z));
	}
	if (ret == 999999999) cout << "Cannot deliver" << endl;
	else cout << ret << endl;
	return 0;
}
accuracy: 1
time_ms: 1340
memory_kb: 3328
final_rank: 13

code_snippet: #include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Edge { int to, cost; };
bool operator<(const Edge& e1, const Edge& e2) { return e1.cost < e2.cost; }
int N, M, Z, p, a, b, w, s[7], t[7], y[15], p1[7], p2[7], r[15], x[2][7], d[1111][1111]; vector<Edge> G[1111];
int main() {
	cin >> N >> M >> Z >> p; p--; r[0] = p;
	for (int i = 0; i < M; i++) {
		cin >> a >> b >> w; a--, b--;
		G[a].push_back(Edge{ b, w });
		G[b].push_back(Edge{ a, w });
	}
	for (int i = 0; i < Z; i++) {
		cin >> s[i] >> t[i]; s[i]--, t[i]--;
	}
	y[0] = p;
	for (int i = 0; i < Z; i++) y[i + 1] = s[i];
	for (int i = 0; i < Z; i++) y[i + Z + 1] = t[i];
	for (int g = 0; g < Z * 2 + 1; g++) {
		int i = y[g];
		priority_queue<Edge> que; que.push(Edge{ i, 0 });
		fill(d[i], d[i] + N, 999999999); d[i][i] = 0;
		while (!que.empty()) {
			int v = que.top().to; que.pop();
			for (auto &e : G[v]) {
				if (d[i][e.to] > d[i][v] + e.cost) {
					d[i][e.to] = d[i][v] + e.cost;
					que.push(Edge{ e.to, d[i][e.to] });
				}
			}
		}
	}
	int ret = 999999999;
	for (int i = 0; i < (1 << (2 * Z)); i++) {
		int dep = 0; bool flag = true;
		for (int j = 0; j < 2 * Z; j++) {
			if (i & (1 << j)) dep--;
			else dep++;
			if (dep < 0) flag = false;
		}
		if (dep != 0) flag = false;
		if (!flag) continue;
		for (int j = 0; j < Z; j++) p1[j] = p2[j] = j;
		do {
			int cr1 = 0;
			for (int j = 0; j < 2 * Z; j++) {
				if (!(i & (1 << j))) {
					x[0][p1[cr1]] = j;
					r[j + 1] = s[p1[cr1++]];
				}
			}
			do {
				int cr2 = 0;
				for (int j = 0; j < 2 * Z; j++) {
					if (i & (1 << j)) {
						x[1][p2[cr2]] = j;
						r[j + 1] = t[p2[cr2++]];
					}
				}
				bool e = true;
				for (int j = 0; j < Z; j++) {
					if (x[0][j] > x[1][j]) {
						e = false;
						break;
					}
				}
				if (!e) continue;
				bool ok = true;
				int res = 0;
				for (int j = 0; j < 2 * Z; j++) {
					if (d[r[j]][r[j] + 1] == 999999999) ok = false;
					res += d[r[j]][r[j + 1]];
				}
				if(ok) ret = min(ret, res);
			} while (next_permutation(p2, p2 + Z));
		} while (next_permutation(p1, p1 + Z));
	}
	if (ret == 999999999) cout << "Cannot deliver" << endl;
	else cout << ret << endl;
	return 0;
}
accuracy: 0.14545454545454545
time_ms: 1190
memory_kb: 3276
final_rank: 20