HDU 1102:Constructing Roads
来源:互联网 发布:java程序设计培训 编辑:程序博客网 时间:2024/06/13 07:08
最小生成树模板:
AC代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#define eps 1e-6using namespace std;const int maxn = 110;const int INF = 1e9;int N;int Map[maxn][maxn];void initMap(){ for(int i = 1; i <= N ; i++) { Map[i][i] = 0; for(int j = i+1; j <= N; j++) { Map[i][j] = Map[j][i] = INF; } }}int lowcost[maxn];int minCost;void prim(int s){ for(int i = 1; i <= N; i++) { lowcost[i] = Map[s][i]; } lowcost[s] = -1; int u; int mst = 0; for(int i = 1; i < N; i++) { minCost = INF; u = 0; for(int j = 1; j <= N; j++) { if(lowcost[j] != -1 && lowcost[j] < minCost) { minCost = lowcost[j]; u = j; } } if(u == 0) break; lowcost[u] = -1; mst += minCost; for(int j = 1; j <= N; j++) { if(Map[u][j] < INF && Map[u][j]<lowcost[j]) { lowcost[j] = Map[u][j]; } } } printf("%d\n",mst);}int main(){ while(~scanf("%d",&N)) { initMap(); for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { scanf("%d",&Map[i][j]); } } int Q,u,v; scanf("%d",&Q); while(Q--) { scanf("%d%d",&u,&v); Map[u][v] = Map[v][u] = 0; } prim(1); } return 0;}
阅读全文
0 0
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- Hdu-1102 Constructing Roads
- Hdu-1102 Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- hdu 1102 Constructing Roads
- Hdu 1102 - Constructing Roads
- hdu 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- HDU 1102 Constructing Roads
- 获取子控件在父控件中下标
- python 爬虫初学项目一(80s电影网)
- 重新编辑regular expression match
- “腾讯分析”网站前端分析
- Hive删除数据库
- HDU 1102:Constructing Roads
- 该干嘛干嘛
- Myeclipse学习总结(13)——Eclipse/MyEclipse/IDEA中《阿里巴巴Java开发规约》插件使用指南
- MySql数据库-01MySql的安装
- CentOS7 minimal 最小化安装网络设置
- hdu 1754_单点更新,区间查询,查询注意if(rr>m) if (ll <= m)
- Word2vec 之 Skip-Gram 模型(结构篇)
- Ubuntu下搭建,使用FTP服务器图解
- 在 Linux 下使用 rfkill 软开关蓝牙及无线功能