日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/4 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模板: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 ? '\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\n", 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; }

總結

以上是生活随笔為你收集整理的模板:Prime最小生成树堆优化 + Dijkstra单源最短路堆优化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。