poj2421 Constructing Roads
来源:互联网 发布:最简单的网络贷款平台 编辑:程序博客网 时间:2024/06/05 01:07
题目链接:http://poj.org/problem?id=2421
题意:给你一个邻接矩阵并且还告诉你已经有一些边相连了,让你求最小生成树
解析:已经相连的边赋值为0,然后直接做最小生成树
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int maxn = 1e5+100;struct node{ int u,v,c; node () {} node (int _u,int _v,int _c) { u = _u; v = _v; c = _c; } bool operator < (const node &b)const { return c<b.c; }}a[maxn];int tmp[105][105];int fa[105];int getfa(int x){ if(x==fa[x]) return fa[x]; return fa[x] = getfa(fa[x]);}int main(void){ int n,m; scanf("%d",&n); for(int i=1;i<=n;i++) { fa[i] = i; for(int j=1;j<=n;j++) scanf("%d",&tmp[i][j]); } scanf("%d",&m); while(m--) { int x,y; scanf("%d %d",&x,&y); tmp[x][y] = 0; } int cnt = 0,ans = 0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) a[cnt++] = node(i,j,tmp[i][j]); } sort(a,a+cnt); for(int i=0;i<cnt;i++) { int t1 = getfa(a[i].u); int t2 = getfa(a[i].v); if(t1!=t2) { fa[t1] = t2; ans += a[i].c; } } printf("%d\n",ans); return 0;}
阅读全文
0 0
- POJ2421 Constructing Roads
- poj2421 Constructing Roads
- POJ2421 Constructing Roads
- poj2421 Constructing Roads
- POJ2421 Constructing Roads
- HDU1102&&POJ2421 Constructing Roads
- poj2421 [java]Constructing Roads
- POJ2421 Constructing Roads
- poj2421 Constructing Roads
- poj2421——Constructing Roads
- poj2421 Constructing Roads (prime)
- 2011.10.28 poj2421 Constructing Roads 解题报告
- POJ2421 Constructing Roads [最小生成树 prim]
- poj2421 Constructing Roads【最小生成树】
- POJ2421 Constructing Roads(最小生成树)
- POJ2421 Constructing Roads(最小生成树,Prim)
- Agri Net POJ1258 && Constructing Roads POJ2421
- Constructing Roads*
- 将“在此处打开命令窗口”添加到右键菜单
- 阿里云ECS使用过程中遇到的坑
- 模板--快速幂及矩阵快速幂
- poj2485 highways 之prim解法
- 详解ImageNet 2017夺冠架构SENet
- poj2421 Constructing Roads
- 科技界的看门人—微积分
- select下拉框与input输入框相结合;正则表达式判断字符串是否未日期格式
- computer vision一些术语-目标识别、目标检测、目标分割、语义分割等
- spl 教程四 接口
- 详解vue之vuex
- 自由幻想UI之成就界面
- Html 的小误区
- virtualenv