【并查集+dp】Team

Team

时间限制: 1 Sec  内存限制: 128 MB
提交: 124  解决: 10
[提交] [状态] [命题人:admin]

题目描述

ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members will join the FZU ACM team. How to form teams becomes a big problem.
There are 3N members this year. Each member has a power Wi. If three distinct members x, y, z form a team, the power of this team is the minimum value of Wx, Wy, Wz.
There are M pairs of relationship called best friend. If member A and member B are best friend, they must be in the same team.
We want to form N teams, and get the maximum sum of the power of these N teams. Can you help us?

 

输入

Input is given from Standard Input in the following format:
N M
W1 W2 . . . W3N
A1 B1
.
.
.
AM BM
Constraints
1≤N, M≤103
1≤Wi≤109
1≤Ai, Bi≤3N (1≤i≤M, Ai≠Bi)
All inputs are integers.

 

输出

If it’s possible to form N teams, print one integer denotes the maximum sum of the power of these N teams. Otherwise print -1.

 

样例输入

2 1
1 2 3 4 5 6
3 4

样例输出

4

题目大意:

      先输入两个整数n和m,代表有n个人,m组朋友关系,下面一行输入n个整数,代表每个人的能力值,然后输入m行关系a,b,代表a和b是好朋友,现在要将他们进行分队,每队固定为3个人,并且每队的能力值为三个人中最小的能力值,即对于队伍1:a,b,c,其队伍的能力为w[a],w[b],w[c]三者的最小值,同时也规定如果a和b是朋友关系,则a,b一定要在同一个队伍里,问组成队伍的最大能力值之和。

解题思路:

       首先确定的是朋友关系的一定须在同一个队伍里,所以可以先按照朋友关系先将当前的情况分为三种:一人一个队,两人一个队,三人一个队,因此可以通过并查集来实现,将为朋友关系的人放到同一个集合中,并将这个集合的权值设为集合中元素能力值的最小值,如果出现了集合中的元素大于3的情况,那么这种情况一定不成立,然后根据其集合人数将其分为1人,2人,3人三种情况,分别存在数组中。

      dp[i][j]代表的是已经在一人集合中选取了i个,在两人集合中选取了j个,则若要组成一个三人小队,可以分为两种情况,选三个一人队,选一个一人一个两人队,因此可以推出动态转移方程为:

dp[i+3][j]=max(dp[i+3][j],dp[i][j]+min(v[1][i],min(v[1][i+1],v[1][i+2]));

dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+min(v[1][i],v[2][j]));

假设:一人队数组长度为len1,两人队数组长度为len2,则我们可以知道若dp[len1][len2]==0,则证明此种情况不成立,组不成全为三人队,若其不为0,则用当前的dp[len1][len2]加上三人队集合的能力值即可。

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
ll w[3100];
int f[3100];
ll num[3100];
ll dp[3100][3100];
vector<ll> v[10];
bool ju;
int find(int x)
{return x==f[x]?x:find(f[x]);
}
void merge(int a,int b)
{int fa=find(a);int fb=find(b);if(fa!=fb) {if(fa<fb) {f[fb]=fa;num[fa]=num[fa]+num[fb];w[fa]=min(w[fa],w[fb]);if(num[fa]>3) ju=false;num[fb]=0;}else {f[fa]=fb;num[fb]=num[fb]+num[fa];w[fb]=min(w[fa],w[fb]);if(num[fb]>3) ju=false;num[fa]=0;}}
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int n,m;cin>>n>>m;rep(i,1,3*n) {cin>>w[i];f[i]=i;num[i]=1;}ju=true;rep(i,1,m) {int a,b;cin>>a>>b;merge(a,b);}if(ju==false) {cout<<"-1"<<endl;return 0;}rep(i,1,3*n) {if(num[i]==1) v[1].push_back(w[i]);if(num[i]==2) v[2].push_back(w[i]);if(num[i]==3) v[3].push_back(w[i]);}int len1=v[1].size();int len2=v[2].size();int len3=v[3].size();sort(v[1].begin(),v[1].end());sort(v[2].begin(),v[2].end());sort(v[3].begin(),v[3].end());v[1].push_back(0);v[1].push_back(0);v[1].push_back(0);v[1].push_back(0);v[2].push_back(0);v[2].push_back(0);v[2].push_back(0);v[2].push_back(0);for(int i=0;i<=len1;i++) {for(int j=0;j<=len2;j++) {dp[i+3][j]=max(dp[i+3][j],dp[i][j]+v[1][i]);dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+min(v[1][i],v[2][j]));}}if(dp[len1][len2]!=0) {ll ans=dp[len1][len2];for(int i=0;i<len3;i++) ans+=v[3][i];cout<<ans<<endl;}else {cout<<"-1"<<endl;}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536289.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

题目链接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 题目大意&#xff1a;Mario从第1站出发&#xff0c;目的是在时刻T会见车站 nnn 的一个间谍。由于在车站等待容易被抓&#xff0c;所以应尽量躲在开动的火车上&#xff0c;即在车站等待的时间最短&#xff0c;且Ma…

HDU1284——钱币兑换问题【dp】

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14528 Accepted Submission(s): 8784 Problem Description 在一个国家仅有1分&#xff0c;2分&#xff0c;3分硬币&#xff0c;将钱N兑换成硬…

HDU6092——Rikka with Subset 【dp】

