hdu 1233 堆优化prim
来源:互联网 发布:剑三成女军娘捏脸数据 编辑:程序博客网 时间:2024/06/09 15:12
#include<bits/stdc++.h>using namespace std;const int maxn=100005;const int inf=0x3f3f3f3f;typedef struct{ int to,cost,next;}node;node edge[maxn*2];int cnt,pre[maxn],dis[maxn],vis[maxn];struct cmp{ bool operator() (int &a, int &b)const{ return dis[a] > dis[b]; }};void prim(){ int res=0; priority_queue<int,vector<int>,cmp> que; dis[1]=0; vis[1]=1; que.push(1); while(que.size()) { int now=que.top(); que.pop(); res+=dis[now]; dis[now]=0; for(int i=pre[now];i!=-1;i=edge[i].next) { int tp=edge[i].to; dis[tp]=min(dis[tp],edge[i].cost); if(vis[tp]) { queue<int> quee; while(que.size()) { quee.push(que.top()); que.pop(); } while(quee.size()) { que.push(quee.front()); quee.pop(); } continue; } que.push(tp); vis[tp]=1; } } printf("%d\n",res);}void add_edge(int u,int v,int w){ edge[cnt].cost=w; edge[cnt].to=v; edge[cnt].next=pre[u]; pre[u]=cnt++;}void init(){ memset(pre,-1,sizeof(pre)); memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); cnt=0;}int main(){ int n,u,v,w,i; while(scanf("%d",&n)!=EOF&&n) { init(); for(i=1;i<=n*(n-1)/2;i++) { scanf("%d%d%d",&u,&v,&w); add_edge(u,v,w); add_edge(v,u,w); } prim(); } return 0;}
阅读全文
0 0
- hdu 1233 堆优化prim
- Prim 的堆优化
- Prim堆优化
- 堆优化prim
- prim 堆优化
- 堆优化Prim
- prim算法的堆优化。
- 堆优化的prim算法
- 最小生成树Prim算法 堆优化
- 【模板】Prim+堆优化 最小生成树
- hihocoder-1109-堆优化的prim
- hdu1102之prim(堆优化)解法
- hdu 3415 堆优化
- HDU-1233(prim写法)
- HDU 1233 Prim
- hdu 1233 prim 算法
- hdu 1233 【prim】
- POJ 1789 Prim 向愚蠢的堆优化告别
- leetcode
- 文件夹列表,xml格式
- Codeforces Round #380~#400 div2 总结
- java在调试模式下打断点的时候,断点显示为禁用状态的解决
- 提高PHP代码执行效率小结
- hdu 1233 堆优化prim
- 对海康28181摄像头PS流解码的支持(一)
- codeforces contest 13 problem E(分块)
- 基于R语言的Kaggle案例分析学习笔记(五)
- 【入门】Java登录注册
- mongodb_权限
- js中数组Array的一些常用方法总结
- ROW_NUMBER() OVER(PARTTON BY T.ITEM_CODE ORDER BY T.VERSION DESC)
- PAT乙级1044. 火星数字(20)