Codeforces Round #457 (Div. 2) C. Jamie and Interesting Graph

C. Jamie and Interesting Graph
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jamie has recently found undirected weighted graphs with the following properties veryinteresting:

  • The graph is connected and contains exactly n vertices andm edges.
  • All edge weights are integers and are in range [1, 109] inclusive.
  • The length of shortest path from 1 to n is a prime number.
  • The sum of edges' weights in the minimum spanning tree (MST) of the graph is a prime number.
  • The graph contains no loops or multi-edges.

If you are not familiar with some terms from the statement you can find definitions of them in notes section.

Help Jamie construct any graph with given number of vertices and edges that isinteresting!

Input

First line of input contains 2 integers n,m  — the required number of vertices and edges.

Output

In the first line output 2 integers sp,mstw (1 ≤ sp, mstw ≤ 1014) — the length of the shortest path and the sum of edges' weights in the minimum spanning tree.

In the next m lines output the edges of the graph. In each line output 3 integersu, v,w (1 ≤ u, v ≤ n, 1 ≤ w ≤ 109) describing the edge connectingu and v and having weightw.

Examples
Input
4 4
Output
7 7
1 2 3
2 3 2
3 4 2
2 4 4
Input
5 4
Output
7 13
1 2 2
1 3 4
1 4 3
4 5 4


思路:n-2条边用1,n-1~n的边用一个质数-(n-2)*1替代。 随意取,可以找一个比n-1大的质数x

其实n-2条边的权值可以是任意值,为了方便都取1

剩下的m-(n-1)边用很大的数去替代

最小生成树的值就是质数x,最短路的值也是x

主要是构造的时候找到规律

#include <bits/stdc++.h>
typedef long long ll;

using namespace std;

bool is(int x){
    for(int j=2;j<=x-1;j++){
        if(x%j==0)  return false;
    }
    return true;
}

int p(int x){
    x++;
    for(int i=x;;i++){
        if(is(i))   return i;
    }
}

int main(void){
    int n,m;
    cin>>n>>m;
    m-=n-1;
    //cout <<"m="<<m<<endl;
    int mmin=p(n);
    cout << mmin <<" "<<mmin<< endl;
    for(int i=1;i<=n-2;i++) printf("%d %d %d\n",i,i+1,1);
    printf("%d %d %d\n",n-1,n,mmin-(n-2));
    if(m==0)    return 0;
    for(int i=1;i<=n-1;i++){
      for(int j=i+2;j<=n;j++){
        printf("%d %d %d\n",i,j,mmin+10086);
        m--;
        if(m==0)    return 0;
      }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值