题意:嗯……没看题……看了眼图……求个最小生成树。

解法:kruskal。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long using namespace std; struct node
{
int u, v, val;
bool operator < (const node &tmp) const
{
return val < tmp.val;
}
}edge[10005];
int father[105];
int Find(int a)
{
if(a != father[a]) father[a] = Find(father[a]);
return father[a];
}
bool Union(int a, int b)
{
int c = Find(a), d = Find(b);
if(c == d) return false;
father[c] = d;
return true;
}
int main()
{
int n;
while(~scanf("%d", &n) && n)
{
int cnt = 0;
for(int i = 1; i <= n; i++)
father[i] = i;
for(int i = 1; i < n; i++)
{
char ch[2];
int k;
scanf("%s%d", ch, &k);
int u = ch[0] - 'A' + 1;
while(k--)
{
int x;
scanf("%s%d", ch, &x);
edge[cnt].u = u;
edge[cnt].v = ch[0] - 'A' + 1;
edge[cnt++].val = x;
}
}
sort(edge, edge + cnt);
int ans = 0;
for(int i = 0; i < cnt; i++)
{
if(Union(edge[i].u, edge[i].v)) ans += edge[i].val;
}
printf("%d\n", ans);
}
return 0;
}

  就不告诉你们是2421的代码改的……

POJ 1251 Jungle Roads的更多相关文章

  1. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  2. POJ 1251 Jungle Roads - C语言 - Kruskal算法

    Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...

  3. POJ 1251 Jungle Roads (prim)

    D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Su ...

  4. POJ 1251 Jungle Roads(最小生成树)

    题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接全部村子的最短路径 还是裸的最小生成树咯 ...

  5. POJ 1251 Jungle Roads (最小生成树)

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  6. POJ 1251 Jungle Roads(Kruskal算法求解MST)

    题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...

  7. POJ 1251 Jungle Roads (zoj 1406) MST

    传送门: http://poj.org/problem?id=1251 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=406 P ...

  8. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  9. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题六 最小生成树 POJ 1251 Jungle Roads

    题意: 有n个点 每个点上有一些道路 求最小生成树 解释下输入格式 A n v1 w1 v2 w2 A点上有n条边 A到v1权值是w1 A到v2权值是w2 思路: 字符串处理之后跑kruskal求最小 ...

随机推荐

  1. Javascript通过className选择元素

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. HDU 4143 A Simple Problem(枚举)

    题目链接 题意 : 就是给你一个数n,让你输出能够满足y^2 = n +x^2这个等式的最小的x值. 思路 : 这个题大一的时候做过,但是不会,后来学长给讲了,然后昨天比赛的时候二师兄看了之后就敲了, ...

  3. maven_Error building POM (may not be this project's POM)错误

    如果maven项目在执行编译等操作时报如题错误的话,请仔细检查pom.xml,一般是由pom的语法错误导致的,例如我的项目是因为: dependencies 元素下不应该有properties元素导致 ...

  4. java生成二维码的三个工具

    1.  使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode ...

  5. 用Delphi创建服务程序

    用Delphi创建服务程序 日期:2005年11月29日 作者:sunmohe 人气: 3154 查看:[大字体 中字体 小字体] Windows 2000/XP和2003等支持一种叫做"服 ...

  6. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  7. Servlet中如何实现页面转发

    在Servlet中实现页面转发主要是利用RequestDispatcher接口实现的.此接口可以把一个请求转发到另一个JSP页面上.     forward():把请求转发到服务器上的另一个资源.   ...

  8. java抓取动态生成的网页

    最近在做项目的时候有一个需求:从网页面抓取数据,要求是首先抓取整个网页的html源码(后期更新要使用到).刚开始一看这个简单,然后就稀里哗啦的敲起了代码(在这之前使用过Hadoop平台的分布式爬虫框架 ...

  9. linux PATH环境变量

    $PATH:决定了shell将到哪些目录中寻找命令或程序,PATH的值是一系列目录,当您运行一个程序时,Linux在这些目录下进行搜寻编译链接 shell下输出path值: echo $PATH: w ...

  10. php库Faker

    Faker License : MIT Source Code Allo点评:Faker是一个很神奇的项目,会自动生成拟真的数据,包括用户资料.长文本.IP.日期等等,在网站上线前测试时非常好用. g ...