CodeForces-916C-Jamie and Interesting Graph

链接:

https://vjudge.net/problem/CodeForces-916C

题意:

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!

思路:

构造,由1连其他点,权为2,在选1-2补上一个值,为mst权值和减去其他权为2的边的值.
多余的边补1e9即可.

代码:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5+10;
pair<pair<int, int>, int > pa[MAXN];

//2*(n-1)
bool Solve(int x)
{
    for (int i = 2;i*i <= x;i++)
        if (x%i == 0)
            return true;
    return false;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    if (n == 2)
    {
        cout << "2 2" << endl;
        cout << "1 2 2" << endl;
        return 0;
    }
    for (int i = 1;i <= n-1;i++)
        pa[i].first.first = 1, pa[i].first.second = i+1, pa[i].second = 2;
    int v = 2*(n-2)+1;
    while (Solve(v))
        v++;
    pa[1].second = v-2*(n-2);
    int cnt = n;
    for (int i = 2;i <= n;i++)
    {
        if (cnt > m)
            break;
        for (int j = i+1;j <= n;j++)
        {
            pa[cnt].first.first = i;
            pa[cnt].first.second = j;
            pa[cnt].second = 1e9;
            cnt++;
            if (cnt > m)
                break;
        }
    }
    cout << 2 << ' ' << v << endl;
    for (int i = 1;i <= m;i++)
        cout << pa[i].first.first << ' ' << pa[i].first.second << ' ' << pa[i].second << endl;


    return 0;
}

转载于:https://www.cnblogs.com/YDDDD/p/11392395.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值