Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- const int MAXN = 50;
- using namespace std;
- typedef long long ll;
- typedef pair<int, ll> pil;
- vector<pil> adj[3][MAXN];
- ll dist[MAXN][MAXN][MAXN][3];
- int p, n, m;
- typedef tuple<int, int, int, int, int> tp;
- ll djikstra(int e1, int e2, int e3){
- memset(dist, 0x3f, sizeof(dist));
- priority_queue<tp, vector<tp>, greater<tp>> pq;
- dist[0][0][0][0] = 0;
- pq.emplace(0, 0, 0, 0, 0);
- while(!pq.empty()){
- auto [d1, a, b, c, i] = pq.top(); pq.pop();
- if(d1 > dist[a][b][c][i]) continue;
- if(i == 0){
- for(auto [to, w]: adj[i][a]){
- ll d = dist[a][b][c][i] + w;
- if(d < dist[to][b][c][1]){
- dist[to][b][c][1] = d;
- pq.emplace(d, to, b, c, 1);
- }
- }
- }else if(i == 1){
- for(auto [to, w]: adj[i][b]){
- ll d = dist[a][b][c][i] + w;
- if(d < dist[a][to][c][2]){
- dist[a][to][c][2] = d;
- pq.emplace(d, a, to, c, 2);
- }
- }
- }else{
- for(auto [to, w]: adj[i][c]){
- ll d = dist[a][b][c][i] + w;
- if(d < dist[a][b][to][0]){
- dist[a][b][to][0] = d;
- pq.emplace(d, a, b, to, 0);
- }
- }
- }
- }
- return dist[e1][e2][e3][0];
- }
- int main (){
- cin.tie(0);
- cin.sync_with_stdio(0);
- cin >> p;
- vector<int> obj(3, 0);
- for(int k = 0; k < p; k++){
- cin >> n >> m;
- for(int i = 0; i < n; i++){
- int w;
- cin >> w;
- adj[k][i].emplace_back(i, w);
- }
- for(int i = 0; i < m; i++){
- int u, v;
- ll w;
- cin >> u >> v >> w;
- u--; v--;
- adj[k][u].emplace_back(v, w);
- }
- cin >> obj[k];
- obj[k]--;
- }
- for(int k = p; k < 3; k++){
- adj[k][0].emplace_back(0, 0);
- }
- cout << djikstra(obj[0], obj[1], obj[2]) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement