POJ 1251 Jungle Roads
题意:嗯……没看题……看了眼图……求个最小生成树。
解法: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的更多相关文章
- 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 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为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- POJ 1251 Jungle Roads (最小生成树)
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- 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 ...
- POJ 1251 Jungle Roads (zoj 1406) MST
传送门: http://poj.org/problem?id=1251 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=406 P ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- [ 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求最小 ...
随机推荐
- Amazon Interview Question: Design an OO parking lot
Design an OO parking lot. What classes and functions will it have. It should say, full, empty and al ...
- 李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片
李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片 源码: // // ViewController.m // 08-九宫格扩展 // // Created by 李洪强 ...
- [hackerrank]The Love-Letter Mystery
https://www.hackerrank.com/contests/w3/challenges/the-love-letter-mystery 简单题. #include <cstdlib& ...
- Unix编程之size_t、ssize_t
http://blog.csdn.net/lalor/article/details/7426184 首先,我非常肯定以 及确定的告诉你ssize_t是有符号整型,在32位机器上等同与int,在64位 ...
- Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Apache编译与安装 RedHat enterprises 6.2
引自:http://blog.chinaunix.net/uid-26881541-id-3336614.html http://apr.apache.org/download.cgi 命令: yum ...
- Use Eclipse to develop groovy[docs.codehaus.org]
http://docs.codehaus.org/display/GROOVY/Install+Groovy-Eclipse+Plugin http://docs.codehaus.org/displ ...
- UML与数据流图
Ref: <数据库设计理论及应用(3)——需求分析及数据>http://wenku.baidu.com/link?url=hbhJFytMKT8A ...
- C#语句及案例
今天学习了,C#语句部分的分支语句,差点转不过弯来. 语句分类: 1.顺序语句 2.选择语句(分支语句) 3.循环语句 分支语句 (一)if(){} ; 按照顺序哪个if条件适合,执行哪个. 不合适就 ...
- python 批量更换图片格式脚本
问题:将某文件下的所有jpg的图片更换为png的图片 简单的实现: # -*- coding:utf-8 -*- from os.path import splitext import glob fr ...