坑!!!我还以为一个整数会截到两行!!
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. [c/c++]指针(4)

    现在讲一下指针的主要用途和常见错误. [用途] 1.用作形参 首先,常见新手写交换函数: void swap(int a,int b){ int t = a; a = b; b = t; } 主函数里 ...

  2. CEF之CefSettings设置日志等级

    CefSettings结构体允许定义全局的CEF配置,经常用到的配置项如下: single_process 设置为true时,Browser和Renderer使用一个进程.此项也可以通过命令行参数“s ...

  3. ubuntu16.04下firefly rk3288的编译安卓4.4

    一.背景 OS: ubuntu 16.04 二.配置交叉编译环境 2.1 安装openjdk sudo apt-get install openjdk-7-jdk 2.2 使在同一台机器上可以编译an ...

  4. IntelliJ IDEA 设置代码提示或自动补全的快捷键(Alt+/)

    点击 文件菜单(File) –> 点击 设置(Settings… Ctrl+Alt+S), –> 打开设置对话框.在左侧的导航框中点击 KeyMap. 接着在右边的树型框中选择 Main ...

  5. 【TCP/IP协议 卷一:协议】第三章 IP:网际协议

    3.1 引言 unreliable不可靠的意思是它并不能保证IP数据报能成功的到达目的地.IP只提供尽力而为的传输服务. conectionless无连接的意思是IP并不维护任何关于后续数据报的状态信 ...

  6. linux下带有空格的文件怎么删除

    如:hello world文件 第一种方式 先用 ls -i 得到 hello world 的inod(就是最前面的数字)假设这个数字是123,然后find . -inum -exec rm {} \ ...

  7. Python day4_list的常见方法1_笔记(浅拷贝和深拷贝的简述)

    li=[1,2,3,'55dd'] li.clear()#清除列表内容 print(li) li.append(1)#追加 li.append(3) print(li) #拓展:直接赋值和copy和d ...

  8. Windows 2012 R2 创建AD域

    创建复数的域控制器,容错的同时(一台AD故障),且能提高用户的登录效率. 为了实现负载平衡,域配置前,两台Ad域的DNS应该按如下设置,同时,也为了避免在AD02上,选择“将域控制器添加到现有域”时出 ...

  9. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  10. Codeforces 595D - Max and Bike

    595D - Max and Bike 思路:开始和结束时的计时器的高度相同时(也就是关于圆竖着直径对称)时间最少. 证明: 总距离为d. 圆周长为s=2*π*r. 设len=d-floor(d/s) ...