poj1258(最小生成树)
来源:互联网 发布:java编写四级查分软件 编辑:程序博客网 时间:2024/06/03 09:54
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 60241 Accepted: 24975
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
又是一道水题。。直接用prim算法
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;#define INF 1e9+1#define MAX_V 105int cost[MAX_V][MAX_V];int mincost[MAX_V];bool used[MAX_V];int V;int prim(){for(int i=0;i<V;i++){mincost[i]=INF;used[i]=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++)mincost[u]=min(mincost[u],cost[v][u]);}return res;}int main(){while(cin>>V){for(int i=0;i<V;i++)for(int j=0;j<V;j++)cin>>cost[i][j];cout<<prim()<<endl;}}
阅读全文
0 0
- POJ1258->最小生成树
- poj1258 最小生成树
- poj1258(最小生成树)
- poj1258 最小生成树krus
- poj1258 prim最小生成树
- poj1258 最小生成树 普林姆
- POJ1258 基础最小生成树
- POJ1258简单最小生成树
- poj1258 prim最小生成树
- poj1258(最小生成树)
- poj1258(最小生成树Kruscal)
- poj1258(最小生成树Prim)
- POJ1258 最小生成树 水题
- 最小生成树(Prim)poj1258
- poj1258 最小生成树,prim
- POJ1258 最小生成树prim算法
- 最小生成树poj1258 prim和kruskal
- POJ1258-最小生成树-kruskal模板
- 浅谈Android系统中drawable的使用
- 【LeetCode】climbStairs
- H5 定位
- HDU6040 排列问题 element运用
- 构造方法和成员方法的区别
- poj1258(最小生成树)
- 批处理系统的调度算法
- setTimeout()与setInterval()
- Andrew Ng机器学习课程笔记--week2(多元线性回归&正规公式)
- C++函数指针与指针识别
- 以太网,令牌网,FDDI,ATM到底是什么?有什么区别?
- 机器学习PAI教你如何品酒
- 激光雷达 imu 同步
- HDU 1754 I Hate it (树状数组)(区间最大值)