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