zoukankan      html  css  js  c++  java
  • FUNDAMENTAL PART3 搜索与图论

    搜索与图论

    +++

    1.朴素版dijkstra (适用于稠密图)
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    const int N = 510;
    
    int g[N][N], dist[N];
    int n, m;
    bool st[N];
    
    int dijkstra()
    {
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        
        for (int i = 0; i < n - 1; i ++ )
        {
            int t = -1;
            for (int j = 1; j <= n; j ++ )
                if(!st[j] && (t == -1 || dist[t] > dist[j]))
                    t = j;
                
            for (int j = 1; j <= n; j ++ )
                dist[j] = min(dist[j], dist[t] + g[t][j]);
            
            st[t] = true;
        }
        
        if(dist[n] == 0x3f3f3f3f) return -1;
        else return dist[n];
    }
    
    int main()
    {
        memset(g, 0x3f, sizeof g);
        
        cin >> n >> m;
        
        while ( m -- )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            
            g[a][b] = min(g[a][b], c);
        }
        
        printf("%d
    ", dijkstra());
        
        return 0;
    }
    
    2.堆优化版的dijkstra (适用于稀疏图)
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    typedef pair<int, int> PII;
    
    const int N = 1e6 + 10;
    
    int n, m;
    int h[N], w[N], e[N], ne[N], idx;
    int dist[N];
    bool st[N];
    
    void add(int a, int b, int c)
    {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    int dijkstra()
    {
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        priority_queue<PII, vector<PII>, greater<PII>> heap;
        heap.push({0, 1});
    
        while (heap.size())
        {
            auto t = heap.top();
            heap.pop();
    
            int ver = t.second, distance = t.first;
    
            if (st[ver]) continue;
            st[ver] = true;
    
            for (int i = h[ver]; i != -1; i = ne[i])
            {
                int j = e[i];
                if (dist[j] > dist[ver] + w[i])
                {
                    dist[j] = dist[ver] + w[i];
                    heap.push({dist[j], j});
                }
            }
        }
    
        if (dist[n] == 0x3f3f3f3f) return -1;
        return dist[n];
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
    
        memset(h, -1, sizeof h);
        while (m -- )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
        }
    
        cout << dijkstra() << endl;
    
        return 0;
    }
    
    
    3.bellman_ford
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 510, M = 10010;
    
    struct Edge
    {
        int a, b, w;
    }edges[M];
    
    int dist[N], backup[N];
    int n, m, k;
    
    int bellman_ford()
    {
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        
        for (int i = 0; i < k; i ++ )
        {
            memcpy(backup, dist, sizeof dist);
            
            for (int j = 0; j < m; j ++ )
            {
                int a = edges[j].a, b = edges[j].b, w = edges[j].w;
                dist[b] = min(dist[b], backup[a] + w);
            }
        }
        
        if(dist[n] > 0x3f3f3f3f / 2) return -1;
        else return dist[n];
    }
    
    int main()
    {
        scanf("%d%d%d", &n, &m, &k);
        
        for (int i = 0; i < m; i ++ )
        {
            int a, b, w;
            scanf("%d%d%d", &a, &b, &w);
            edges[i] = {a, b, w};
        }
        
        int t = bellman_ford();
        
        if(t == -1) puts("impossible");
        else printf("%d
    ", t);
        
        return 0;
    }
    
    4.spfa

    借助队列,只将dist数值修改了的结点入队

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    const int N = 100010;
    
    int m, n;
    int h[N], w[N], e[N], ne[N], idx;
    int dist[N];
    bool st[N];
    
    void add(int a, int b, int c)
    {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    int spfa()
    {
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        
        queue<int> q;
        q.push(1);
        st[1] = true;
        
        while(q.size())
        {
            auto t = q.front();
            q.pop();
            
            st[t] = false;
            
            for (int i = h[t]; i != -1; i = ne[i])
            {
                int j = e[i];
                if(dist[j] > dist[t] + w[i])
                {
                    dist[j] = dist[t] + w[i];
                    if(!st[j])
                    {
                        q.push(j);
                        st[j] = true;
                    }
                }
            }
        }
        
        if(dist[n] == 0x3f3f3f3f) return -1;
        else return dist[n];
    }
    
    int main()
    {
        cin >> n >> m;
        
        memset(h, -1, sizeof h);
        
        for (int i = 0; i < m; i ++ )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
        }
        
        int t = spfa();
        
        if(t == -1) puts("impossible");
        else printf("%d
    ", t);
        
        return 0;
    }
    
    spfa判断负环
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    const int N = 2010, M = 10010;
    
    int n, m;
    int h[N], w[M], e[M], ne[M], idx;
    int dist[N], cnt[N];
    bool st[N];
    
    void add(int a, int b, int c)
    {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    bool spfa()
    {
        queue<int> q;
    
        for (int i = 1; i <= n; i ++ )
        {
            st[i] = true;
            q.push(i);
        }
    
        while (q.size())
        {
            int t = q.front();
            q.pop();
    
            st[t] = false;
    
            for (int i = h[t]; i != -1; i = ne[i])
            {
                int j = e[i];
                if (dist[j] > dist[t] + w[i])
                {
                    dist[j] = dist[t] + w[i];
                    cnt[j] = cnt[t] + 1;
    
                    if (cnt[j] >= n) return true;
                    if (!st[j])
                    {
                        q.push(j);
                        st[j] = true;
                    }
                }
            }
        }
    
        return false;
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
    
        memset(h, -1, sizeof h);
    
        while (m -- )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
        }
    
        if (spfa()) puts("Yes");
        else puts("No");
    
        return 0;
    }
    
    5.spfa
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 210, INF = 1e9;
    
    int d[N][N];
    int n, m ,Q;
    
    void floyd()
    {
        for (int k = 1; k <= n; k ++ )
            for (int i = 1; i <= n; i ++ )
                for (int j = 1; j <= n; j ++ )
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    }
    
    int main()
    {
        scanf("%d%d%d", &n, &m, &Q);
        
        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= n; j ++ )
                if(i == j) d[i][j] = 0;
                else d[i][j] = INF;
        
        while (m -- )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            d[a][b] = min(d[a][b], c);
        }
        
        floyd();
        
        while (Q -- )
        {
            int a, b;
            scanf("%d%d", &a, &b);
            
            if(d[a][b] > INF / 2) puts("impossible");
            else printf("%d
    ", d[a][b]);
        }
        
        return 0;
    }
    
    6.朴素版的prim
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    const int N = 510, INF = 0x3f3f3f3f;
    
    int g[N][N], dist[N];
    bool st[N];
    int n, m;
    
    int prim()
    {
        memset(dist, 0x3f, sizeof dist);
        
        int res = 0;
        for (int i = 0; i < n; i ++ )
        {
            int t = -1;
            for (int j = 1; j <= n; j ++ )
                if(!st[j] && (t == -1 || dist[t] > dist[j]))
                    t = j;
            
            if(i && dist[t] == INF) return INF;
            
            if(i) res += dist[t];
            st[t] = true;
            
            for (int j = 1; j <= n; j ++ ) dist[j] = min(dist[j], g[t][j]);
        }
        
        return res;
    }
    
    int main()
    {
        memset(g, 0x3f, sizeof g);
        
        scanf("%d%d", &n, &m);
        while (m -- )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            
            g[a][b] = g[b][a] = min(g[a][b], c);
        }
        
        int t = prim();
        
        if(t == INF) puts("impossible");
        else printf("%d
    ", t);
        
        return 0;
    }
    
    7.Kruskal
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    const int N = 100010, M = 200010, INF = 0x3f3f3f3f;
    
    struct Edge
    {
        int a, b, w;
        
        bool operator< (const Edge & W)const
        {
            return w < W.w;
        }
    }edges[M];
    
    int p[N];
    int n, m;
    
    int find(int x)
    {
        if(p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
    int Kruskal()
    {
        sort(edges, edges + m);
        
        for (int i = 1; i <= n; i ++ ) p[i] = i;
        
        int res = 0, cnt = 0;
        for (int i = 0; i < m; i ++ )
        {
            int a = edges[i].a, b = edges[i].b, w = edges[i].w;
            
            a = find(a), b = find(b);
            if(a != b)
            {
                p[a] = b;
                res += w;
                cnt ++ ;
            }
        }
        
        if(cnt < n - 1) return INF;
        return res;
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        
        for (int i = 0; i < m; i ++ )
        {
            int a, b, w;
            scanf("%d%d%d", &a, &b, &w);
            edges[i] = {a, b, w};
        }
        
        int t = Kruskal();
        
        if(t == INF) puts("impossible");
        else printf("%d
    ", t);
        
        return 0;
    }
    
    8.染色法判断二分图
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int N = 100010, M = 200010;
    
    int h[N], e[M], ne[M], idx;
    int color[N];
    int n, m;
    
    void add(int a, int b)
    {
        e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    bool dfs(int u, int c)
    {
        color[u] = c;
        
        for (int i = h[u]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(!color[j])
            {
                if(!dfs(j, 3 - c)) return false;
            }
            else if(color[j] == c) return false;
        }
        
        return true;
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        
        memset(h, -1, sizeof h);
        
        for (int i = 0; i < m; i ++ )
        {
            int a, b;
            scanf("%d%d", &a, &b);
            
            add(a, b), add(b, a);
        }
        
        bool flag = true;
        for (int i = 1; i <= n; i ++ )
            if(!color[i])
            {
                if(!dfs(i, 1))
                {
                    flag = false;
                    break;
                }
            }
        
        if(flag) puts("Yes");
        else puts("No");
        
        return 0;
    }
    
    9.匈牙利算法求二分图最大匹配
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    const int N = 510, M = 100010;
    
    int n1, n2, m;
    int h[N], e[M], ne[M], idx;
    int match[N];
    bool st[N];
    
    void add(int a, int b)
    {
        e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    bool find(int x)
    {
        for (int i = h[x]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (!st[j])
            {
                st[j] = true;
                if (match[j] == 0 || find(match[j]))
                {
                    match[j] = x;
                    return true;
                }
            }
        }
    
        return false;
    }
    
    int main()
    {
        scanf("%d%d%d", &n1, &n2, &m);
        
        memset(h, -1, sizeof h);
        
        while (m -- )
        {
            int a, b;
            scanf("%d%d", &a, &b);
            add(a, b);
        }
        
        int res = 0;
        for (int i = 1; i <= n1; i ++ )
        {
            memset(st, false, sizeof st);
            if(find(i)) res ++ ;
        }
        
        printf("%d
    ", res);
        
        return 0;
    }
    
  • 相关阅读:
    java Thread之ThreadLocal(线程局部变量)
    java设计模式之接口隔离原则(ISP)
    java设计模式之开放关闭原则(OCP)
    java设计模式之迪米特法则(LoD)
    java设计模式之单一职责原则(SRP)
    android点滴(25)之 originalpackage
    VC 注册表操作
    java设计模式之依赖倒置原则(DIP)
    DFT 离散傅里叶变换 与 补零运算
    序列循环移位
  • 原文地址:https://www.cnblogs.com/scl0725/p/13870061.html
Copyright © 2011-2022 走看看