hdu 2122 Ice_cream’s world III
2015-08-12 15:28
417 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2122
解题思路:
求最小生成树。。。注意测试数据里有重边。。。
AC代码:
http://acm.hdu.edu.cn/showproblem.php?pid=2122
解题思路:
求最小生成树。。。注意测试数据里有重边。。。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> #define INF 0xfffffff using namespace std; const int N = 1005; const int M = 10005; int n,m; int edge ; int vis ; int dis ; void build(){ memset(vis,0,sizeof(vis)); for(int i = 0; i < n; i++){ edge[i][i] = 0; for(int j = 0; j < i; j++) edge[i][j] = edge[j][i] = INF; } for(int i =0; i < m; i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); edge[x][y] = edge[y][x] = min(edge[x][y],z); } } void prime(int cur) { int i, j, tmp; int sum = 0; memset(vis, 0, sizeof(vis)); vis[cur] = 1; for(i = 0; i < n; i++) dis[i] = edge[cur][i]; for(i = 1; i < n; i++) { int Min = INF,tmp = -1; for(j = 0; j < n; j++) { if(!vis[j] && dis[j] < Min) Min = dis[tmp = j]; } if(tmp == -1){ printf("impossible\n"); return ; } vis[tmp] = 1; sum += Min; for(j = 0; j < n; j++) { if(!vis[j] && dis[j] > edge[tmp][j]) dis[j] = edge[tmp][j]; } } printf("%d\n",sum); } int main(){ while(~scanf("%d%d",&n,&m)){ build(); prime(0); printf("\n"); } return 0; }
相关文章推荐
- 关于前后台汉字乱码问题
- 内存调试的东西
- android之view测量总结
- 程序员面试揭秘之程序员靠什么途径去美国工作?
- jQuery全屏滚动插件fullPage.js中文帮助
- pip常用命令
- eclipse 启动后一直在Loading descriptor
- 设计模式
- 如何学习android高级编程
- KMP小计
- C# WPF 控件通用属性简介
- 华为OJ(蛇形矩阵)
- hdu 2406 Power Strings KMP
- Matlab强迫症产生的图像
- 为什么要使用freemarker
- jquery自定义类的封装
- My97DatePicker时间控件使用说明
- ListView之BaseAdapter的基本使用以及ViewHolder模式
- datanode起不起来
- 今天辞职-第一博纪念