POJ 1258 Agri-Net(MST)
来源:互联网 发布:哪有好的java培训 编辑:程序博客网 时间:2024/05/19 18:36
Agri-Net
http://poj.org/problem?id=1258
Time Limit: 1000MS
Memory Limit: 10000K
Description
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.
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
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
裸MST。用Prim搞定。
完整代码:
未优化,O(N^3)
/*16ms,184KB*/#include<cstdio>#include<cstring>using namespace std;const int maxn = 101;int map[maxn][maxn];bool visited[maxn];int main(){int n;while (~scanf("%d", &n)){memset(visited, 0, sizeof(visited));memset(map, 0, sizeof(map));int i, j, k;for (i = 0; i < n; i++)for (j = 0; j < n; j++)scanf("%d", &map[i][j]);visited[0] = true;int temp = 0, sum = 0;for (i = 1; i < n; i++){int min = 1000000;//okfor (j = 0; j < n; j++){if (visited[j])//从所有已算入的点出发{for (k = 0; k < n; k++){if (!visited[k] && map[j][k] < min)//不搜索到已算入的点的边{min = map[j][k];temp = k;//printf("%d ", min);}//else//printf(" ");}}//printf("\n");}//printf("###\n");sum += min;visited[temp] = true;}printf("%d\n", sum);}return 0;}
- poj 1258 Agri-Net MST
- POJ 1258 Agri-Net(MST)
- [MST]POJ 1258Agri-Net
- POJ 1258 Agri-Net【MST】
- POJ 1258 Agri-Net MST .
- POJ 1258 Agri-Net (经典MST)
- POJ 1258 Agri-Net (MST)
- POJ 1258 Agri-Net 【MST,Prime算法】
- POJ 1258 Agri-Net|| POJ 2485 Highways MST
- Poj 1258 Agri-Net[MST最小生成树]
- POJ 1258 Agri-Net (prim求MST)
- POJ-1258 Agri-Net 最小生成树(MST)
- poj 1258 Agri-Net 最经典的MST★
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj 1258-Agri-net
- poj 1258 Agri-Net
- poj 1904 King's Quest(强连通)
- 基于linux-2.6.35的class_create(),device_create解析
- 【代码大全】第12章 基本数据类型
- 【代码大全】第13章 不常见的数据类型
- 【代码大全】第14章 组织直线型代码
- POJ 1258 Agri-Net(MST)
- 关于Linux下计划任务无法执行的问题
- 传智播客学习日记Day24
- 【代码大全】第15章 使用条件语句
- 【代码大全】第16章 控制循环
- GDB调试
- 【代码大全】第17章 不常见的控制结构
- “OVERRIDE”: 未知重写说明符
- Windows Event 的各个相关函数解释 及 简单例子一个