• 模板:Prime最小生成树堆优化 + Dijkstra单源最短路堆优化


    Dijkstra 单源最短路堆优化

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int, int> PII;
    const int N = 2e5 + 10;
    int head[N], to[N], value[N], nex[N], cnt = 1;
    int n, m, rt, st, dis[N], visit[N];
    struct cmp {
    	bool operator()(PII a, PII b) {
    		return a.second > b.second;
    	}
    };
    void add(int x, int y, int w) {
    	to[cnt] = y;
    	nex[cnt] = head[x];
    	value[cnt] = w;
    	head[x] = cnt++;
    }
    void Dijkstra() {
    	memset(dis, 0x3f, sizeof dis);
    	dis[rt] = 0;
    	priority_queue<PII, vector<PII>, cmp> q;
    	q.push(make_pair(rt, 0));
    	while(!q.empty()) {
    		int p = q.top().first;
    		q.pop();
    		if(visit[p])	continue;
    		visit[p] = 1;
    		for(int i = head[p]; i; i = nex[i]) {
    			if(dis[to[i]] > dis[p] + value[i]) {
    				dis[to[i]] = dis[p] + value[i];
    				q.push(make_pair(to[i], dis[p] + value[i]));
    			}
    		}
    	}
    	for(int i = 1; i <= n; i++)
    		printf("%d%c", dis[i] == 0x3f3f3f3f ? (int)((1ll << 31) - 1) : dis[i], i == n ? '
    ' : ' ');
    }
    int main() {
    	int x, y, w;
    	scanf("%d %d %d", &n, &m, &rt);
    	for(int i = 0; i < m; i++) {
    		scanf("%d %d %d", &x, &y, &w);
    		add(x, y, w);
    	}
    	Dijkstra();
    	return 0;
    }
    

    Prime 最小生成树堆优化

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int, int> PII;
    const int INF = 0x3f3f3f3f;
    const int N1 = 5e3 + 10, N2 = 4e5 + 10;
    int head[N1], nex[N2], to[N2], value[N2], cnt = 1;
    int visit[N1], dis[N1], n, m, ans;
    struct cmp {
        bool operator () (const PII & a, const PII & b) const {
            return a.second > b.second;
        }
    };
    void add(int x, int y, int w) {
        to[cnt] = y;
        value[cnt] = w;
        nex[cnt] = head[x];
        head[x] = cnt++;
    }
    void prime() {
        int flag = 0;
        for(int i = 1; i <= n; i++) dis[i] = INF;
        dis[1] = 0;
        priority_queue<PII, vector<PII>, cmp> q;
        q.push(make_pair(1, 0));
        while(!q.empty()) {
            int temp = q.top().first;
            int add = q.top().second;
            q.pop();
            if(visit[temp]) continue;
            visit[temp] = 1;
            flag++;
            ans += add;
            for(int i = head[temp]; i; i = nex[i]) {
                if(dis[to[i]] > value[i]) {
                    dis[to[i]] = value[i];
                    q.push(make_pair(to[i], value[i]));
                }
            }
        }
        if(flag == n)   printf("%d
    ", ans);
        else    puts("orz");
    }
    int main() {
        // freopen("in.txt", "r", stdin);
        int x, y, w;
        scanf("%d %d", &n, &m);
        for(int i = 0; i < m; i++) {
            scanf("%d %d %d", &x, &y, &w);
            add(x, y, w);
            add(y, x, w);
        }
        prime();
        return 0;
    }
    
  • 相关阅读:
    【BZOJ5298】【CQOI2018】交错序列(矩阵快速幂优化dp)
    【BZOJ5297】【CQOI2018】社交网络(有向图生成树计数)
    【BZOJ5296】【CQOI2018】破解D-H协议(BSGS)
    【BZOJ1185】【HNOI2007】最小矩形覆盖(凸包+旋转卡壳)
    【BZOJ1069】【SCOI2007】—最大土地面积(凸包+旋转卡壳)
    【BZOJ2300】【HAOI2011】—防线修建(set维护动态凸包)
    【POJ1912】【Ceoi2002】—A highway and the seven dwarfs(凸包)
    【BZOJ1043】【HAOI2008】—下落的圆盘(圆的并集)
    node-多进程
    Node-RESTful
  • 原文地址:https://www.cnblogs.com/lifehappy/p/12601171.html
Copyright © 2020-2023  润新知