Advertisement
K_Y_M_bl_C

Untitled

Dec 23rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. int solve()
  2. {
  3.     int n, m;
  4.     scanf("%d %d", &n, &m);
  5.     vector<vector<pii>> g(n);
  6.     for (int i = 0; i < m; ++i)
  7.     {
  8.         int x, y, c;
  9.         scanf("%d %d %d", &x, &y, &c);
  10.         --x, --y;
  11.         g[x].inb(mk(y, c));
  12.         g[y].inb(mk(x, c));
  13.     }
  14.     vector<vi> d(n, vi(9, 0));
  15.     vi dst(n, INF);
  16.     set<pair<pair<int, vi>, int>> q;
  17.     dst[0] = 0;
  18.     q.insert(mk(mk(0, d[0]), 0));
  19.     while (!q.empty())
  20.     {
  21.         vi cur = q.begin()->X.Y;
  22.         int u = q.begin()->Y;
  23.         q.erase(*q.begin());
  24.         for (auto tt : g[u])
  25.         {
  26.             int to = tt.X;
  27.             int c = tt.Y;
  28.             if (c == 0)
  29.             {
  30.                 if (dst[to] > dst[u] || dst[to] == dst[u] && d[to] > d[u])
  31.                 {
  32.                     q.erase(mk(mk(dst[to], d[to]), to));
  33.                     d[to] = cur;
  34.                     dst[to] = dst[u];
  35.                     q.insert(mk(mk(dst[to], d[to]), to));
  36.                 }
  37.             }
  38.             else
  39.             {
  40.                 cur[c - 1]++;
  41.                 if (dst[to] > dst[u] + 1 || dst[to] == dst[u] + 1 && d[to] > cur)
  42.                 {
  43.                     q.erase(mk(mk(dst[to], d[to]), to));
  44.                     d[to] = cur;
  45.                     dst[to] = dst[u] + 1;
  46.                     q.insert(mk(mk(dst[to], d[to]), to));
  47.                 }
  48.                 cur[c - 1]--;
  49.             }
  50.         }
  51.     }
  52.     vi ans = d[n - 1];
  53.     if (dst[n - 1] == 0)
  54.         puts("0"), exit(0);
  55.     for (int i = 0; i < 9; ++i)
  56.     {
  57.         for (int j = 0; j < ans[i]; ++j)
  58.             printf("%d", i + 1);
  59.     }
  60.     puts("");
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement