Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define isz(x) (int)(x).size()
  3. struct Edge {
  4.     int u, v, cost;
  5. };
  6. const int NMAX = 20020, INF = (int)1e9+7;
  7. int nV;
  8. std::vector<Edge> edges;
  9. std::vector<int> adj[NMAX];
  10. int dist[NMAX], from[NMAX];
  11. void find_path(int s, int t, bool first) {
  12.     std::fill(dist,dist+NMAX,INF);
  13.     std::fill(from,from+NMAX, -1);
  14.     std::deque<int> q = {s};
  15.     dist[s] = 0;
  16.     while (isz(q)) {
  17.         auto u = q.front(); q.pop_front();
  18.         for (int i : adj[u]) {
  19.             const auto & e = edges[i];
  20.             int v = e.u + e.v - u;
  21.             if (dist[v] > dist[u] + e.cost) {
  22.                 if (e.cost == 0 && !first) { q.push_front(v); }
  23.                 else { q.push_back(v); }
  24.                 dist[v] = dist[u] + e.cost;
  25.                 from[v] = i;
  26.             }
  27.         }
  28.     }
  29.     while (from[t] != -1) {
  30.         int id = from[t];
  31.         auto &e = edges[id];
  32.         e.cost++;
  33.         t = e.u + e.v - t;
  34.     }
  35. }
  36.  
  37. int main() {
  38.     std::ios_base::sync_with_stdio(false);
  39.     std::cin.tie(0);
  40.     freopen("inevit.in", "rt", stdin);
  41.     freopen("inevit.out", "wt", stdout);
  42.     int nE; std::cin >> nV >> nE;
  43.     for (int i = 0, u, v; i < nE; i++) {
  44.         std::cin >> u >> v;
  45.         edges.push_back(Edge{u,v,0});
  46.         adj[u].push_back(i);
  47.         adj[v].push_back(i);
  48.     }
  49.     find_path(1, nV, 1);
  50.     find_path(1, nV, 0);
  51.     int t = nV;
  52.     std::vector<int> answ;
  53.     while (from[t] != -1) {
  54.         int id = from[t];
  55.         const auto &e = edges[id];
  56.         if (e.cost == 2) {
  57.             answ.push_back(id + 1);
  58.         }
  59.         t = e.u + e.v - t;
  60.     }
  61.     std::reverse(answ.begin(),answ.end());
  62.     std::cout << answ.size() << '\n';
  63.     for (auto it : answ) std::cout << it << ' ';
  64.     std::cout << std::endl;
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement