每个世界可以和别的世界连通,也可以直接联通虚拟的已经毁灭的世界,这样变成一个最小生成树问题。

但是好像哪里不对?

有人用dp过掉的?

不太清楚怎么搞的。

其实就是最小生成树……

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; inline int read() {
int x=0;
char c;
do {
c=getchar();
} while(c<'0'||c>'9');
do {
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
} while(c>='0'&&c<='9');
return x;
} inline void _write(int x) {
if(x>9)
_write(x/10);
putchar(x%10+'0');
} inline void write(int x) {
_write(x);
putchar('\n');
} /* Kruskal begin */ const int MAXN=505;
const int MAXM=126005; struct Edge {
int u,v,w;
} edge[MAXM]; int tol; inline void add_edge(int u,int v,int w) {
edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
} bool cmp(Edge a,Edge b) {
return a.w<b.w;
} int F[MAXN];
inline int find(int x) {
int r=F[x];
while(F[r]!=r)
r=F[r];
while(F[x]!=r) {
int t=F[x];
F[x]=r;
x=t;
}
return r;
} inline int Kruskal(int n) {
for(int i=1; i<=n; i++)
F[i]=i;
sort(edge,edge+tol,cmp);
int cnt=0;
int ans=0;
for(int i=0; i<tol; i++) {
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w; int t1=find(u);
int t2=find(v);
if(t1!=t2) {
ans+=w;
F[t1]=t2;
cnt++;
if(cnt==n-1)
return ans;
}
}
//不连通
return -1;
} /* Kruskal end */ int main() {
#ifdef Yinku
freopen("Yinku.in","r",stdin);
#endif // Yinku
int n=read();
for(int i=1; i<=n; ++i) {
int c=read();
add_edge(n+1,i,c);
}
for(int i=1; i<=n; ++i) {
for(int j=1; j<=n; ++j) {
int c=read();
if(j>=i)
continue;
add_edge(i,j,c);
}
} write(Kruskal(n+1));
}

SCUT - 336 - 酋雷姆 - 最小生成树的更多相关文章

  1. 数据结构之 图论---最小生成树(prim + kruskal)

    图结构练习——最小生成树 Time Limit: 1000MS Memory limit: 65536K 题目描述  有n个城市,其中有些城市之间可以修建公路,修建不同的公路费用是不同的.现在我们想知 ...

  2. 世界城市 XML

    下载地址:http://www.qlcoder.com/uploads/dd01140921/147988679320159.xml <Location> <CountryRegio ...

  3. JS城市data

    CityData = { "中国": { "北京": ["东城区", "西城区", "崇文区", & ...

  4. js设计模式系列之(一)请节约你的请求-代理模式

    What’s the proxy pattern? 代理模式其实就是将违反单一性原则的类给抽离出来,尽量满足开放和封闭的原则. 相当于一个类的行为只是一种,但是你可以给这个类添加额外的行为.比如: 一 ...

  5. [bilibili]弹幕屏蔽列表

    <filters> <item enabled="true">t=定单身</item> <item enabled="true& ...

  6. 翻译Java虚拟机的结构

    英文原版:  https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html 直接谷歌翻译: Java SE规范 > Java虚拟机 ...

  7. HDUOJ----2489 Minimal Ratio Tree

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. Android中的数据结构

    数据结构在Android中也有着大量的运用,这里采用数据结构与源代码分析相结合,来认识Android的数据结构 线性表 线性表可分为顺序存储结构和链式存储结构 顺序存储结构-ArrayList 通过对 ...

  9. VIDENT iSmart900自动多系统扫描工具OBDII支持ABS / SRS / EPB /传输诊断DPF再生/上油复位编码电池配置

    Vident系列中有许多多功能产品,其中最好的是Vident iSmart 900.购买视频系列后,以下是用户的一些评论 乔:因为我想它很好用.该工具很容易更新.我将公制重量单位的代码放到工具中.很容 ...

随机推荐

  1. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  2. 我的Android进阶之旅------>直接拿来用!最火的Android开源项目

    转载于CSDN,相关链接如下: http://www.csdn.net/article/2013-05-03/2815127-Android-open-source-projects http://w ...

  3. CF A. DZY Loves Hash

    A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. 线程池ThreadPoolExcecutor介绍

    线程池ThreadPoolExecutor 使用Executors和ThreadPoolExecutor 并发新特性—Executor 框架与线程池

  5. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  6. 基于ajax的登录

    验证码 当登录一个网站的时候往往会有验证码. python生成随机验证码,需要使用到 PIL 模块 安装 : pip3 install pillow 1. 创建图片        我们现在写的验证码属 ...

  7. mysql的安装与基本管理

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS ...

  8. IOS 获取 文件(UIImage) 创建时间

    Image 在手机里 如果是手机拍的那么能使用ALAssetsLibrary获取一系列 图片的信息 颜色.GPS.尺寸.创建日期等 使用ALAssetsLibrary导入框架AssetsLibrary ...

  9. wamp server 安装后 Apache80端口占用

    提示:Your port 80 is actually used by :Server: Microsoft-HTTPAPI/2.0 解决方案:计算机->右键管理->服务和应用程序-> ...

  10. CodeForces - 960F Pathwalks —— 主席树(n棵线段树)

    题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...