坑!!!我还以为一个整数会截到两行!!
Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40056   Accepted: 16303

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

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的更多相关文章

  1. 快速切题sgu127. Telephone directory

    127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...

  2. 快速切题sgu126. Boxes

    126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...

  3. 快速切题 sgu123. The sum

    123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...

  4. 快速切题 sgu120. Archipelago 计算几何

    120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...

  5. 快速切题 sgu119. Magic Pairs

    119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...

  6. 快速切题 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 ...

  7. 快速切题 sgu117. Counting 分解质因数

    117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...

  8. 快速切题 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, ...

  9. 快速切题 sgu115. Calendar 模拟 难度:0

    115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...

随机推荐

  1. Python3基础 list 推导式 生成与已知列表等长度+元素为0的列表

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. Go第一篇之轻松入门

    Go语言简介 Go语言简史 Go 语言(或 Golang)是 Google 在 2007 年开发的一种开源编程语言,于 2009 年 11 月 10 日向全球公布.Go 是非常年轻的一门语言,它的主要 ...

  3. 牌型种数|2015年蓝桥杯B组题解析第七题-fishers

    牌型种数 小明被劫持到X赌城,被迫与其他3人玩牌. 一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张. 这时,小明脑子里突然冒出一个问题: 如果不考虑花色,只考虑点数,也不考虑自己得 ...

  4. datagridview控件的使用

    http://home.cnblogs.com/group/topic/40730.html datagridview定位到最后一行的方法 this.dataGridView2.CurrentCell ...

  5. jquery 之 extend的实现

    function getOpt(target, obj1, obj2, obj3){ $.extend(target, obj1, obj2, obj3); return target; } var ...

  6. 查找SQL 存储过程、触发器、视图!

    ALTER proc [dbo].[SP_SQL](@ObjectName sysname)  as  set nocount on ;  declare @Print nvarchar(max)-- ...

  7. dataframe使用笔记

    dates=pd.date_range(',periods=6) #创建固定频度的时间序列 df=pd.DataFrame(np.random.randn(6,4),index=dates,colum ...

  8. list_01

    双向链表 不支持随机存取([?] / at(?)) A.头尾 添加/移除 A.1.list::push_back(elemValue); A.2.list::pop_back(); A.3.list: ...

  9. Java内存管理知识你需要吗?

    0.Java 对内存的划分: Java虚拟机规范将物理内存(主内存和CPU中的缓存.寄存器)划分为程序计数器.Java 虚拟机栈.本地方法栈.Java 堆.方法区五个区域,但并没有规定这些区域的具体实 ...

  10. pv、uv、ip、tps、qps 等术语简单释义

    跟网站打交道,经常可以听到数据分析之类的专有名词,如pv多少.ip多少.tps多少之类的问题.下面就这些常见的数据给出英文全称及其释义. PV 即 page view,页面浏览量,用户每一次对网站中的 ...