前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforce 1255 Round #601 (Div. 2)B. Fridge Lockers(思维)

Codeforce 1255 Round #601 (Div. 2)B. Fridge Lockers(思维)

作者头像
风骨散人Chiam
发布2020-10-28 11:45:10
4110
发布2020-10-28 11:45:10
举报
文章被收录于专栏:CSDN旧文

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

代码语言:javascript
复制
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3

Output

代码语言:javascript
复制
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1

根据题意,每个点至少连两条边,2点时无解,自己画图。 那么就是说N个点N条边连完之后的权值都是一样,我们就考虑形成最大环的连法,对于多出来的边,肯定是连权值最小的边,题目给了说,两点之间可以连任意多的边。完事撒花❀。

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/11/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档