题目网址&#xff1a; https://vjudge.net/problem/HDU-6092 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has nn positive A1−AnA1−An and their sum is mm…

洛谷P1725琪露诺【单调队列+dp】

题目描述 在幻想乡&#xff0c;琪露诺是以笨蛋闻名的冰之妖精。 某一天&#xff0c;琪露诺又在玩速冻青蛙&#xff0c;就是用冰把青蛙瞬间冻起来。但是这只青蛙比以往的要聪明许多&#xff0c;在琪露诺来之前就已经跑到了河的对岸。于是琪露诺决定到河岸去追青蛙。 小河可以看…

扶桑号战列舰【RMQ+分治】

扶桑号战列舰 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 197 解决: 63 [提交] [状态] [命题人:admin] 题目描述 众所周知&#xff0c;一战过后&#xff0c;在世界列强建造超无畏级战列舰的竞争之中&#xff0c;旧日本海军根据“个舰优越主义”&#xff0c;建造了扶…

大凤号装甲空母【找规律+矩阵快速幂】

大凤号装甲空母 时间限制: 1 Sec 内存限制: 128 MB 提交: 108 解决: 15 [提交] [状态] [命题人:admin] 题目描述 大凤号航空母舰很喜欢算术。 它&#xff0c;是旧日本海军中最为先进的航空母舰。 它&#xff0c;是旧日本海军中最为短命的航空母舰。 同时&#xff0c;她还是最平…

codevs5429 多重背包【多重背包+单调队列】

5429 多重背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 你有一个容量为M的背包&#xff0c;和N种物品。 每种物品都有三个属性&#xff0c;vi&#xff0c;wi&#xff0c;与ci&#xff0c;分别表示这种物品的体积、价值和件数。 你的…

generator 1【矩阵快速幂】

题目描述 You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0​,x1​,a,b. And you know xia⋅xi−1b⋅xi−2x_i a \cdot x_{i-1} b \cdot x_{i-2}xi​a⋅xi−1​b⋅xi−2​ for all i≥2i \ge 2i≥2. Given two positive integers n, and MOD, please calcul…

Give Candies【快速幂+欧拉】

Give Candies 时间限制: 1 Sec 内存限制: 128 MB 提交: 243 解决: 92 [提交] [状态] [命题人:admin] 题目描述 There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the childr…

Save the Room【找规律】

Save the Room 时间限制: 1 Sec 内存限制: 128 MB 提交: 149 解决: 90 [提交] [状态] [命题人:admin] 题目描述 Bob is a sorcerer. He lives in a cuboid room which has a length of A, a width of B and a height of C, so we represent it as ABC. One day, he finds that …

Participate in E-sports【Java大数+二分】

Participate in E-sports 时间限制: 2 Sec 内存限制: 128 MB 提交: 194 解决: 53 [提交] [状态] [命题人:admin] 题目描述 Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to…

Poor God Water【矩阵快速幂】

Poor God Water 时间限制: 1 Sec 内存限制: 128 MB 提交: 102 解决: 50 [提交] [状态] [命题人:admin] 题目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonou…

Transport Ship【多重背包】

Transport Ship 时间限制: 1 Sec 内存限制: 128 MB 提交: 175 解决: 65 [提交] [状态] [命题人:admin] 题目描述 There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship i…

洛谷P2015 二叉苹果树【树形dp】

P2015 二叉苹果树 时间限制 1.00s 内存限制 125.00MB 题目描述 有一棵苹果树&#xff0c;如果树枝有分叉&#xff0c;一定是分2叉&#xff08;就是说没有只有1个儿子的结点&#xff09; 这棵树共有N个结点&#xff08;叶子点或者树枝分叉点&#xff09;&#xff0c;编号为1-N,…

洛谷P2014【树形dp】

P2014 选课 时间限制 1.00s 内存限制 125.00MB 题目描述 在大学里每个学生&#xff0c;为了达到一定的学分&#xff0c;必须从很多课程里选择一些课程来学习&#xff0c;在课程里有些课程必须在某些课程之前学习&#xff0c;如高等数学总是在其它课程之前学习。现在有N门功课&…

洛谷P2016 战略游戏【树形dp】

P2016 战略游戏 时间限制 1.00s 内存限制 125.00MB 题目描述 Bob喜欢玩电脑游戏&#xff0c;特别是战略游戏。但是他经常无法找到快速玩过游戏的办法。现在他有个问题。 他要建立一个古城堡&#xff0c;城堡中的路形成一棵树。他要在这棵树的结点上放置最少数目的士兵&#x…

Shell Pyramid【数学+二分】

Shell Pyramid 时间限制: 1 Sec 内存限制: 128 MB 提交: 291 解决: 95 [提交] [状态] [命题人:admin] 题目描述 In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at tha…

Degree Sequence of Graph G【模拟】

Degree Sequence of Graph G 时间限制: 1 Sec 内存限制: 128 MB 提交: 362 解决: 92 [提交] [状态] [命题人:admin] 题目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…

Simple Addition expression【打表+二分】

Simple Addition expression 时间限制: 1 Sec 内存限制: 128 MB 提交: 355 解决: 80 [提交] [状态] [命题人:admin] 题目描述 A luxury yacht with 100 passengers on board is sailing on the sea in the twilight. The yacht is ablaze with lights and there comes out laug…