(最小生成树) Jungle Roads -- POJ -- 1251
链接:
http://poj.org/problem?id=1251
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 21776 | Accepted: 10086 |
Description

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
Input
Output
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n, J[N][N], dist[N], vis[N]; int Prim()
{
int i, j, ans=;
dist[]=;
memset(vis, , sizeof(vis));
vis[]=; for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int index=, MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
index=j;
MIN=dist[j];
}
}
vis[index]=;
ans += MIN;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j])
dist[j]=J[index][j];
}
}
return ans;
} int main ()
{
while(scanf("%d", &n), n)
{
int i, j, b, t, m;
char ch; for(i=; i<=n; i++)
for(j=; j<=i; j++)
J[i][j]=J[j][i]=INF; for(i=; i<n; i++)
{
cin>>ch>>m;
for(j=; j<m; j++)
{
cin>>ch>>t;
b=ch-'A'+;
J[i][b]=J[b][i]=t;
}
}
int ans=Prim(); printf("%d\n", ans);
}
return ;
}
(最小生成树) Jungle Roads -- POJ -- 1251的更多相关文章
- A - Jungle Roads - poj 1251(简单)
想必看这道题的时候直接看数据还有那个图就能明白什么意思吧,说的已经很清楚了,每个点都有一些相连的点和权值,求出来如果连接所有点,最小的权值是多少,赤裸裸的最小生成树... ************** ...
- Jungle Roads POJ - 1251 模板题
#include<iostream> #include<cstring> #include<algorithm> using namespace std; cons ...
- 最小生成树Jungle Roads
这道题一定要注意录入方式,我用的解法是prime算法 因为单个字符的录入会涉及到缓冲区遗留的空格问题,我原本是采用c语言的输入方法录入数据的,结果对了,但是提交却一直wrong,后来改成了c++的ci ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- 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 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
随机推荐
- eclipse添加源码的另外一种方法
当我们使用maven或者gradle时,我们不需要担心源码的问题.Maven会帮我们下载jar包的同时下载对应的源码包.一般为source.jar,比如servlet-api-2.5-sources. ...
- 专业英语词汇(Java)
abstract (关键字) 抽象 ['.bstr.kt] access vt.访问,存取 ['.kses]‘(n.入口, ...
- 经典动态规划python实现
1.最长上升子序列 对于一个数字序列,请设计一个复杂度为O(nlogn)的算法,返回该序列的最长上升子序列的长度,这里的子序列定义为这样一个序列U1,U2...,其中Ui < Ui+1,且A[U ...
- node集成mysql——pool连接池
安装 mysql npm install mysql or cnpm install mysql 创建db.js,实现mysql操作模块 var mysql = require('mysql'); v ...
- jenkins 自动构建gitlab项目
安装的plugin: - kubernetes:1.7.1 - workflow-aggregator:2.5 - workflow-job:2.21 - credentials-b ...
- HTTP Response Code 中文详解
引自:https://blog.csdn.net/lplj717/article/details/70053560 1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应 ...
- Ajax的get方式传值 避免& 与= 号
js代码 例如: var name = $("#name”).value;//为a&b=7 name=encodeURLComponent(name); 可以将a&b=7转化 ...
- 解决SpringBoot中webScocket不能注入bean的问题
最近在做websocket聊天,但是遇到一个问题,就是在websocket的服务中要调用Service层的东西.首先我想到的是通过@Autowire注解来实现bean的注入.但是进过测试发现,注入的b ...
- 27-x的y次方的后三位数
题目内容: 输入描述 数据分n组,对于每组数据有两个正整数x和y(x的y次方必须大于100) 输出描述 对于每组输出,输出一个值,即x的y次方结果的最后三位数 提示:13的13次方为:30287510 ...
- C# Socket网络编程精华篇
我们在讲解Socket编程前,先看几个和Socket编程紧密相关的概念: TCP/IP层次模型 当然这里我们只讨论重要的四层 01,应用层(Application):应用层是个很广泛的概念,有一些基本 ...