【题解】CF916C

CF916C 题解

思路

思考发现,如果我们让很多边的边权变得非常大,而故意留下 \(1\)\(n\) 的某一条路径,使整条路径之和甚至还没有剩下一条边的权值大,这条路径显然就是最短路了。

更重要的是,这样构造的结果,这条路径同时还是整张图
的最小生成树。

这样我们只需要找一个 \(100000\) 以上的质数,使得找出的路径的长度等于这个质数就可以了。

AC Code

#include<bits/stdc++.h>
using namespace std;
int n,m,p=1e5+3;
int main() {
	cin>>n>>m;
	cout<<p<<" "<<p<<endl<<1<<" "<<2<<" "<<p-n+2<<endl;
	for (int i=2; i<n; i++) cout<<i<<" "<<i+1<<" "<<1<<endl;//然后分别连接i和i+1,边权均为1。 
	m-=n-1;
	for (int k=0,j=n; m--;){
		if (++j>n) j=++k+2;
		cout<<k<<" "<<j<<" 9999999"<<endl;
	}
	return 0;
}
posted @ 2024-04-06 18:01  Kcjhfqr  阅读(47)  评论(0)    收藏  举报
.poem-wrap { position: relative; width: 1000px; max-width: 80%; border: 2px solid #797979; border-top: none; text-align: center; margin: 40px auto; } .poem-left { left: 0; } .poem-right { right: 0; } .poem-border { position: absolute; height: 2px; width: 27%; background-color: #797979; } .poem-wrap p { width: 70%; margin: auto; line-height: 30px; color: #797979; } .poem-wrap h1 { position: relative; margin-top: -20px; display: inline-block; letter-spacing: 4px; color: #797979; font-size: 2em; margin-bottom: 20px; } #poem_sentence { font-size: 25px; } #poem_info { font-size: 15px; margin: 15px auto; }