Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=10005;
int head[maxn];
int n,len=0,counter;
long long ans;
struct node{
int v,cost,u;
//操作符的重写默认是小于等于
bool operator < (const node& tmp) const{
return cost< tmp.cost;
}
}gra[maxn];
struct BingCha{
//并查集
int father[105];
void initial(){
//初始化不要忘记
for(int i=1;i<=n;i++){
father[i]=i;
}
}
int getfather(int v){
if(father[v]==v)return v;
return father[v]=getfather(father[v]);
//状态压缩
}
bool findtwo(int a,int b){
//看是否是同根的
return getfather(a)==getfather(b);
}
void unioned(int a,int b){
int fa=father[a],fb=father[b];
father[fa]=fb;
}
}jihe;
void addedge(int u,int v,int cost){
gra[len].u=u;
gra[len].v=v;
gra[len].cost=cost;
len++;
}
void init(){
len=0;
counter=1;ans=0;
int x;
memset(head,-1,sizeof(head));
//memset gra?
for(int i=1;i<=n;i++){//add the edges
for(int j=1;j<=n;j++){
scanf("%d",&x);
if(i<j){
addedge(i,j,x);
}
}
}
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
sort(gra,gra+len);
//已经有序,那么从小值开始选边添加即可,也保证了最优结果
int idx=0;
jihe.initial();
while(counter<n || idx<len){
if(!jihe.findtwo(gra[idx].u,gra[idx].v)){
//如果不同根,就选择这条边,执行更新操作
ans+=gra[idx].cost;
counter++;
jihe.unioned(gra[idx].u,gra[idx].v);
}
idx++;
}
printf("%d\n",ans);
}
}
2421 也是裸的mst:只要把已经建了的作为输入先处理一下就好了。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
;
int n,len,counter;
long long ans;
struct node{
int v,cost,u;
bool operator < (const node& tmp) const{
return cost< tmp.cost;
}
}gra[maxn*(maxn-)>>];
struct BingCha{
];
void initial(){
;i<=n;i++){
father[i]=i;
}
}
int getfather(int v){
if(father[v]==v)return v;
return father[v]=getfather(father[v]);
}
bool findtwo(int a,int b){
return getfather(a)==getfather(b);
}
void unioned(int a,int b){
int fa=getfather(a),fb=getfather(b);
father[fa]=fb;
}
}jihe;
void addedge(int u,int v,int cost){
gra[len].u=u;
gra[len].v=v;
gra[len].cost=cost;
len++;
}
int findxy(int x,int y){
if(x>y){int z=y;y=x;x=z;}
,id=;
;i<x;i++){
id+=j--;
}
y-=x;// here!!!
id+=y;
return id;
}
void init(){
len=;
counter=;ans=;
jihe.initial();
int x,q,y;
//memset gra?
;i<=n;i++){//add the edges
;j<=n;j++){
scanf("%d",&x);
if(i<j){
addedge(i,j,x);
}
}
}
scanf("%d",&q);
;i<=q;i++){
scanf("%d%d",&x,&y);
;
if(!jihe.findtwo(gra[idx].u,gra[idx].v)){
counter++;
jihe.unioned(gra[idx].u,gra[idx].v);
}
}
}
int main(){
while(scanf("%d",&n)!=EOF){
init();
sort(gra,gra+len);
;
while(counter<n){//|| idx<=len
if(!jihe.findtwo(gra[idx].u,gra[idx].v)){
ans+=gra[idx].cost;
counter++;
jihe.unioned(gra[idx].u,gra[idx].v);
}
idx++;
}
printf("%d\n",ans);
}
}
Agri Net POJ1258 && Constructing Roads POJ2421的更多相关文章
- HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏
Constructing Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- POJ2421 Constructing Roads
Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Constructing Roads (MST)
Constructing Roads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- Oracle 常用命令
一 管理用户 查询用户集合 select username from dba_users; A 查询某个用户是否存在 select username from dba_users where user ...
- ASP.NET的错误处理机制
对于一个Web应用程序来说,出错是在所难免的,因此我们应该未雨绸缪,为可能出现的错误提供恰当的处理.事实上,良好的错误处理机制正是衡量Web应用程序好坏的一个重要标准.试想一下,当用户不小心在浏览器输 ...
- xamarin.ios 豆瓣电台视频教程
视频中提到的网址: http://www.sufeinet.com/thread-655-1-1.html https://github.com/akfish/fm-terminal/blob/dev ...
- jQuery ajax 请求php遍历json数组到table中
html代码(test.html),js在html底部 <!DOCTYPE html> <html lang="en"> <head> < ...
- mac 上的版本控制工具SmartSVN9.0.4(破解版)
附带上破解版安装说明: 1.在MAC上选中SmartSVN.dmg,右键->打开2.双击syntevo_keygen.jar 如果没有安装java会自动提示安装的3.输入Name Email(随 ...
- sublime text2 配置php调试环境
步骤一: 首先确保你电脑安装了php,并把php设置到环境变量里了. 步骤二: 点击 sublime_text的“工具”->"编译系统"->"编译新系统&qu ...
- AJAX 原理(转摘)
在写这篇文章之前,曾经写过一篇关于AJAX技术的随笔,不过涉及到的方面很窄,对AJAX技术的背景.原理.优缺点等各个方面都很少涉及null.这次写这篇文章的背景是因为公司需要对内部程序员做一个培训.项 ...
- EntityFramework.Extended
记录 Entity Framework扩展,可以实现批量更新.删除,但需要EntityFramework6.0支持,需要支持低版本的EF,可下载该扩展的低版本. https://www.nuget.o ...
- K-meams文本聚类算法C++实现
FROM:http://www.cnblogs.com/finallyliuyu/archive/2010/09/03/1817348.html 头文件: #ifndef _Preprocess_H ...
- linux学习笔记(3)-文件系统
三大类文件类型 普通文件:包括文本文件.数据文件.可执行的二进制程序文件 目录文件:linux系统把目录看成一种特殊的文件,利用它构成了文件系统的树形结构 设备文件:把设备也看成是一个文件,例如你的鼠 ...