快速切题 poj1258
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 40056 | Accepted: 16303 |
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 应用时:5min
实际用时:3h12min
思路:裸prim
#include <cstdio>
#include<cstring>
#include <queue>
#include <assert.h>
using namespace std; int blen;
int d[102][102];
int n;
bool vis[102];
typedef pair<int,int> P;
int prim(){
memset(vis,0,sizeof(vis));
priority_queue<P,vector<P>,greater<P> >que;
int num=1;
int ans=0;
for(int i=1;i<n;i++){
que.push(P(d[0][i],i));
}
vis[0]=true;
while(num<n&&!que.empty()){
int t=que.top().second;
int td=que.top().first;
que.pop();
if(vis[t])continue;
vis[t]=true;
ans+=td;
num++;
for(int i=0;i<n;i++){
if(!vis[i]){
que.push(P(d[t][i],i));
}
}
}
return ans;
}
int main(){
while(scanf("%d",&n)==1){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",d[i]+j);
}
}
int ans=prim();
printf("%d\n",ans);
}
return 0;
}
快速切题 poj1258的更多相关文章
- 快速切题sgu127. Telephone directory
127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...
- 快速切题sgu126. Boxes
126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...
- 快速切题 sgu123. The sum
123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...
- 快速切题 sgu120. Archipelago 计算几何
120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...
- 快速切题 sgu119. Magic Pairs
119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...
- 快速切题 sgu118. Digital Root 秦九韶公式
118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...
- 快速切题 sgu117. Counting 分解质因数
117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...
- 快速切题 sgu116. Index of super-prime bfs+树思想
116. Index of super-prime time limit per test: 0.25 sec. memory limit per test: 4096 KB Let P1, P2, ...
- 快速切题 sgu115. Calendar 模拟 难度:0
115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...
随机推荐
- 利用.bat(批处理)来删除KEIL编译生成的无用文件
新建一个.txt文件. 在里面输入如下内容: del *.bak /s del *.ddk /s del *.edk /s del *.lst /s del *.lnp /s del *.mpf /s ...
- 小工具:word表格文字转化成insert语句
群里的一个朋友有个需要,要让把word里的表格数据插入到数据库里面. 我的思路是,把格式化的数据转成insert语句,然后去执行就可以了. 要求的insert语句格式是:'insert into xx ...
- Docker 下安装 Spark
1. 安装Docker, 见上篇. 2. 安装ubuntu: docker run --name dcSpark ubuntu 3. 运行 Bash: docker exec -ti d ...
- 将DevExpress.Utils.ImageCollection变量的image导出
private void tspBtnExportExcel_Click(object sender, EventArgs e) { //暂时用来导出图片 string filePath = Syst ...
- HDU 3065 病毒侵袭持续中(AC自动机(每个模式串出现次数))
http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:求每个模式串出现的次数. 思路: 不难,把模板修改一下即可. #include<iostrea ...
- UVa 1343 旋转游戏(dfs+IDA*)
https://vjudge.net/problem/UVA-1343 题意:如图所示,一共有8个1,8个2和8个3,如何以最少的移动来使得中间8个格子都为同一个数. 思路:状态空间搜索问题. 用ID ...
- 关于ArrayList.clear()与=null以及new ArrayList<E>()
ArrayList是常用到的JCF类,用来保存类型相同的一组对象,并通过下标来快速访问指定对象.今天讨论的是当我们使用完ArrayList后应该选择怎样合适的处理方式. 这里现在有三种方式如下: 1. ...
- 提高Intellij创建Maven工程的速度
按照默认的方式创建Maven工程的时候会发现Maven插件加载的很慢如下 解决方法:在创建的过程中,在Properties中添加一个参数archetypeCatalog=internal . 因为ar ...
- shell until 循环
until 循环 格式: until condition do command done #输出0-9 #!/bin/bash a=0 until [ ! $a -lt 10 ] do echo ...
- 定义c/c++全局变量/常量几种方法的区别
转自:http://www.cnblogs.com/yaozhongxiao/archive/2010/08/08/1795338.html 1. 编译单元(模块): 在ide开发工具大行其道的今天, ...