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数据类型对应Java类型
SQL数据类型 JDBC类型代码 标准的Java类型 Oracle扩展的Java类型 1.0标准的JDBC类型: CHAR java.sql.Types.CHAR java.lang.St ...
- Adapter模式
Adapter模式主要用于将一个类的接口转换为另外一个接口,通常情况下再不改变原有体系的条件下应对新的需求变化,通过引入新的适配器类来完成对既存体系的扩展和改造.实现方式主要包括: 1.类的Adapt ...
- windbg远程调试
1, A,调试机. B,被调试机. 2, 在B机上安装windbg,公共符号文件,程序的PDB都要复制过来. 公共符号文件位置设置在于A机相同的位置. windbg–server tcp:port=5 ...
- ios版弹珠游戏源码
这个是我们比较喜欢玩的一直小游戏的,ios版弹珠游戏源码,该游戏源码来着IOS教程网其他网友提供上传的,大家可以了解一下吧. nore_js_op> <ignore_js_op&g ...
- Android核心组件 Service
Service: 服务 Service 是Activity系统的核心组件之一. 因此需要继承和注册 Service 是内有界面的, 适合在后台长期的执行任务. (如放歌, 检测版本跟新, 下载, 上传 ...
- C# - write values to configuration file
using System.Configuration;System.Configuration.Configuration config = ConfigurationManager.OpenExeC ...
- laravel方法汇总详解
1.whereRaw() 用原生的SQL语句来查询,whereRaw('select * from user') 就和 User::all()方法是一样的效果 2.whereBetween() 查询时 ...
- ubuntu server版连接vpn服务器
命令行的方法: 1.要下载pptp的客户端 sudo apt-get install pptp-linux 2.创建连接 sudo pptpsetup –create vpn001 –s ...
- Ecshop与Jquery冲突的完美解决方案
ecshop把AJAX事件和JSON解析的模块放在common/transport.js之中,可以说它也有自己封装的一套工具,这其实是很正常的. 但恰恰的,在封装JSON各种方法的同时对objec ...
- 2016 Multi-University Training Contest 1 GCD RMQ+二分(预处理)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:有N(N <= 100,000),之后有Q(Q <= 100,000)个区间查询[ ...