POJ-1258 Agri-Net(最小生成树[Prim])
2015-08-19 11:17
330 查看
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
开始学最小生成树了,照着郭老师的代码敲了一边,第一次交还错了。。。不过发现了二维vector的clear()函数的正确使用方法
Time Limit: 1000MS | Memory Limit: 10000K | |
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
开始学最小生成树了,照着郭老师的代码敲了一边,第一次交还错了。。。不过发现了二维vector的clear()函数的正确使用方法
#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; struct Edge{ int v,w;//v表示边端点,另一个端点已知;w表示边权值,也表示v到最小生成树的距离 Edge(int vv=0,int ww=INF):v(vv),w(ww) {} bool operator < (const Edge& a) const { return w>a.w; } }u; int n,dis[105],ans,cnt;//dis表示各顶点到最小生成树的距离 bool vis[105];//vis表示各顶点是否已被加入最小生成树 vector<vector<Edge> > g(105);//邻接表 void prim() {//prim算法求最小生成树 int i,j,v,w; priority_queue<Edge> q;//存放顶点及其到最小生成树的距离 memset(vis,false,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); cnt=ans=0;//cnt表示已被加入最小生成树的顶点数;ans表示最小生成树的总权值 q.push(Edge(0,0));//开始只有顶点0,它到最小生成树的距离为0 while(cnt<n&&!q.empty()) { do {//每次从队列里面拿离最小生成树最近的点 u=q.top(); q.pop(); }while(vis[u.v]&&!q.empty()); if(!vis[u.v]) { ans+=u.w; vis[u.v]=true; ++cnt; for(i=0,j=g[u.v].size();i<j;++i) {//更新加入点的邻点 if(!vis[v=g[u.v][i].v]&&dis[v]>(w=g[u.v][i].w)) { dis[v]=w; q.push(Edge(v,w)); } } } } //若此时cnt<n,则图不连通 } int main() { int i,j,w; while(1==scanf("%d",&n)) { /*g.clear(); //或者最外层一维清空并且对最外层resize() g.resize(n+1);*/ for(i=0;i<n;++i) { g[i].clear();//必须最内层每一维单独清空才能清空所有数据 for(j=0;j<n;++j) { scanf("%d",&w); g[i].push_back(Edge(j,w)); } } prim(); printf("%d\n",ans); } return 0; }
相关文章推荐
- UVA673括号匹配之vector和stack使用的时候的注意点
- js 跨浏览器基础事件,浏览器检测,判断浏览器的名称、版本号、操作系统
- C/C++之sizeof与strlen的区别
- 如何从一台客户机向服务机发起40亿TCP长连接?
- 曾国藩:从30岁起,脱胎换骨
- Linux环境进程间通信(一)
- ffmpeg视频转换工具使用
- win8应用商店提示无法完成你的购买的两种解决办法
- 如何快速的杀掉Oracle的Session
- php调用webservice的几种方法
- 分享体积小巧,功能强大的网络嗅探和分析工具SnifferView 版本-1.0.0.25
- GPU/DRM 简介
- 没有利润,哪来的服务?
- ASP.NET前台AJAX方法调用后台的方法写法
- 判断一个单向链表是否有环
- 开源全局配置中心XDiamond
- Mysql 5.6.26 zip免安装版配置安装教程
- ios开发判断网络连接及网络异常(ios自学笔记)
- Qt网络获取外网的网络信息
- 生成某一个view的png图片