poj 1258 Agri-Net【最小生成树(prime算法)】
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 44827 | Accepted: 18351 |
Description
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
Output
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
做题做的头疼,水一道:
#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f
#define MAX 110
int map[MAX][MAX],low[MAX],vis[MAX];
int t;
void prime()
{
int i,j,next,min,lowdis=0;
memset(vis,0,sizeof(vis));
for(i=1;i<=t;i++)
{
low[i]=map[1][i];
}
vis[1]=1;
for(i=1;i<t;i++)
{
min=inf;
for(j=1;j<=t;j++)
{
if(!vis[j]&&min>low[j])
{
min=low[j];
next=j;
}
}
lowdis+=min;
vis[next]=1;
for(j=1;j<=t;j++)
{
if(!vis[j]&&low[j]>map[next][j])
{
low[j]=map[next][j];
}
}
}
printf("%d\n",lowdis);
}
int main()
{
int n,m,j,i,s;
while(scanf("%d",&t)!=EOF)
{
for(i=1;i<=t;i++)
{
for(j=1;j<=t;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=inf;
}
}
for(i=1;i<=t;i++)
{
for(j=1;j<=t;j++)
{
scanf("%d",&n);
map[i][j]=n;
}
}
prime();
}
return 0;
}
poj 1258 Agri-Net【最小生成树(prime算法)】的更多相关文章
- POJ 1258 Agri-Net(最小生成树,模板题)
用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...
- POJ2395 最小生成树 - Prime算法
题目: Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description The ...
- 最小生成树 prime算法 UVALive - 6437
题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...
- 最小生成树prime算法模板
#include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; i ...
- POJ 1258 Agri-Net(最小生成树 Prim+Kruskal)
题目链接: 传送门 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Description Farmer John has been elec ...
- POJ 1258 Agri-Net (最小生成树)
Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...
- poj 1287 Networking【最小生成树prime】
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7321 Accepted: 3977 Descri ...
- hdoj 1233 还是畅通工程---最小生成树---prime算法
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS ...
- hdoj 1863 畅通工程 最小生成树---prime算法
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...
随机推荐
- iOS军火库-好用的ActionSheetView
GitHub地址 一个自定义的ActionSheetView,支持显示标题,默认选中,使用block回调. 使用说明 [GLActionSheet showWithDataSource:@[@&quo ...
- 通过css实现文本超出部分以省略号(......)代替
一.单行溢出 1,固定宽度(非常容易) text-overflow: ellipsis; 2,不固定宽度 思路:想让这个区域成为块元素,然后不换行,溢出隐藏. display: block; whit ...
- [Lua]索引极致,form.lua
local form = {_tag = 'form'} function form.build(tag, super) local target = { _tag = tag, _super = s ...
- 学习hamcrest和mockito时的总结和demo
UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...
- Android App的生命周期是什么
怎么说呢 看Android一般指的是 Activity的生命周期, 关于app的生命周期, 有明白的大神请告诉我 上面这张图是 网上搜到的一张关于app生命周期的图, 在我看来, 其实就是一个Acti ...
- ListView item 中TextView 如何获取长按事件
昨天晚上小伙伴突然来信, ListView item中嵌套的TextView 无法获取长按事件 从前从来没有仔细留意过, coding后发现...果然没什么动静 而且没有合适的API让我调用获取Tex ...
- 解决VS2015无法调试dotnet core项目
dotnet core 1.0正式版和VS2015 update3安装后一直无法在VS中正常调试. 错误提示:The debugger's worker process (msvsmon.exe) u ...
- scrollView and tableView
As we all know, tableView is the subclass of scrollView, tableView has every properties that scroll ...
- [转]DOS命令
windows dos命令 - 知识天地 - 博客园http://www.cnblogs.com/mfryf/archive/2012/02/13/2348685.html
- dispatch队列
GCD编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行,它类似NSOperationQueue但更复杂也更强大,并且可以嵌套使用.所以说,结合bloc ...