CSU 1541 There is No Alternative (最小生成树+枚举)
题目链接:传送门
题意:
有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 4
1 2 3
1 3 5
2 3 3
2 4 3
4 4
1 2 3
1 3 1
2 3 3
2 4 3
3 3
1 2 1
2 3 1
1 3 1
***/
CSU 1541 There is No Alternative (最小生成树+枚举)的更多相关文章
- There is No Alternative~最小生成树变形
Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...
- BNUOJ-26586 Simon the Spider 最小生成树+枚举
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26586 题意:给一个图,每条边有一个权值.要你求选择一棵树,权值和为sum,然后在树上选择 ...
- HDU 1589 Find The Most Comfortable Road 最小生成树+枚举
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU4081 Qin Shi Huang's National Road System【prim最小生成树+枚举】
先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.因为结果是A/B.A的变化会引起B的变化,两个制约.无法直接贪心出最大的A/B. ...
- csu1116 Kingdoms 最小生成树-枚举状态
题目链接: csu 1116 题意: 有一幅双向图连接N个城市(标号1~n,1表示首都) 每一个城市有一个价值W. 地震摧毁了全部道路,现给出可修复的m条道路并给出修复每条道路所需的费用 问在总费用 ...
- CSUOJ 1541 There is No Alternative
There is No Alternative Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Aiz ...
- CSU 1337 搞笑版费马大定理【优化枚举】
费马大定理:当n>2时,不定方程an+bn=cn没有正整数解.比如a3+b3=c3没有正整数解.为了活跃气氛,我们不妨来个搞笑版:把方程改成a3+b3=c3,这样就有解了,比如a=4, b=9, ...
- 2017北京国庆刷题Day6 afternoon
期望得分:100+100+40=240 实际得分:100+0+40=140 二进制拆分.二进制前缀和 #include<cstdio> #include<iostream> u ...
- Codeforces Round #535(div 3) 简要题解
Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...
随机推荐
- 【BZOJ 2351】 Matrix
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2351 [算法] 哈希 [代码] #include<bits/stdc++.h& ...
- 使用ssh和putty操控远程的linux server
windows下没有openssh,今天这里使用openssh-server作为server,windows下使用putty作为client, putty主要流程分以下几步: step 1: 下载pu ...
- Super超级ERP系统---(6)采购管理--入库上架
采购商品入库完成后,下一步就是上架操作.所谓上架就是把入库放到移动托盘的商品转移到固定货架上,货架上有货位号,可以把商品放到指定的货位上.主要分两步操作,上架操作主要是移动PDA上完成的 1.扫描移 ...
- jquery选择器(可见对象,不可见对象) +判断,对象(逆序)
//可见对象: $("li:visible ") //可见对象下的 隐藏对象 $("li:visible [type='hidden']") //获得 可见 的 ...
- Nginx介绍及知识点(摘抄)
正向代理是把自己的网络环境切换成代理的网络 反向代理是代理机器返回给我要我的资源 本文借鉴参考于http://tengine.taobao.org/book/chapter_02.html. 属于纯干 ...
- 第7章 性能和可靠性模式 Load-Balanced Cluster(负载平衡群集)
上下文 您已经决定在设计或修改基础结构层时使用群集,以便在能够适应不断变化的要求的同时保持良好的性能. 问题 在保持可接受的性能级别的同时,如何设计一个可适应负载变化的.可伸缩的基础结构层? 影响因素 ...
- TensorFlow-LSTM序列预测
问题情境:已知某一天内到目前为止股票各个时刻的价格,预测接下来短时间内的价格变化. import tushare as ts import time from collections import n ...
- 【Oracle】创建用户
任务: 1)创建用户siebel,密码oracle 2)授予sse_role,tblo_role角色 3)siebel用户没有对system,sysaux的使用权限 4)默认表空间ts_users,无 ...
- Spring 获取propertise文件中的值
Spring 获取propertise文件中的值 Spring 获取propertise的方式,除了之前的博文提到的使用@value的注解注入之外,还可以通过编码的方式获取,这里主要说的是要使用Emb ...
- APICloud开发小技巧(一)
apicloud开发文档中,前端开发框架指的就是,类似jq\js的语法: https://docs.apicloud.com/Front-end-Framework/framework-dev-gui ...