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 very interesting:

  • The graph is connected and contains exactly n vertices and m 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 is interesting!

Input

First line of input contains 2 integers nm  — the required number of vertices and edges.

Output

In the first line output 2 integers spmstw (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 integers uvw (1 ≤ u, v ≤ n, 1 ≤ w ≤ 109) describing the edge connecting u and v and having weight w.

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

题意:给定点数与边数,要求构造一个图,满足图中1到n的最短路与最小生成树的权值均为质数。

思路:不妨先构造出这个图的最短路,构造一条链,使得前n-2条边的权值为1,最后一条边将最短路的权值凑成一个质数。再次向图中添加权值尽可能大的边直到满足m条边,则图的最小生成树也为最短路。

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int prime = 1e5+3;    //任取一个大于1e5的质数
const int maxn = 1e7;
int main()
{
    int n,m;
    while (~scanf("%d %d",&n,&m)){
        printf("%d %d\n",prime,prime);
        printf("1 2 %d\n",prime-n+2);
        int cnt=m-n+1,f=0;
        for (int i=2;i<n;i++){
            printf("%d %d 1\n",i,i+1);
        }

        for (int i=1;i<n-1;i++){
            for (int j=i+2;j<=n;j++){
                if (cnt==0){
                    f=1;
                    break;
                }
                printf("%d %d %d\n",i,j,maxn);
                cnt--;
            }
            if (f)
                break;
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值