1258-Agri-net
来源:互联网 发布:淘宝千幻魔镜 编辑:程序博客网 时间:2024/05/22 06:57
1258:Agri-Net
- 查看
- 提交
- 统计
- 提示
- 提问
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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. - 输入
- 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.
- 输出
- 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.
40 4 9 214 0 8 179 8 0 1621 17 16 0
28
其实是一个很简单的Prim算法。
不过WA和TLE各一次。WA是因为没注意到多组数据,TLE是因为将while(cin)写成了while(1),后来在hyc大神的帮助下了解到二者的区别,while(1)循环无法退出,二者在文件输入的情况下是不同的,其实这也算是个历史遗留问题了吧。今后写程序还是得注意细节,得过且过是绝对不行的。
把代码附上:
#include <iostream>#define MAX 100010using namespace std;int main(){ int N; while(cin >> N) { int flag[101] = {0}, cnt = 1, sign[101] = {0}; int Net[101][101] = {0}; for(int i = 1; i <= N; ++i) for(int j = 1; j <= N; ++j) cin >> Net[i][j]; flag[1] = 1; sign[1] = 1; int tmp = 0, sum = 0, edge = MAX; while(cnt < N) { edge = MAX; for(int i = 1; i <= cnt; ++i) { for(int j = 1; j <= N; ++j) { if(flag[i] == j || sign[j] == 1) continue; if(edge > Net[flag[i]][j]) { edge = Net[flag[i]][j]; tmp = j; } } } cnt += 1; flag[cnt] = tmp; sign[tmp] = 1; sum += edge; } cout << sum << endl; }}
0 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
- 动态规划3:最大子段和问题到最大子矩阵问题(三):初探最大子矩阵之和问题
- Nginx CORS实现JS跨域
- 黑马程序员_Java_泛型(Generic)
- 11g的exp导出空表提示EXP-00011: SCOTT.TEST1 不存在
- UVA 10917 Walk Through the Forest SPFA
- 1258-Agri-net
- dede:channel 分页 & JS分页
- XP批处理导入数字签名和对应用程序签名
- 将树莓派Raspberry Pi设置为无线路由器(WiFi热点AP,RTL8188CUS芯片)2
- 黑马程序员_Java_枚举(enum)
- SVG画多种不同的图形
- 全局分区索引与局部分区索引
- FFMPEG架构分析
- 使用xcode+luajit 加密lua