CSU 1541 There is No Alternative (最小生成树+枚举)
来源:互联网 发布:ros通过ip绑定mac地址 编辑:程序博客网 时间:2024/06/16 05:47
题目链接:传送门
题意:
有n个点,m条边,要使n个点全部连起来且要花费最小,问有哪些边是必须要连的。
分析:
要使花费最小肯定是做最小生成树,但是题目要求哪些边是必须要用的。我们可以
这样思考,我们先求一次最小生成树,然后把这些用到的边统计起来,然后依次枚
举这n-1条边,使他们不能用,然后继续做最小生成树,如果最后求的值和第一次
不一样的话那么这条边是肯定要连的。
代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 50010;struct nod{ int x,y,val; bool operator < (const struct nod &tmp) const{ return this->val<tmp.val; }}edge[maxn];int par[maxn/100],num[maxn/100];int id[maxn/100];int n,m;void init(){ for(int i=0;i<=n;i++) par[i]=i,num[i]=1;}int find_par(int x){ if(x!=par[x]) return par[x]=find_par(par[x]); return par[x];}bool Union(int x,int y){ x=find_par(x); y=find_par(y); if(x!=y){ par[y]=x; num[x]+=num[y]; return true; } return false;}int ans ,cnt,sum1,sum2;void solve(){ sum1=sum2=0; for(int i=0;i<cnt;i++){ int tmp=0;init(); for(int j=0;j<m;j++){ if(j!=id[i]){ if(Union(edge[j].x,edge[j].y)) tmp+=edge[j].val; } } if(tmp!=ans) sum1++,sum2+=edge[id[i]].val; } printf("%d %d\n",sum1,sum2);}int main(){ while(~scanf("%d%d",&n,&m)){ init(); for(int i=0;i<m;i++){ scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].val); } sort(edge,edge+m); ans=0,cnt=0; memset(id,0,sizeof(id)); for(int i=0;i<m;i++){ if(Union(edge[i].x,edge[i].y)){ ans+=edge[i].val; id[cnt++]=i; } } solve(); } return 0;}/***4 41 2 31 3 52 3 32 4 34 41 2 31 3 12 3 32 4 33 31 2 12 3 11 3 1***/
0 0
- CSU 1541 There is No Alternative(最小生成树+枚举)
- CSU 1541 There is No Alternative (最小生成树+枚举)
- CSU 1541 There is No Alternative【最小生成树+思维】
- csu 1541: There is No Alternative(最小生成树 Kruskal)
- CSU--1541-- There is No Alternative
- CSU 1541- There is No Alternative(克鲁斯卡尔)
- Tokyo2014 There is No Alternative (最小生成树,灵活题)
- Gym 100803F There is No Alternative(最小生成树)
- 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest F題:There is No Alternative [最小生成树]
- COJ--1541: There is No Alternative
- UVALive 6837There is No Alternative (MST + LCA)
- uvalive6837 There is No Alternative(暴力+kruskal)
- UVALive 6837 There is No Alternative (MST+暴力LCA)
- csu 1116 Kingdoms 最小生成树 二进制枚举
- HDOJ 2435 - There is a war 枚举+最小割
- There is a war (hdu 2435 最小割+枚举)
- CSU 1541 判断最小生成树是否唯一
- hdu 2489(枚举 + 最小生成树)
- Android自定义答题进度条
- 大浪淘金——下半年经济与资本市场展望(姜超6月25日于海通深圳策略会)
- Image图片处理总结
- [原]深刻理解activity启动模式,彻底理解android的四种启动模式,尤其singleTask
- HTML 标签 <html>
- CSU 1541 There is No Alternative (最小生成树+枚举)
- 关于开发板的nfs挂载
- 提高php代码质量 36计
- 送给股市大盘前亏损的朋友,你亏钱的原因是什么?
- 使用类似linux系统滴答时钟解决定时器不足的问题
- UIView及其子类围绕任意点旋转的方法
- 什么是敏捷软件测试
- CocoaPods 版本管理冲突 解决
- 关于json操作