当前位置: 首页 > news >正文

wqs二分

f304c95fe525afae2c91ec06eb0973ff

 

最小度限制生成树  QWQ

 

//度限制最小生成树wqs二分
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e12;
int n,m,s,k,tot0,tot1,fa[1000010],cnt,res;
struct edg{int u,v,w;bool isNeed;
}orig[2][1000010],e[1000010];
bool cmp(edg a,edg b){return a.w<b.w;
}
int Find(int x){if(fa[x]==x){return x;}return fa[x]=Find(fa[x]);
}
bool Merge(int u,int v){int fu=Find(u),fv=Find(v);if(fu==fv){return 0;}fa[fu]=fv;return 1;
}
void mergeSort(int delta){for(int i=1;i<=tot1;i++){orig[1][i].w+=delta;}int i=1,j=1,sm=0;while(i<=tot0&&j<=tot1){if(orig[0][i].w<orig[1][j].w){e[++sm]=orig[0][i++];continue;}e[++sm]=orig[1][j++];}while(i<=tot0){e[++sm]=orig[0][i++];}while(j<=tot1){e[++sm]=orig[1][j++];}for(int i=1;i<=tot1;i++){orig[1][i].w-=delta;}
}
bool check(int delta){mergeSort(delta);res=0,cnt=0;for(int i=1;i<=n;i++){fa[i]=i;}int block=n;for(int i=1;i<=m;i++){if(!Merge(e[i].u,e[i].v)){continue;}cnt+=e[i].isNeed;res+=e[i].w;block--;if(block==1){break;}}if(cnt>=k){return 1;}return 0;
}
signed main(){ios::sync_with_stdio(0);cin>>n>>m>>s>>k;for(int i=1;i<=n;i++){fa[i]=i;}for(int i=1;i<=m;i++){int u,v,w;cin>>u>>v>>w;if(u==s||v==s){orig[1][++tot1]={u,v,w,1};continue;}orig[0][++tot0]={u,v,w,0};}if(tot1<k){cout<<"Impossible"<<endl;return 0;}int block=n;for(int i=1;i<=tot0;i++){if(Merge(orig[0][i].u,orig[0][i].v)){block--;}}for(int i=1;i<=tot1;i++){if(Merge(orig[1][i].u,orig[1][i].v)){block--;}}if(block!=1){cout<<"Impossible"<<endl;return 0;}sort(orig[0]+1,orig[0]+tot0+1,cmp);sort(orig[1]+1,orig[1]+tot1+1,cmp);if(!check(-inf)||cnt<k){cout<<"Impossible"<<endl;return 0;}check(inf);if(cnt>k){cout<<"Impossible"<<endl;return 0;}int l=-inf,r=inf,maxp=-inf;while(l<=r){int mid=(l+r)>>1;if(check(mid)){maxp=mid;l=mid+1;continue;}r=mid-1;}check(maxp);cout<<res-maxp*k<<endl;return 0;
}

 

http://www.jsqmd.com/news/310137/

相关文章:

  • 【路径规划】基于快速扩展随机树RRT规划器实现机器人在在网格内找到从指定起始区域到目标区域的路径,同时避开沿途障碍物附matlab代码
  • 【图像增强】水下图像一致性增强评价系统Matlab实现
  • 【免费代码分享】10种卷积神经网络融合BiLSTM的多变量时间序列预测
  • 时序数据库 Apache IoTDB 入选国家重点研发计划高新技术成果产业化试点
  • 2026年回望:Sealos DevBox如何重新定义了云端开发的标准
  • Mac启动Redis并连接
  • 渲染慢到通宵,如何提高渲染速度? 这套技巧3 步搞定!
  • GPU 和 CPU 渲染谁更顶?新手必看的选型指南
  • 如何高效查询海量IP归属地?大数据分析中的IP查询应用
  • Github开源插件!最新豆包AI无水印图批量下载,免费无广告使用,支持高清无损图片下载 (1)
  • 私藏视频不想被看到?1招伪装教你一秒钟伪装
  • 《P2151 [SDOI2009] HH 去散步》
  • 基于Springboot学生交流培养管理平台【附源码+文档】
  • 基于Springboot流浪动物救助平台【附源码+文档】
  • 高驰运动手表二手回收价格怎么定价,上门回收
  • 主循环SYS_TICK无阻塞延迟
  • Java毕设项目:基于springboot的小区公共收益管理系统(源码+文档,讲解、调试运行,定制等)
  • 【毕业设计】基于springboot的小区公共收益管理系统(源码+文档+远程调试,全bao定制等)
  • 计算机等级考试—CMMI CL讲解KTV收银系统—东方仙盟练气期
  • 【课程设计/毕业设计】基于springboot的小区物业费公共收益管理系统【附源码、数据库、万字文档】
  • 高驰二手运动手表回收价格全解析:看懂这几点,闲置变现不亏
  • 勇闯博客世界之我的Java大冒险之善良的小动态sql
  • 成果展示
  • 2026高驰二手运动手表回收价格,支持全国上门回收
  • java: 找不到符号方法 getCode()
  • 【双指针】盛水最多的容器
  • 【双指针】接雨水
  • 告别同质化竞争,先知AI赋予男装品牌独特智慧基因
  • 【数组】分糖果问题
  • 【优先级队列】主持人调度(二)