POJ 1258 Agri-Net (最小生成树/Prim)
来源:互联网 发布:刚玉的优化处理方法 编辑:程序博客网 时间:2024/06/05 22:31
最小生成树:不成环 权值最小 包含所有点 的树。
Prim :
maps 地图
book 标记数组
flag { 以人工选定的树根 (1) } 的最小生成树 最小生成树一定存在这个点
刚开始的 flag数组 以树根到其他点的距离 然后每次添加一个点 维护 flag数组。
每次添加的点 权值最小 不成环。
代码:
#include<stdio.h>#include<string.h>using namespace std;const int maxn=105;const int dis=1e5+5;int maps[105][105];int book[105];int flag[105];int t;void init(){ memset(book,0,sizeof(book));}int Prim(){ int ans=0; int cnt=1; for(int i=1;i<=t;i++) flag[i]=maps[1][i]; int j; while(cnt<=t) { int minn=dis; for(int i=1;i<=t;i++) { if(book[i]==0&&minn>flag[i]) { minn=flag[i]; j=i; } } cnt++; book[j]=1; ans=ans+flag[j]; for(int n=1;n<=t;n++) { if(book[n]==0&&flag[n]>maps[j][n]) { flag[n]=maps[j][n]; } } } return ans;}int main(){ while(~scanf("%d",&t)) { init(); for(int i=1; i<=t; i++) { for(int j=1; j<=t; j++) { scanf("%d",&maps[i][j]); } } int ans=Prim(); printf("%d\n",ans); }}
代码:
#include<stdio.h>#include<string.h>using namespace std;const int maxn=1e5;struct node{ int u,v,w;}edge[maxn];int f[100000005];int t;int getf(int v){ if(f[v]==v) return v; else { f[v]=getf(f[v]); return f[v]; }}int unin(int v,int u){ int A,B; A=getf(v); B=getf(u); if(A!=B) { f[B]=A; return 1; } return 0;}void quicksort(int left,int right){ int i,j; node t; if(left>right) return; i=left,j=right; while(i!=j) { while(edge[j].w>=edge[left].w&&i<j) { j--; } while(edge[i].w<=edge[left].w&&i<j) { i++; } if(i<j) { t=edge[i]; edge[i]=edge[j]; edge[j]=t; } } t=edge[left]; edge[left]=edge[i]; edge[i]=t; quicksort(left,i-1); quicksort(i+1,right); return ;}int main(){ while(~scanf("%d",&t)) { int flag=1; for(int i=1; i<=t; i++) { for(int j=1; j<=t; j++) { edge[flag].u=i; edge[flag].v=j; scanf("%d",&edge[flag++].w); } } for(int i=1; i<=t*t; i++) f[i]=i; //if(flag-1==t*t)while(1){}; quicksort(1,t*t); int cnt=0; int ans=0; for(int i=1; i<=flag; i++) { if(unin(edge[i].u,edge[i].v)) { //printf("%d %d %d\n",i,j,edge[i*t+j].w); cnt++; ans=ans+edge[i].w; } if(cnt==t-1) break; } printf("%d\n",ans); }}
题目:
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 50567 Accepted: 21014
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
0 0
- POJ 1258:Agri-Net(最小生成树&&prim)
- POJ 1258-Agri-Net(最小生成树prim)
- poj 1258 Agri-Net (最小生成树 prim)
- poj-1258 Agri-Net(最小生成树 Prim算法)
- POJ 1258 Agri-Net(最小生成树-Prim)
- poj 1258 Agri-Net(prim求最小生成树)
- POJ-1258 Agri-Net(最小生成树[Prim])
- POJ 1258 Agri-Net (最小生成树/Prim)
- POJ 1258 Agri-Net (最小生成树 prim)
- POJ 1258 Agri-Net(最小生成树-Prim)
- POJ 1258-Agri Net 最小生成树Prim算法
- poj 1258Agri-Net-最小生成树prim
- Poj 1258 Agri-Net (最小生成树Prim & Kruskal)
- POJ 1258 Agri-Net 图论 prim算法 最小生成树
- POJ 1258 Agri-Net 最小生成树 Prim
- poj 1258 Agri-Net(prim求最小生成树)
- POJ 1258 Agri-Net(最小生成树prim算法)
- 【最小生成树之prim算法】POJ-1258---Agri-Net
- android反编译
- oracle 11.2.0.4 physical standby failover
- 图的遍历
- 【记录】详解HTTP GET请求
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- POJ 1258 Agri-Net (最小生成树/Prim)
- Spring MVC 急速集成 Shiro 实录
- Java webSocket 网页聊天室
- 战争与和平 摘录一
- swift版本的图片轮播(借鉴于前辈的思路)
- 七牛evm主机主机快照和磁盘快照使用介绍
- 热插件 反射模式 查找接口相关的类,并初始化实例
- [php]对象的传值方式
- 在eclipse.ini中为eclipse指定jdk启动