POJ 1258-Agri Net 最小生成树Prim算法
2010-08-26 00:47
453 查看
题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=1258
解题报告:
典型的最小生成树问题,这次用了Prim算法,搞了很久,主要就是纠结在priority_queue的使用上,一开始使用优先队列,但是又发现这没有Decrease_key操作,后来改用Set,但是在自定义operator后遇到问题,用不来。。。。最后直接放弃,改用直接用O(n)的复杂度来寻找key的最小值,效率下降了,但是还是通过了。。。
附录:
Agri-Net
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
Sample Output
解题报告:
典型的最小生成树问题,这次用了Prim算法,搞了很久,主要就是纠结在priority_queue的使用上,一开始使用优先队列,但是又发现这没有Decrease_key操作,后来改用Set,但是在自定义operator后遇到问题,用不来。。。。最后直接放弃,改用直接用O(n)的复杂度来寻找key的最小值,效率下降了,但是还是通过了。。。
#include <iostream> using namespace std; #define INF 9999999 int n; //点的数目 int A[101][101]; //邻接矩阵表示法 int key[101]; //点集 bool flag[101]; int Prim(int r) { int result=0; for(int i=0;i<n;i++) { key[i]=INF; flag[i]=true; } key[r]=0; for(int i=0;i<n;i++) { int minimum=INF; int min=0; for(int j=0;j<n;j++) { if(flag[j] && minimum > key[j]) { minimum=key[j]; min=j; } } result+= key[min]; flag[min]=false; for(int j=0;j<n;j++) { if(flag[j] && key[j] > A[min][j]) key[j]=A[min][j]; } } return result; } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) cin >> A[i][j]; } cout << Prim(0) << endl;//从点0开始 } }
附录:
Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15910 | Accepted: 6468 |
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算法】POJ-1258---Agri-Net
- 最小生成树(prim算法)--poj1258
- 最小生成树问题·POJ-1258 Agri-Net·prim算法
- POJ-1258 最小生成树 prim算法
- POJ 1258 Agri-Net(最小生成树Prim算法)
- POJ1258 Agri-Net(最小生成树,prim算法)
- poj-1258 Agri-Net(最小生成树 Prim算法)
- 最小生成树之Prim算法---POJ1258---Agri-Net
- POJ 1258 -- 最小生成树(prim算法)
- POJ 1258 Agri-Net 最小生成树 Prim算法
- POJ 1258 Agri-Net(最小生成树prim算法)
- 最小生成树 ———prim算法 poj 1258
- POJ 1258 Agri-Net(最小生成树Prim算法)
- POJ1258--贪心&最小生成树的prim算法
- 【最小生成树】(模板-prim算法)poj 1258
- POJ 1258 Agri-Net 图论 prim算法 最小生成树
- POJ 1258 (简单最小生成树,prim算法)
- poj1258 最小生成树 prim算法
- POJ 1258 Agri-Net 最小生成树 Prim算法
- POJ 2485 Highways(最小生成树Prim算法)