• 图论


    Travel

    The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n.

    Among n(n−1) / 2 pairs of towns, m of them are connected by bidirectional highway, which needs aa minutes to travel.

    The other pairs are connected by railway, which needs bb minutes to travel.

    Find the minimum time to travel from town 1 to town n.

    Input

    The input consists of multiple tests. For each test:

    The first line contains 4 integers n,m,a,bn,m,a,b (2≤n≤1e5,0≤m≤5⋅1e5,1≤a,b≤1e9).

    Each of the following mm lines contains 22 integers ui,viui,vi, which denotes cities uiui and vivi are connected by highway. (1≤ui,vi≤n,ui≠vi).

    Output

    For each test, write 1 integer which denotes the minimum time.

    Sample Input

    3 2 1 3
    1 2
    2 3
    3 2 2 3
    1 2
    2 3
    Sample Output

    2




    -----------------------------------------我是分割线^_^---------------------------------


    这个题就是一个求补图的最短路的很好的例子,由于在这个题中补图的权值一样,所以就可以和用
    BFS求普通的最短路一样的求解了,至于结构体的方式来储存邻接表还是第一次,不过感觉挺方便的
    所以以后还是尽量习惯吧,对了,还要注意理解题解中set的作用,用来筛选出与当前点未连接的点。


    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<vector>
    #include<cctype>
    #include<set>
    #include<map>
    #include<sstream>
    using namespace std;

    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define INF 0x3f3f3f3f
    #define Int __int64
    #define pii pair<int,int>
    #define check(x) cout<<"["<<x<<"]"<<endl;

    const int MAXN = 5555555;
    int head[MAXN];
    bool vis[MAXN];
    int dis[MAXN];

    struct Node {
        int v;
        int w;
        int nxt;
    } edge[MAXN];

    long long a, b;
    int n, m;
    int edgecnt;

    void Add_Edge(int u, int v) {
        edge[edgecnt].v = v;
        edge[edgecnt].w = a;
        edge[edgecnt].nxt = head[u];
        head[u] = edgecnt++;
    }

    long long Spfa() {
        memset(vis, false, sizeof(vis));
        memset(dis, 0x3f, sizeof(dis));//记得是初始化为无穷大= =
        dis[1] = 0;
        queue<int>q;
        while (!q.empty()) q.pop();
        vis[1] = true;
        q.push(1);
        while (!q.empty()) {
            int now = q.front();
            q.pop();
            for (int i = head[now]; i != -1; i = edge[i].nxt) {
                int v = edge[i].v;
                int w = edge[i].w;
                if (dis[v] > dis[now] + w) {
                    dis[v] = dis[now] + w;
                    if (!vis[v]) {//标记这里还是有点疑问,因为有的人不标记也行= =
                        vis[v] = true;
                        q.push(v);
                    }
                }
            }
        }
        return dis[n] < b ? dis[n] : b;
    }

    long long Bfs() {
        dis[n] = INF;
        set<int>st, ts;
        st.clear();
        ts.clear();
        for (int i = 2; i <= n; i++) {
            st.insert(i);
        }
        queue<int>q;
        while (!q.empty()) q.pop();
        q.push(1);
        dis[1] = 0;
        while (!q.empty()) {
            int now = q.front();
            q.pop();
            for (int i = head[now]; i != -1; i = edge[i].nxt) {
                int v = edge[i].v;
                if (st.count(v) == 0) {
                    continue;
                }
                st.erase(v);
                ts.insert(v);
            }
            set<int>::iterator it = st.begin();
            for ( ; it != st.end(); it++) {
                q.push(*it);
                dis[*it] = dis[now] + 1;
            }
            st.swap(ts);
            ts.clear();
        }
        return dis[n] * b < a ? dis[n] * b : a;
    }

    int main() {
        //freopen("input.txt", "r", stdin);
        while (scanf("%d %d %lld %lld", &n, &m, &a, &b) != EOF) {
            edgecnt = 0;
            memset(head, -1, sizeof(head));
            int u, v;
            bool judge = false;
            for (int i = 0; i < m; i++) {
                scanf("%d %d", &u, &v);
                Add_Edge(u, v);
                Add_Edge(v, u);
                if ( (u == 1 && v == n) || (u == n && v == 1) ) {
                    judge = true;
                }
            }
            if (judge) {
                printf("%d ", Bfs());
            } else {
                printf("%d ", Spfa());
            }
        }
        return 0;
    }
  • 相关阅读:
    航空公司客户价值分析
    电力窃漏电用户自动识别
    信息论基础
    Xgboost
    直线或曲线拟合
    matplotlib中绘图
    pandas中绘图
    matplotlib图形设置
    子图subplot
    时间序列:时间序列理论、时间序列建模
  • 原文地址:https://www.cnblogs.com/steamedbun/p/5786710.html
Copyright © 2020-2023  润新知