【最小生成树】(模板-prim算法)poj 1258
来源:互联网 发布:农村淘宝服务站利润 编辑:程序博客网 时间:2024/06/05 05:30
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 57238 Accepted: 23744
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
Source
USACO 102
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
题意:有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离;
思路: 最小生成树(prim)模板;
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespace std;const int maxv=110;const int INF=0x3f3f3f3f;int cost[maxv][maxv];int lowc[maxv];bool vis[maxv];int V;int prim() //prim算法模板;{ int ans=0; memset(vis,false,sizeof(vis)); vis[0]=true; for(int i=1;i<V;i++) lowc[i]=cost[0][i]; for(int i=1;i<V;i++) { int minn=INF; int p=-1; for(int j=0;j<V;j++) { if(!vis[j]&&minn>lowc[j]) { minn=lowc[j]; p=j; } } if(minn==INF) return -1; //返回-1,图不联通; ans+=minn; vis[p]=true; for(int j=0;j<V;j++) { if(!vis[j]&&lowc[j]>cost[p][j]) lowc[j]=cost[p][j]; } } return ans;}int main(){ while(~scanf("%d",&V)) { for(int i=0;i<V;i++) for(int j=0;j<V;j++) scanf("%d",&cost[i][j]); int ans=prim(); printf("%d\n",ans); } return 0;}
0 0
- 【最小生成树】(模板-prim算法)poj 1258
- 最小生成树(prim算法模板)
- 最小生成树-Prim算法(模板)
- Prim算法模板(最小生成树)
- 最小生成树(prim算法)模板
- POJ-1258 最小生成树 prim算法
- POJ 1258 -- 最小生成树(prim算法)
- poj-2485-最小生成树prim算法模板
- poj-1258 Agri-Net(最小生成树 Prim算法)
- POJ 1258 (简单最小生成树,prim算法)
- poj 1258 【最小生成树】【prim】【纯模板】
- POJ 1258:Agri-Net Prim最小生成树模板题
- poj 1258 Agri-Net ( 最小生成树[模板题]--prim )
- POJ 1258 : 最小生成树(Prim)
- 最小生成树Prim算法模板
- 最小生成树-prim算法模板
- hdu1233最小生成树 prim算法模板
- prim算法模板—最小生成树
- A. k-Factorization
- win10 UEFI 启动安装 Ubuntu 双系统
- Storm实战常见问题及解决方案
- Echarts 数据可视化图表库 Overview
- 再探java基础——throw与throws
- 【最小生成树】(模板-prim算法)poj 1258
- AndroidStudio 获取SHA1值
- 设计模式之职责链模式
- 不使用if、?:、switch及其他判断语句如何找出两个int型变量中的最大值和最小值
- CPU卡程序设计实例(二十五)ESAM过程密钥内部认证
- Android下Device Tree简介
- [排序] 计数排序
- scala 完美中文教程
- CPU卡程序设计实例(二十六)卡和ESAM之间外部认证