Codeforce 1255 Round #601 (Div. 2)B. Fridge Lockers(思维)
Hanh lives in a shared apartment. There are nn people (including Hanh) living there, each has a private fridge.
nn fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of nn people can open it.
For exampe, in the picture there are n=4n=4 people and 55 chains. The first person knows passcodes of two chains: 1−41−4 and 1−21−2. The fridge 11 can be open by its owner (the person 11), also two people 22 and 44 (acting together) can open it.
The weights of these fridges are a1,a2,…,ana1,a2,…,an. To make a steel chain connecting fridges uu and vv, you have to pay au+avau+av dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges.
Hanh's apartment landlord asks you to create exactly mm steel chains so that all fridges are private. A fridge is private if and only if, among nn people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge ii is not private if there exists the person jj (i≠ji≠j) that the person jj can open the fridge ii.
For example, in the picture all the fridges are private. On the other hand, if there are n=2n=2 fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).
Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly mm chains, and if yes, output any solution that minimizes the total cost.
Input
Each test contains multiple test cases. The first line contains the number of test cases TT (1≤T≤101≤T≤10). Then the descriptions of the test cases follow.
The first line of each test case contains two integers nn, mm (2≤n≤10002≤n≤1000, 1≤m≤n1≤m≤n) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1040≤ai≤104) — weights of all fridges.
Output
For each test case:
- If there is no solution, print a single integer −1−1.
- Otherwise, print a single integer cc — the minimum total cost. The ii-th of the next mm lines contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi), meaning that the ii-th steel chain connects fridges uiui and vivi. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.
Example
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3
Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1
根据题意,每个点至少连两条边,2点时无解,自己画图。 那么就是说N个点N条边连完之后的权值都是一样,我们就考虑形成最大环的连法,对于多出来的边,肯定是连权值最小的边,题目给了说,两点之间可以连任意多的边。完事撒花❀。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct fridge
{
int id;
int val;
}a[20005];
int cmp(fridge a,fridge b)
{
return a.val < b.val;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n,k;
cin>>n>>k;
int mi=0,ans=0;
for(int i = 1;i <= n;i++)
{
cin>>a[i].val;
a[i].id = i;
}
sort(a + 1,a + 1 + n,cmp);
if(k<n||n==2)
{
puts("-1");
continue;
}
for(int i = 1;i <= n;i++)
{
ans+=a[i].val*2;
}
ans+=(k-n)*(a[1].val + a[2].val);
printf("%d\n",ans);
for(int i=1;i<n;i++)
{
cout<<i<<" "<<i+1<<endl;
}
cout<<n<<" "<<1<<endl;
for(int i=1;i<=k-n;i++)
{
printf("%d %d\n",a[1].id,a[2].id);
}
}
return 0;
}
Codeforce 1255 Round #601 (Div. 2)B. Fridge Lockers(思维)的更多相关文章
- Codeforce 1255 Round #601 (Div. 2)D. Feeding Chicken (模拟)
Long is a huge fan of CFC (Codeforces Fried Chicken). But the price of CFC is increasing, so he deci ...
- Codeforce 1255 Round #601 (Div. 2) C. League of Leesins (大模拟)
Bob is an avid fan of the video game "League of Leesins", and today he celebrates as the L ...
- Codeforce 1255 Round #601 (Div. 2) A. Changing Volume (贪心)
Bob watches TV every day. He always sets the volume of his TV to bb. However, today he is angry to f ...
- Codeforces Round #601 (Div. 2) B Fridge Lockers
//题目要求的是每一个点最少要有两条边连接,所以可以先构成一个环.然后再把剩余的最短的边连接起来 #include<iostream> #include<algorithm> ...
- 【cf比赛记录】Codeforces Round #601 (Div. 2)
Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...
- Codeforces Round #601 (Div. 2)
传送门 A. Changing Volume 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/19 22:37:33 */ #include ...
- codeforce Codeforces Round #201 (Div. 2)
cf 上的一道好题: 首先发现能生成所有数字-N 判断奇偶 就行了,但想不出来,如何生成所有数字,解题报告 说是 所有数字的中最大的那个数/所有数字的最小公倍数,好像有道理:纪念纪念: #incl ...
- CodeForce edu round 53 Div 2. D:Berland Fair
D. Berland Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version)
Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version) N个盒子,每个盒子有a[i]块巧克力,每次操作可以将盒子中的 ...
随机推荐
- java中OOM错误解析(面试可以聊的东西)
嗯,生活加油鸭.... 实习中遇到OOM错误 GC overhead limit exceeded 问题,所以整理一下OOM异常问题: 先看一下“阿里的开发手册”对OOM的描述: OOM,全称“Out ...
- YII基础二
YII多表关联查询 ->select("{{%relation_detail}}.name") ->from("{{%user}}") ->l ...
- mysql 单机多实例配置
如果你想在一台机器上进行主从配置实验,本篇可以帮助你实现愿望 [client] #password = your_password port = 3306 socket = /tmp/mysql.so ...
- PyCharm 项目打开窗口设置为当前还是新开一个怎么办?
前言: 我找这个设置找了好久,后来在一篇博文中才找到,现在记录下来一下,顺便带图解释一下 设置步骤: File -> Setting -> Appearance & ...
- Fiddler实战之拟2G、3G、4G网络进行弱网测试
至于fidder网络代理设置就不多说了 模拟网速: 1.启动Fiddler,打开菜单栏Rules---Performances---Simulate Modem Speeds这里打开了模拟调节速度 2 ...
- vueCli 运行报错
error 如下: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! shopping@0.1.0 serve: `vue-cli-service ...
- stand up meeting 1/19/2016
part 组员 工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 准备最后的发布和整个开发的整理总结 6 继续releas ...
- 数据结构之栈—强大的四则复杂运算计算器(超过windows自带的科学计算器)【中缀转后缀表达式】
比windows自带计算器还强的四则复杂运算计算器! 实测随机打出两组复杂算式:-7.5 * 6 / ( -2 + ( -6.5 - -5.22 ) )与7.5+-3*8/(7+2) windows ...
- Numpy学习-(1)
记录我学习Numpy过程 1. 介绍 (1)NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库 ...
- Matlab学习-(2)
1. 文件读取 在编写一个matlab项目时候,通常要导入很多不同格式的数据,下面我们来学习不同的导入函数.(1) 保存工作区MATLAB支持工作区的保存.用户可以将工作区或工作区中的变量以文件的形式 ...