poj 2377
来源:互联网 发布:无线上网数据卡怎么用 编辑:程序博客网 时间:2024/06/08 05:48
题意:
最大生成树
思路:
最大生成树
code:
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<queue>#include<map>#include<set>#include<cmath>using namespace std;#define INF 0x3f3f3f3f#define PI acos(-1.0)#define eps 1e-4#define maxd 10e4#define mem(a, b) memset(a, b, sizeof(a))typedef pair<int,int> pii;typedef long long LL;//------------------------------const int maxn = 1005;const int maxm = 20005;struct Edge{ int u,v,w; bool operator < (const Edge et) const{ return w > et.w; }}e[maxm];int n,m;void init(){ int u,v,w; for(int i = 0; i < m; i++){ scanf("%d%d%d",&u,&v,&w); e[i].u = u; e[i].v = v; e[i].w = w; }}int p[maxn];int find(int x){ if(x == p[x]) return p[x]; else return p[x] = find(p[x]);}void solve(){ sort(e, e+m); for(int i = 0; i <= n; i++) p[i] = i; int cnt = 1; LL ans = 0; for(int i = 0; i < m; i++){ int pu = find(e[i].u); int pv = find(e[i].v); if(pu != pv){ ans += e[i].w; p[pu] = pv; cnt++; } if(cnt == n){ break; } } if(cnt < n) printf("-1\n"); else cout << ans << endl;}int main(){ scanf("%d%d",&n,&m); init(); solve(); return 0;}
这道题没有题解一定是因为特太简单了T_T 我居然因为 ans += e[i].w 写错位置wa了一发 b l g l
0 0
- poj-2377
- poj 2377
- POJ 2377
- POJ 2377
- poj 2377
- POJ 2377
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ-2377-Bad Cowtractors
- POJ 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- Bad Cowtractors.(POJ-2377)
- poj 2377 Bad Cowtractors
- 算法复习--------------基本算法:静态链表的实现
- mpstat 命令详解
- From feature descriptors to deep learning: 20 years of computer vision
- cocos2dx 3.4 动画
- 开始Java基础学习
- poj 2377
- 推荐杂志
- Python中的Numpy、SciPy、MatPlotLib安装与配置
- 如何学习安卓软件开发
- 欢迎使用CSDN-markdown编辑器
- 计算广告[资料搜集]
- 【状压DP】 ZOJ 3777 Problem Arrangement
- 华为2016届实习生招聘(武汉)笔试
- 【js思考】用js实现将输入的数字的每一位平方,输出也是数字形式