当前位置:网站首页>257. 关押罪犯
257. 关押罪犯
2022-06-24 19:43:00 【追寻远方的人】
S 城现有两座监狱,一共关押着 N 名罪犯,编号分别为 1∼N。
他们之间的关系自然也极不和谐。
很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突。
我们用“怨气值”(一个正整数值)来表示某两名罪犯之间的仇恨程度,怨气值越大,则这两名罪犯之间的积怨越多。
如果两名怨气值为 c 的罪犯被关押在同一监狱,他们俩之间会发生摩擦,并造成影响力为 c 的冲突事件。
每年年末,警察局会将本年内监狱中的所有冲突事件按影响力从大到小排成一个列表,然后上报到 S 城 Z 市长那里。
公务繁忙的 Z 市长只会去看列表中的第一个事件的影响力,如果影响很坏,他就会考虑撤换警察局长。
在详细考察了 N 名罪犯间的矛盾关系后,警察局长觉得压力巨大。
他准备将罪犯们在两座监狱内重新分配,以求产生的冲突事件影响力都较小,从而保住自己的乌纱帽。
假设只要处于同一监狱内的某两个罪犯间有仇恨,那么他们一定会在每年的某个时候发生摩擦。
那么,应如何分配罪犯,才能使 Z 市长看到的那个冲突事件的影响力最小?这个最小值是多少?
输入格式
第一行为两个正整数 N 和 M,分别表示罪犯的数目以及存在仇恨的罪犯对数。
接下来的 M 行每行为三个正整数 aj,bj,cj,表示 aj 号和 bj 号罪犯之间存在仇恨,其怨气值为 cj。
输出格式
输出共 1 行,为 Z 市长看到的那个冲突事件的影响力。
如果本年内监狱中未发生任何冲突事件,请输出 0。
数据范围
N≤20000,M≤100000
输入样例:
4 6
1 4 2534
2 3 3512
1 2 28351
1 3 6618
2 4 1805
3 4 12884
输出样例:
3512
代码:
/* (二分,染色法判断二分图) O((N+M)logC) 将罪犯当做点,罪犯之间的仇恨关系当做点与点之间的无向边,边的权重是罪犯之间的仇恨值。 那么原问题变成:将所有点分成两组,使得各组内边的权重的最大值尽可能小。 我们在 [0,109] 之间枚举最大边权 limit,当 limit 固定之后,剩下的问题就是: 判断能否将所有点分成两组,使得所有权值大于 limit 的边都在组间,而不在组内。也就是判断由所有点以及所有权值大于 limit 的边构成的新图是否是二分图。 判断二分图可以用染色法,时间复杂度是 O(N+M),其中 N 是点数,M 是边数 为了加速算法,我们来考虑是否可以用二分枚举 limit, 假定最终最大边权的最小值是 Ans: 那么当 limit∈[ans,109] 时,所有边权大于 limit 的边,必然是所有边权大于Ans 的边的子集,因此由此构成的新图也是二分图。 当 limit∈[0,ans−1] 时,由于ans 是新图可以构成二分图的最小值,因此由大于 limit 的边构成的新图一定不是二分图。 所以整个区间具有二段性,可以二分出分界点 ans 的值。 */
#include <bits/stdc++.h>
using namespace std;
const int N = 20010, M = 200010;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
int color[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool dfs(int u, int c, int mid)
{
color[u] = c;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (w[i] <= mid)
continue;
if (color[j])
{
if (color[j] == c)
return false;
}
else if (!dfs(j, 3 - c, mid))
return false;
}
return true;
}
bool check(int mid)
{
memset(color, 0, sizeof color);
for (int i = 1; i <= n; i++)
if (!color[i])
if (!dfs(i, 1, mid))
return false;
return true;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e9;
while (l < r)
{
int mid = r + l >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
printf("%d\n", r);
return 0;
}
边栏推荐
- websocket学习
- Laravel user authorization
- 数字IC设计经验整理(二)
- [text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]
- 【js】-【树】-学习笔记
- Financial management [3]
- 非单文件组件
- 2022 safety officer-a certificate examination questions and answers
- Non single file component
- Servlet
猜你喜欢

EPICS记录参考2--EPICS过程数据库概念

Design and implementation of spark offline development framework

【js】-【字符串-应用】- 学习笔记

Epics record reference 4 -- fields for all input records and fields for all output records

The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!

Tech talk activity review kubernetes skills of cloud native Devops

Non single file component

vulnhub DC: 2

01_SpingBoot 框架入门

InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog
随机推荐
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
对抗训练理论分析:自适应步长快速对抗训练
UNION ALL UNION FULL JOIN
数字IC设计经验整理(二)
[laravel series 7.9] test
Getting started with the go Cobra command line tool
02_SpingBoot 入门案例
laravel 添加helper文件
Super detailed cookie addition, deletion, modification and query
【js】-【链表-应用】-学习笔记
07_ Springboot for restful style
[postgraduate entrance examination English] prepare for 2023, learn list9 words
. Net 7 Preview 1 has been officially released
推送Markdown格式信息到釘釘機器人
jar中没有主清单属性
Second IPO of Huafang group: grown up in Zanthoxylum bungeanum, trapped in Zanthoxylum bungeanum
The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!
Record the range of data that MySQL update will lock
慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
EPICS记录参考3 -- 所有记录都有的字段