POJ 1258 Agri-Net
来源:互联网 发布:自己淘宝店铺的链接 编辑:程序博客网 时间:2024/06/16 19:36
题目大意如下:
农民John当了镇长,他要为小镇谋福利,就为小镇所有的农场铺设光缆链接,给定矩阵表示没两个农场之间的距离,求铺设光缆的最小成本。
Prim算法,代码如下:
#include <stdio.h>#include <string.h>const int M=505;int map1[M][M],n;#define typec int // type of costconst typec inf = 0x3f3f3f3f; // max of costint vis[M];typec lowc[M];typec prim(typec cost[][M]) // vertex: 0 ~ n-1{ int i, j, p; typec minc, res = 0; memset(vis, 0, sizeof(vis)); vis[0] = 1; for (i=1; i<n; i++) { lowc[i] = cost[0][i]; } for (i=1; i<n; i++) { minc = inf; p = -1; for (j=0; j<n; j++) { if (0 == vis[j] && minc > lowc[j]) { minc = lowc[j]; p = j; } } res+=minc; vis[p] = 1; for (j=0; j<n; j++) { if (0 == vis[j] && lowc[j] > cost[p][j]) { lowc[j] = cost[p][j]; } } } return res;}int main(){ int i,j; while(scanf("%d",&n)!=EOF) { for(i=0; i<n; i++) { for(j=0; j<n; j++) { scanf("%d",&map1[i][j]); } } printf("%d\n",prim(map1)); } return 0;}
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj 1258-Agri-net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ+1258++Agri-Net
- Poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj-1258-Agri-Net
- poj-1258 Agri-Net
- Poj 1258 Agri-Net
- POJ 1258 - Agri-Net
- poj 3415
- 树状数组模板
- 黑马程序员---语句
- javacript 面向对象编程(模拟购物车)
- 向量的叉积 凸包
- POJ 1258 Agri-Net
- Java对象和XML转换
- when execute the ASM "LOCK" prefix, are all the memory being locked or are the relevant area in the
- 使用 SOCKET 通信实现 FTP 客户端程序
- 差分约束系统
- sql server 2008远程连接数据库的配置
- Uva11609 teams
- XCode工程中关于Targets的介绍
- CPU架构学习心得(一)