Codeforces Round #601 (Div. 2) B题

Fridge Lockers

Hanh lives in a shared apartment. There are n people (including Hanh) living there, each has a private fridge.

n 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 n people can open it.

For exampe, in the picture there are n=4 people and 5 chains. The first person knows passcodes of two chains: 1−4 and 1−2. The fridge 1 can be open by its owner (the person 1), also two people 2 and 4 (acting together) can open it.
The weights of these fridges are a1,a2,…,an. To make a steel chain connecting fridges u and v, you have to pay au+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 m steel chains so that all fridges are private. A fridge is private if and only if, among n 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 i is not private if there exists the person j (i≠j) that the person j can open the fridge i.

For example, in the picture all the fridges are private. On the other hand, if there are n=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 m 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 T (1≤T≤10). Then the descriptions of the test cases follow.

The first line of each test case contains two integers n, m (2≤n≤1000, 1≤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 n integers a1,a2,…,an (0≤ai≤104) — weights of all fridges.

Output
For each test case:

If there is no solution, print a single integer −1.
Otherwise, print a single integer c — the minimum total cost. The i-th of the next m lines contains two integers ui and vi (1≤ui,vi≤n, ui≠vi), meaning that the i-th steel chain connects fridges ui and vi. An arbitrary number of chains can be between a pair of fridges.
If there are multiple answers, print any.


一道思维题;

这道题没看清楚题目,m<=n;我还去判断了m>n时存在多余边的情况,但是还有个更致命的点是没判断n<=2时无论如何都是不行的;惨痛的cf

如果m<=n的话那么只有每个点都连起来,边数就是n条,形成一个大环就行;没有其他连法,总权值就是每个点的权值*2;

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=200100;
const LL mod=998244353;
int n,m;
int main(){
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		int a;
		int sum=0;
		for(int i=1;i<=n;i++){
			cin>>a;
			sum+=a;
		} 
		if(n>m||n<=2){
			cout<<-1<<endl;
			continue;
		}
		cout<<sum*2<<endl;
		for(int i=1;i<=n-1;i++) cout<<i<<" "<<i+1<<endl;
		cout<<n<<" "<<1<<endl;
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值