Agri-Net_POJ1258
来源:互联网 发布:淘宝卖家有ipad客户端 编辑:程序博客网 时间:2024/05/18 20:10
题目
链接 POJ1258
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 56713 Accepted: 23518
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
思路
1. 最小生成树。Prim算法。
代码
#include <iostream>#include <algorithm>using namespace std;#define MAX_N 100#define INF 0x3F3F3F3Fint cost[MAX_N+1][MAX_N+1];int mincost[MAX_N+1];bool used[MAX_N+1];int V;int prime();int main(){ int N = 0; while (cin >> N) {V = N;for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)cin >> cost[i][j];cout << prime() << endl; } return 0;}int prime(){fill(mincost, mincost+MAX_N, INF);fill(used, used+MAX_N, false);mincost[0] = 0;int res = 0;while (true){int v = -1;for (int u = 0; u < V; u++){if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;}if (v == -1) break;used[v] = true;res += mincost[v];for (int u = 0; u < V; u++){if (!used[u])mincost[u] = min(mincost[u], cost[v][u]);}}return res;}
0 0
- Agri-Net_POJ1258
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- storm任务提交流程
- 【Maven】使用Maven构建多模块项目
- 异步操作的简单笔记
- Cornerstone使用
- 值传递和引用传递
- Agri-Net_POJ1258
- Android自定义View-------为什么重写onMeasure()以及怎么重写
- 函数指针作为参数
- 关于Windows Server 服务器 安装tomcat部署Java Web 项目的问题
- LeetCode-7(Reverse Integer)
- Rman备份由于空间不足导至的错误
- 安装虚拟机提示“无法安装服务VMAuthdService”,请确保您有足够的权限安装系统服务
- win7环境下安装ubuntu双系统
- 基于腾讯云的域名绑定与tomcat部署