Poj1258 Agri-Net (Prim)
来源:互联网 发布:淘宝无营业执照 编辑:程序博客网 时间:2024/06/05 02:48
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 44981 Accepted: 18449
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题和上一题类似。就是最小生成树。对自己竟无语凝噎。。。。又把更新dis[]的时候写错。。dis[j]>Map[pos][j] dis[j]=Map[pos][j];#include<iostream>#include<cstdio>#include<cstring>#define inf 0x3f3f3f3fusing namespace std;int Map[600][600],dis[600],vis[600],z,n,m;void Prim(){ int ma; int pos; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) dis[i]=Map[1][i]; vis[1]=1; for(int i=1;i<n;i++)// { ma=inf; for(int j=1;j<=n;j++) { if(ma>dis[j]&&!vis[j]) { ma=dis[j]; pos=j; } } z+=ma; vis[pos]=1; for(int j=1;j<=n;j++) { if(dis[j]>Map[pos][j]&&!vis[j]) { dis[j]=Map[pos][j]; } } } cout<<z<<endl;}int main(){ int i,j,k; while(cin>>n&&n) { z=0; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cin>>Map[i][j]; } } Prim(); } return 0;}
0 0
- poj1258 Agri-Net(Prim)
- POJ1258 Agri-Net【Prim】
- Poj1258 Agri-Net (Prim)
- POJ1258《Agri-Net》方法:Prim
- POJ1258 Agri-Net 【最小生成树Prim】
- POJ1258 Agri-Net(Prim求解最小生成树)
- 最小生成树之Prim算法---POJ1258---Agri-Net
- poj1258 Agri-Net 最小生成树 prim kruskal 模板
- POJ1258 Agri-Net(最小生成树,prim算法)
- POJ1258 Agri-Net
- POJ1258 Agri-Net
- POJ1258 Agri-Net
- POJ1258 Agri-Net
- Agri-Net(POJ1258)
- poj1258 - Agri-Net
- POJ1258--Agri-Net--Kruskal
- poj1258 Agri-Net
- POJ1258 &&TOJ3197 Agri-Net
- 数据结构(三):非线性逻辑结构-特殊的二叉树结构:堆、哈夫曼树、二叉搜索树、平衡二叉搜索树、红黑树、线索二叉树
- QT::QTableView的使用
- 程序员,你幸福吗?
- Kali Linux 卸载gnome桌面环境
- Java设计模式(二) 之 迭代器模式
- Poj1258 Agri-Net (Prim)
- 用svg实现上传图片进度条效果
- windows 多线程
- list set map 区别
- org.springframework.context.ApplicationContextAware使用理解
- oj 中G++和C++区别
- DAVE SDK 4.1.2 Help content (翻译)——manifest
- android design library提供的TabLayout的用法
- 转--Could not create the view: An unexpected exception was thrown.问题解决