poj-1258-Agri-Net
来源:互联网 发布:淘宝公益宝贝 买家 编辑:程序博客网 时间:2024/06/16 12:05
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.
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
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
题意:求最小生成树。
题解:Prim的模板题,直接莽上去敲,外加kruskal模板
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int maxn=110;const int INF=1<<29;int a[maxn][maxn];int n;int mincost[maxn];bool used[maxn];int prim(){ for(int i=0;i<n;i++) { mincost[i]=INF; used[i]=false; } mincost[0]=0; int res=0; while(true) { int v=-1; for(int u=0;u<n;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<n;u++) { mincost[u]=min(mincost[u],a[v][u]); } } return res;}int main(){ while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) for(int j=0;j<n;j++) { scanf("%d",&a[i][j]); } int ans=prim(); printf("%d\n",ans); } return 0;}
#include <cstdio>#include <algorithm>#include<cstring>#include<iostream>using namespace std;const int maxn = 105;struct edge{ int u, v, w; bool operator < (const edge & p) const { return w < p.w; }} edges[maxn*maxn];int p[maxn];int n;int _find(int x){ return p[x]==x ? x : p[x]=_find(p[x]);}int kruskal(){ int ans = 0; int t=n*n, u, v; for (int i=n; i<t; ++i){ u = _find(edges[i].u); v = _find(edges[i].v); if (u != v){ ans += edges[i].w; p[u] = v; } } return ans;}int main(){ while (scanf("%d", &n)!=EOF){ for (int i=0; i<n; ++i) p[i] = i; int w, cnt=0; for (int i=0; i<n; ++i) for (int j=0; j<n; ++j){ scanf("%d", &w); edges[cnt].u = i; edges[cnt].v = j; edges[cnt++].w = w; } sort(edges, edges+n*n); printf("%d\n", kruskal()); } return 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
- VRPN-体验
- 重载总结
- 洛谷 P1137 旅行计划
- VMware网络模式介绍
- 对象总结2
- poj-1258-Agri-Net
- Maven环境配置(POM文件改中央仓库)
- 微信小程序「官方示例代码」剖析【下】:运行机制
- 接口总结
- Oracle限制数据以及对数据排序
- 最喜欢的科技资讯类英文网站
- 20160922 - 考验来了
- php写入和读取json数据方法
- 面向对象总结