Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std; typedef long long ll; typedef unsigned long long ull; const int mod = 1e9 + 7, N = 1010, inf = INT_MAX;
- #define fi first
- #define se second
- #define pb push_back
- #define mp make_pair
- #define ep emplace_back
- //#define int ll
- int n, m, ans, c[N], dist[N], visited[N];
- signed main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- while (cin >> n >> m)
- {
- ans = inf;
- vector<pair<int, int>>adj[N];
- vector<int>lon[N];
- for (int i = 1; i <= n; i++)
- {
- cin >> c[i];
- }
- for (int a = 0; a <= n; a++)
- {
- string s;
- getline(cin, s);
- stringstream z(s);
- int now, pre;
- z >> pre;
- lon[a].ep(pre);
- while (z >> now)
- {
- lon[a].ep(now);
- adj[a * 100 + pre].ep(100 * a + now, (now - pre)*c[a]);
- adj[a * 100 + now].ep(100 * a + pre, (now - pre)*c[a]);
- pre = now;
- }
- }
- for (int i = 0; i < 100; i++)
- {
- for (int j = 1; j <= n; j++)
- {
- for (int k = 1; k <= n; k++)
- {
- if (j == k) continue;
- if (binary_search(lon[j].begin(), lon[j].end(), i)
- && binary_search(lon[k].begin(), lon[k].end(), i))
- {
- adj[100 * j + i].ep(100 * k + i, 60);
- }
- }
- }
- }
- for (int st = 100; st <= n * 100; st += 100)
- {
- memset(visited, 0, sizeof visited);
- fill(dist, dist + N, inf);
- priority_queue<pair<int, int>>q;
- q.emplace(0, st);
- dist[st] = 0;
- while (!q.empty())
- {
- int s = q.top().se; q.pop();
- if (visited[s]) continue; visited[s] = 1;
- for (auto u : adj[s])
- {
- if (dist[u.fi] > dist[s] + u.se)
- {
- dist[u.fi] = dist[s] + u.se;
- q.emplace(-dist[u.fi], u.fi);
- }
- }
- }
- for (int i = 1; i <= n; i++)
- {
- ans = min(ans, dist[100 * i + m]);
- }
- }
- if (ans != inf) cout << ans << endl;
- else cout << "IMPOSSIBLE\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement