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 ...
随机推荐
- @Resource和@Autowired的区别
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...
- 6.JAVA_SE复习(集合)
集合 结构图: 总结: 1.集合中的元素都是对象(注意不是基本数据类型),基本数据类型要放入集合需要装箱. 2.set与list的主要区别在于set中不允许重复,而list(序列)中可以有重复对象. ...
- L004-oldboy-mysql-dba-lesson04
L004-oldboy-mysql-dba-lesson04 [root@web01 mysql]# mysql -uroot -ptestpassword -S /tmp/mysql.soc ...
- Java调用CMD命令
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是执行完d ...
- DTCMS更改图片相册上传图片类型,手机上传图片相册
/admin/js/uploader.js 中 filetypes: "jpg,jpge,png,gif", //文件类型 改为 filetypes: "jpg,jpeg ...
- Windows平台下为Python添加MySQL支持
到Python官网下载MySQL-python-1.2.5.win32-py2.7.exe 安装MySQL-python-1.2.5.win32-py2.7 附 64位MySQL-python下载地址 ...
- discuz x3插件开发傻瓜图文教程,用demo说话
此demo功能是在模板footer部位插入一段javascript代码,这段代码可以是alert提示,也可以是加载广告等等. 第一步: 在config\config_global.php 文件里设置$ ...
- Chrome浏览器安装插件提示(net::ERR_NAME_NOT_RESOLVED)
在chrome的webstore中安装currently插件.使用goagentFQ后能正常访问,但出现"net::ERR_NAME_NOT_RESOLVED"错误. 该错误的含义 ...
- AvalonDock 2.0 的简单运用
最近在研究AvalonDock的一些使用,碰到了一些问题.现在拿出来跟大家分享分享. 网上找了一大把AvalonDock 1.3版本的资料,弄出Demo后发现属性面板(DockableContent) ...
- fedora 取消自动升级
fedora启动时经常CPU使用很高,看下进程原来是yumBackend.这是个yum的自动更新进程 只要关闭自动更新就可以了 gsettings set org.gnome.settings-dae ...