Advertisement
UNoobAle

UVA 10801

Jan 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std; typedef long long ll; typedef unsigned long long ull; const int mod = 1e9 + 7, N = 1010, inf = INT_MAX;
  3. #define fi first
  4. #define se second
  5. #define pb push_back
  6. #define mp make_pair
  7. #define ep emplace_back
  8. //#define int ll
  9.  
  10. int n, m, ans, c[N], dist[N], visited[N];
  11.  
  12. signed main()
  13. {
  14.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  15.     while (cin >> n >> m)
  16.     {
  17.         ans = inf;
  18.         vector<pair<int, int>>adj[N];
  19.         vector<int>lon[N];
  20.         for (int i = 1; i <= n; i++)
  21.         {
  22.             cin >> c[i];
  23.         }
  24.         for (int a = 0; a <= n; a++)
  25.         {
  26.             string s;
  27.             getline(cin, s);
  28.             stringstream z(s);
  29.             int now, pre;
  30.             z >> pre;
  31.             lon[a].ep(pre);
  32.             while (z >> now)
  33.             {
  34.                 lon[a].ep(now);
  35.                 adj[a * 100 + pre].ep(100 * a + now, (now - pre)*c[a]);
  36.                 adj[a * 100 + now].ep(100 * a + pre, (now - pre)*c[a]);
  37.                 pre = now;
  38.             }
  39.         }
  40.         for (int i = 0; i < 100; i++)
  41.         {
  42.             for (int j = 1; j <= n; j++)
  43.             {
  44.                 for (int k = 1; k <= n; k++)
  45.                 {
  46.                     if (j == k) continue;
  47.                     if (binary_search(lon[j].begin(), lon[j].end(), i)
  48.                         && binary_search(lon[k].begin(), lon[k].end(), i))
  49.                     {
  50.                         adj[100 * j + i].ep(100 * k + i, 60);
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         for (int st = 100; st <= n * 100; st += 100)
  56.         {
  57.             memset(visited, 0, sizeof visited);
  58.             fill(dist, dist + N, inf);
  59.             priority_queue<pair<int, int>>q;
  60.             q.emplace(0, st);
  61.             dist[st] = 0;
  62.             while (!q.empty())
  63.             {
  64.                 int s = q.top().se; q.pop();
  65.                 if (visited[s]) continue; visited[s] = 1;
  66.                 for (auto u : adj[s])
  67.                 {
  68.                     if (dist[u.fi] > dist[s] + u.se)
  69.                     {
  70.                         dist[u.fi] = dist[s] + u.se;
  71.                         q.emplace(-dist[u.fi], u.fi);
  72.                     }
  73.                 }
  74.             }
  75.             for (int i = 1; i <= n; i++)
  76.             {
  77.                 ans = min(ans, dist[100 * i + m]);
  78.             }
  79.         }
  80.         if (ans != inf) cout << ans << endl;
  81.         else cout << "IMPOSSIBLE\n";
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement