题目链接:

  FZu Problem 2233 ~APTX4869

题目描述:

  给一个n*n的矩阵,(i, j)表示第 i 种材料 和 第 j 种材料的影响值,这个矩阵代表这n个物品之间的影响值。当把这n个物品分成两部分后,每部分内部材料不会相互影响,但是不同部分的材料之间会相互影响。问如何分割使得两部分材料相互之间的最小影响值最大?

解题思路:

  材料内部不会影响,那么只需要把影响值小的物品放在同一部分即可,所以用结构体保存物品之间的影响值,然后sort一下,影响值小的物品用并查集放在一个集合,当集合等于2的时候,遍历到物品分别在不同集合的影响值就是ans。

 #include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define lson 2*root
#define rson 2*root+1
typedef __int64 LL;
const LL mod = ;
const LL INF= 1e9+;
const int maxn = ; struct node
{
int x, y, z;
}a[maxn*maxn];
int vis[maxn];
bool cmp (node a, node b)
{
return a.z < b.z;
} void init ()
{
for (int i=; i<maxn; i++)
vis[i] = i;
} int find (int x)
{
if (vis[x] != x)
vis[x] = find(vis[x]);
return vis[x];
}
int main ()
{
int n;
while (scanf ("%d", &n) != EOF)
{
init ();
int m = ;
for (int i=; i<n; i++)
for (int j=; j<n; j++)
{
scanf ("%d", &a[m].z);
if (i < j)
{
a[m].x = i;
a[m ++].y = j;
}
}
sort (a, a+m, cmp); int ans = INF, cnt = n;
for (int i=; i<m; i++)
{
int x = find (a[i].x);
int y = find (a[i].y); if (x == y)
continue; if (x != y && cnt > )
{
vis[x] = y;
cnt --;
}
else
ans = min (ans, a[i].z); if (ans != INF)
break;
} printf ("%d\n", ans);
}
return ;
} /*
4
-1 100 200 300
100 -1 400 500
200 400 -1 600
300 500 600 -1
*/

FZu Problem 2233 ~APTX4869 (并查集 + sort)的更多相关文章

  1. Problem 2233 ~APTX4869

    Problem 2233 ~APTX4869 Accept: 55    Submit: 176Time Limit: 1000 mSec    Memory Limit : 32768 KB Pro ...

  2. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  3. D. The Door Problem 带权并查集

    http://codeforces.com/contest/776/problem/D 注意到每扇门都有两个东西和它连接着,那么,如果第i扇门的状态是1,也就是已经打开了,那么连接它的两个按钮的状态应 ...

  4. SOJ4480 Easy Problem IV (并查集)

    Time Limit: 3000 MS Memory Limit: 131072 K Description 据说 你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过六个人你就能够认识任 ...

  5. HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)

    题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题 题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...

  6. CF954I Yet Another String Matching Problem(FFT+并查集)

    给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)子串与\(T\)的距离 两个等长的串的距离定义为最少的,将某一个字符全部视作另外一个字符的次数. \(|T|<=|S|<= ...

  7. 2019-2020 ACM-ICPC Brazil Subregional Programming Contest Problem A Artwork (并查集)

    题意:有一个矩形,有\(k\)个警报器,警报器所在半径\(r\)内不能走,问是否能从左上角走到右下角. 题解:用并查集将所有相交的圆合并,那么不能走的情况如下图所示 所以最后查询判断一下即可. 代码: ...

  8. POJ 2377 (并查集+sort求最远路)

    Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...

  9. 并查集 & 最小生成树详细讲解

    并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集?     并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...

随机推荐

  1. ActionFilterAttribute之HtmlFilter,压缩HTML代码

    当开启这个过滤器后,最终生成的HTML代码将会被压缩一下,在流量很大的网站中,能减少带宽成本就减少一点,何乐而不为? [csharp] view plaincopy using System; usi ...

  2. Xammp修改端口

    How can I get XAMPP working on port 80 under Windows 10? By default, Windows 10 starts Microsoft IIS ...

  3. MySQL(6)--复制,docker容器中

    MySQL5.7.11实现replication 启动两个安装好mysql的空的docker image ----------------- shell1  master $docker run -i ...

  4. Linux 及 CentOS系统安装

    VMware与Centos系统安装   今日任务 .Linux发行版的选择 .vmware创建一个虚拟机(centos) .安装配置centos7 .xshell配置连接虚拟机(centos) 选择性 ...

  5. sql建表,建索引注意事项

    建表注意 .建议字段定义为NOT NULL 搜索引擎 MyISAM InnoDB 区别 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基 ...

  6. Lambda 闭包 匿名 函数 类

    深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法) - _Luc_ - 博客园 https://www.cnblogs.com/figure9/p/java-8 ...

  7. pyspark mongodb yarn

    from pyspark.sql import SparkSession my_spark = SparkSession \ .builder \ .appName("myApp" ...

  8. rails使用mysql数据库

    简单步骤 1,安装mysql 安裝 MySQL Ubuntu 上安裝 MySQL 請執行: $ sudo apt-get install mysql-server mysql-common mysql ...

  9. scrollView 代理方法的实现顺序的些许区别

  10. react native 之页面跳转

    第一章  跳转的实现 1.component  中添加这行代码 <View style={styles.loginmain}> <Text style={styles.loginte ...