[usaco]Agri-Net(使用最小生成树算法)
来源:互联网 发布:数据库登录 编辑:程序博客网 时间:2024/06/05 07:39
Agri-Net
Russ Cox
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.
PROGRAM NAME: agrinet
INPUT FORMAT
Line 1: The number of farms, N (3 <= N <= 100).
Line 2..end: The subsequent lines contain the N x N connectivity 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.
SAMPLE INPUT (file agrinet.in)
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
OUTPUT FORMAT
The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
SAMPLE OUTPUT (file agrinet.out)
28
------------------
这个问题就是让求最小生成树。
利用prim 算法,或者kruskal算法即可。
鉴于节点的总个数小于等于100,可以使用一个常量数组
我的解法
----------------
/*ID:yunleis2PROG:agrinetLANG:C++*/#include<iostream>#include<fstream>using namespace std;/************************************************************************//* minimal spinning tree *//************************************************************************/int metri[101][101];int destance[101]; bool intree[101];int main(){fstream fin("agrinet.in",ios::in);int N;fin>>N;for(int i=0;i<N;i++){for(int j=0;j<N;j++){fin>>metri[i][j];}destance[i]=0;intree[i]=false;}int sum=0;for(int i=0;i<N;i++){if(!intree[i]){intree[i]=true;for(int j=0;j<N;j++){destance[j]=metri[i][j];}while(true){int ptr=-1;for(int j=0;j<N;j++){if(ptr==-1||destance[ptr]==0||(intree[ptr]))ptr=j;else if((!intree[j])&&destance[j]<destance[ptr])ptr=j;}if(intree[ptr])break;intree[ptr]=true;sum+=destance[ptr];for(int j=0;j<N;j++){if((!intree[j])&&(destance[j]>metri[ptr][j])){destance[j]=metri[ptr][j];}}}}}fstream fout("agrinet.out",ios::out);fout<<sum<<endl;//system("pause");}
测试:
USER: Ma yunlei [yunleis2]
TASK: agrinet
LANG: C++
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 3064 KB]
Test 2: TEST OK [0.000 secs, 3064 KB]
Test 3: TEST OK [0.000 secs, 3064 KB]
Test 4: TEST OK [0.000 secs, 3064 KB]
Test 5: TEST OK [0.000 secs, 3064 KB]
Test 6: TEST OK [0.000 secs, 3064 KB]
Test 7: TEST OK [0.000 secs, 3064 KB]
Test 8: TEST OK [0.000 secs, 3064 KB]
Test 9: TEST OK [0.000 secs, 3064 KB]
Test 10: TEST OK [0.000 secs, 3064 KB]
All tests OK.
YOUR PROGRAM ('agrinet') WORKED FIRST TIME! That's fantastic
-- and a rare thing. Please accept these special automated
congratulations.
- [usaco]Agri-Net(使用最小生成树算法)
- USACO Section 3.1 Agri-Net(最小生成树Prime算法)
- USACO Agri-Net,最小生成树
- USACO 3.1.1 Agri-Net 最短网络(最小生成树Prime算法)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)(普里姆算法)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)(克鲁斯卡尔算法)
- USACO section 3.1 Agri-Net(最小生成树,prim)
- USACO-Section 3.1 Agri-Net(最小生成树[Prim])
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- [最小生成树]USACO 3.1 Agri-Net 最短网络
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- USACO 3.1 Agri-Net 最短网络 (最小生成树)
- 等待4
- 脚本||程序_神马的_头大
- linux字符串函数的实现(内核源代码)
- Android有效解决加载大图片时内存溢出的问题
- 2011年上半年登陆美国市场上市科技企业知多少
- [usaco]Agri-Net(使用最小生成树算法)
- 深入理解指针函数
- JDK5多线程框架java.util.concurrent,android中也是可以用的
- IE6,背景图片出现闪烁问题。
- zju 1525 Air Raid(最小路径覆盖)
- linux设备驱动
- Managing Projects
- 开博了....
- vcs双机基础之一