【POJ】1251 Jungle Roads
题目链接:http://poj.org/problem?id=1251
题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离。求构建所有村庄道路的最短距离。
题解:最小生成树裸题。注意输入。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
int mp[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int n;
int prim(){
memset(dis,0x3f,sizeof(dis));
memset(vis,,sizeof(vis));
int ans = ;
dis[] = ;
while(){
int k = ;
for(int j = ; j <= n;j++){
if(!vis[j] && dis[j] < dis[k])
k = j;
}
if(!k) break;
vis[k] = ;
ans += dis[k];
for(int j = ; j <= n;j++){
if(dis[j] > mp[k][j]){
dis[j] = mp[k][j];
} }
}
return ans;
}
int main(){
int m, w;
char ch1,ch2;
while(cin >> n && n){
memset(mp,0x3f,sizeof(mp));
for(int i = ; i < n; i++)
mp[i][i] = ;
for(int i = ; i < n; i++){
cin>>ch1>>m;
int s = ch1 - 'A' + ;
while(m--){
cin>>ch2>>w;
int t = ch2 - 'A' + ;
mp[s][t] = mp[t][s] = w;
}
}
cout<<prim()<<endl;
}
return ;
}
【POJ】1251 Jungle Roads的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- 【Kruscal最小生成树】D. Jungle Roads
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/D [Accepted] #include<iostream> #in ...
- 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉
DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $ 当然这里的$i$和$k$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
随机推荐
- goroutine 需要注意的一个小细节
虽然goroutine 是并发执行的,但是它们并不是并行运行的.如果不告诉Go 额外的东西,同一时刻只会有一个goroutine 执行.利用runtime.GOMAXPROCS(n) 可以设置goro ...
- flask 的orm
https://www.cnblogs.com/chichung/p/9794702.html
- js实现点击按钮传值
js实现点击按钮传值 page1源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- Quartus II 使用 modelsim 仿真
转自:http://www.cnblogs.com/emouse/archive/2012/07/08/2581223.html Quartus 中调用modelsim的流程 1. 设定仿真工具 as ...
- Linux用户空间内存区域的匿名映射
1 在调用mmap系统调用时,可以指定的标志(flag)参数: 1: #define MAP_SHARED 0x01 /* Share changes */ 2: #define MAP_PRIVAT ...
- Python脚本轻松实现批量图片重命名
************************************************************************** 摘要:网上爬取的图片名字很混乱,格式也比较多,需要 ...
- python不同包之间调用时提示文件模块不存在的问题
python对于跨包调用函数时,经常会提示模块不存在的问题,主要是python程序执行时,搜索路径导致的,python程序执行的路径依次是: (1)程序根目录(2)环境变量(3)标准库目标(D:\Py ...
- 修改Tomcat的端口号方法
(1).查找conf路径下的server.xml文件,路径如: I: \tomcat6\apache-tomcat-6.0.32\conf\server.xml (2).打开server.xml文 ...
- 如何运行linux shell程序
原文地址:http://www.sohu.com/a/138822796_610671 首先,我们从一个十分简单的例子test.sh开始吧: #!/bin/sh #this is a test. cd ...
- 使用EditPlus批量修改文件编码格式
步骤一: 步骤二: 步骤三: 步骤四: