poj_1258_prime_就是dj减配版
来源:互联网 发布:linux rps是什么意思 编辑:程序博客网 时间:2024/06/08 09:17
Agri-Net
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
Source
USACO 102
题意:给你n n*n 每个点代表i-j的距离
求所有点连接的最小距离
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int vis[111];int mp[111][111], d[111], p;const int inf = (int)1e9;int prime(int s){ int i, j, ans = 0; //找第一个点的距离 for (i = 1; i <= p; i++) { d[i] = mp[s][i]; vis[i] = 0; } vis[s] = 1, d[s] = 0; for (i = 1; i < p; i++) { int Min = inf, k; //找下一个点 for (j = 1; j <= p; j++) { if (!vis[j] && Min > d[j]) { k = j; Min = d[j]; } } vis[k] = 1; ans += Min; //更新 for (j = 1; j <= p; j++) { if (!vis[j] && d[j] > mp[k][j]) d[j] = mp[k][j]; } } return ans;}int main(){ int n, i, j; while (cin >> p &&p) { for (i = 0; i <= p; i++) for (j = 0; j <= p; j++) if (i == j) mp[i][j] = 0; else mp[i][j] = inf; cin >> n; for (i = 0; i < n; i++) { int x, y, c; scanf("%d%d%d", &x, &y, &c); mp[x][y] = mp[y][x] = min(mp[x][y], c); } cout << prime(1) << endl; } return 0;}
- poj_1258_prime_就是dj减配版
- 嘿嘿 DJ
- 自制DJ
- TJU_2146_Computer DJ
- dj 模板
- GOD is a DJ
- 回复DJ的游戏
- 可视化dj 绿色
- 好听的DJ
- DJ的由来
- 求巨星DJ台词
- dj的交流
- DJ舞曲网站推荐
- 如果你是dj
- 美国DJ - 兆丰三栏
- DJ Jazzy Jeff
- DJ Jazzy Jeff
- dj的邻接矩阵实现
- Servlet第一个程序--实践操作(命令行)
- spring Quartz和 Spring Task定时任务
- css3多列布局
- pythoQT5 github project
- java三种排序算法的实现
- poj_1258_prime_就是dj减配版
- restful架构
- [Err] 2006
- Python用于文本分析的一个demo—统计文档中只出现一次的字符
- At least one JAR was scanned for TLDs解决方法
- C函数的实参与形参的输出区别
- Spring
- DAY6之Python学习笔记:面向对象术语整理
- Java面向对象三大特征之封装