#include #include #include using namespace std; typedef vector BolVec; typedef vector IntVec; typedef vector IntMat; typedef queue IntQue; IntMat adj(500000); int n, k, a, b; int bfs(bool anew) { // 'inc' is the price of the cheaper trains; 'max' is the other int inc = anew ? b : a; int mx = anew ? a : b; IntVec cost(n, -1); cost[0] = 0; IntQue que; que.push(0); while (!que.empty()) { int city = que.front(); que.pop(); int c = cost[city] + inc; // return if the journey got too expensive if (c >= mx) return -1; // make sure we only get on the cheap trains BolVec avail(n, anew); avail[city] = false; for (int i = 0; i < (int)adj[city].size(); ++i) avail[adj[city][i]] = !anew; // set the cost for newly visited stations // return when we get to destination for (int i = 0; i < n; ++i) { if (avail[i]) { if (i == n - 1) return c; else if (cost[i] == -1) { cost[i] = c; que.push(i); } } } } return -1; } int main() { //freopen("C:\\in.txt", "r", stdin); //ios_base::sync_with_stdio(false); while (scanf("%d %d %d %d", &n, &k, &a, &b)==4) { // read new trains into adj matrix // 'anew' tells if the direct train has new price bool anew = false; for (int i = 0; i < n; ++i) adj[i].clear(); for (int i = 0; i < k; ++i) { int x, y; scanf("%d %d", &x, &y);//cin >> x >> y; if ((x == 1 && y == n) || (y == 1 && x == n)) anew = true; --x; --y; adj[x].push_back(y); adj[y].push_back(x); } // if the cost of the direct train is the cheapest, take it! int cheap = a < b ? a : b; int cost = anew ? a : b; if (cost == cheap) printf("%d\n", cheap);//cout << cheap << endl; // otherwise, find the shortest path using the cheaper trains else { int c = bfs(anew); printf("%d\n", (c == -1 ? cost : c));// cout << (c == -1 ? cost : c) << endl; } } return 0; }