题目传送门
二分
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
int a,b;
int lt=- 2 e 9 - 1 ,rt= 2 e 9 + 1 ;
signed main (){
cin>> a>> b;
while (lt+ 1 < rt){
int mid= (lt+ rt)>> 1 ;
if (mid<= a+ b){
lt= mid;
}else {
rt= mid;
}
}cout<< lt;
return 0 ;
}
最小生成树
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
int fa[5 ];
struct kkk {
int x,y,w;
}a[5 ];
int cnt,sum;
bool cmp (kkk x ,kkk y ){
return x.w< y.w;
}
int findd (int x ){
if (x== fa[x]){
return x;
}return fa[x]= findd (fa[x]);
}
void unionn (int x ,int y ){
x= findd (x);
y= findd (y);
if (x!= y){
fa[x]= y;
}return ;
}
signed main (){
ios ::sync_with_stdio (0 );
cin.tie (0 );cout.tie (0 );
for (int i= 1 ;i<= 3 ;i++ ){
fa[i]= i;
}for (int i= 1 ;i<= 2 ;i++ ){
a[i].x= i;
a[i].y= i+ 1 ;
cin>> a[i].w;
}sort (a+ 1 ,a+ 3 ,cmp);
for (int i= 1 ;i<= 2 ;i++ ){
if (cnt== 2 ){
break ;
}if (findd (a[i].x)!= findd (a[i].y)){
unionn (a[i].x,a[i].y);
sum+= a[i].w;
cnt++ ;
}
}cout<< sum;
return 0 ;
}
Floyd
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define int long long
int a[5 ][5 ];
int x;
void floyd (){
for (int k= 1 ;k<= 3 ;k++ ){
for (int i= 1 ;i<= 3 ;i++ ){
for (int j= 1 ;j<= 3 ;j++ ){
if (a[i][k]+ a[k][j]< a[i][j]){
a[i][j]= a[i][k]+ a[k][j];
}
}
}
}
}
signed main (){
for (int i= 1 ;i<= 3 ;i++ ){
for (int j= 1 ;j<= 3 ;j++ ){
a[i][j]= 2 e 9 + 1 ;
}
}for (int i= 1 ;i<= 2 ;i++ ){
cin>> x;
a[i][i+ 1 ]= x;
}for (int i= 1 ;i<= 3 ;i++ ){
a[i][i]= 0 ;
}floyd ();
cout<< a[1 ][3 ];
return 0 ;
}