题目链接:

  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
*/

最新文章

  1. Scalaz(17)- Monad:泛函状态类型-State Monad
  2. adding validation annotators to model classes 在linq to EntityFrame的Model中添加前台验证validation annotators
  3. IOS6学习笔记(二)
  4. javascript之delete操作符
  5. js 控制div 显示隐藏的问题
  6. js获取昨天日期
  7. 自学Xpath的几个例子
  8. validate验证
  9. Zabbix监控nginx性能
  10. Eclipse安装教程 ——史上最详细安装Java &amp;Python教程说明
  11. java发送http的get、post请求【备忘】
  12. ubuntu 语言设置
  13. 010-判断是否回传IsPostBack属性
  14. apache常用配置文件讲解
  15. 右键添加使用Sublime打开
  16. Docker创建虚机和swarm
  17. spark repartition
  18. e785. 监听JList中项的变动
  19. Android 捕获组合键
  20. windows vbs启动多个应用程序并使程序最小化(显示桌面)

热门文章

  1. WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView
  2. 给EasyUi的Form加入自己主动填充部分输入框的方法
  3. XJTUOJ wmq的A&#215;B Problem FFT/NTT
  4. createDocumentFragment 文档碎片提升dom增删的性能
  5. (linux)SD卡初始化-mmc_sd_init_card函数(续)
  6. bzoj3462: DZY Loves Math II
  7. 使用sql compare生成的sql语句
  8. linux初级学习笔记七:linux用户管理,密码和组命令详解!(视频序号:04_1)
  9. Ubuntu上命令行下卸载软件
  10. wireshark分析ssl协议