题目链接:传送门

题意:

有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 (最小生成树+枚举)的更多相关文章

  1. There is No Alternative~最小生成树变形

    Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens reque ...

  2. BNUOJ-26586 Simon the Spider 最小生成树+枚举

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26586 题意:给一个图,每条边有一个权值.要你求选择一棵树,权值和为sum,然后在树上选择 ...

  3. HDU 1589 Find The Most Comfortable Road 最小生成树+枚举

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU4081 Qin Shi Huang&#39;s National Road System【prim最小生成树+枚举】

    先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.因为结果是A/B.A的变化会引起B的变化,两个制约.无法直接贪心出最大的A/B. ...

  5. csu1116 Kingdoms 最小生成树-枚举状态

    题目链接: csu 1116 题意: 有一幅双向图连接N个城市(标号1~n,1表示首都)  每一个城市有一个价值W. 地震摧毁了全部道路,现给出可修复的m条道路并给出修复每条道路所需的费用 问在总费用 ...

  6. CSUOJ 1541 There is No Alternative

    There is No Alternative Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Aiz ...

  7. CSU 1337 搞笑版费马大定理【优化枚举】

    费马大定理:当n>2时,不定方程an+bn=cn没有正整数解.比如a3+b3=c3没有正整数解.为了活跃气氛,我们不妨来个搞笑版:把方程改成a3+b3=c3,这样就有解了,比如a=4, b=9, ...

  8. 2017北京国庆刷题Day6 afternoon

    期望得分:100+100+40=240 实际得分:100+0+40=140 二进制拆分.二进制前缀和 #include<cstdio> #include<iostream> u ...

  9. Codeforces Round #535(div 3) 简要题解

    Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...

随机推荐

  1. web请求乱码问题总结

    问题1:springmvc通过@ResponseBody向页面返回值(包括汉字)时,乱码 解决方案: springmvc3.2之后可以声明注解驱动器(不知道是不是这么翻译)的时候的控制编码的转换,结果 ...

  2. oc4--方法

    // main.m // 第一个OC类-方法2 #import <Foundation/Foundation.h> // 1.编写类的声明 @interface Iphone : NSOb ...

  3. Php learn note

    Php learn note 1. Between two part of ECHO, there is , sign rather than + sign. echo 'Hello World!!' ...

  4. Java-Maven:POM百科

    ylbtech-Java-Maven:POM百科 Maven是以项目为中心的设计.POM(project object model)是Maven对一个单一项目的描述.没有POM的话,Maven是毫无用 ...

  5. 院校-国外-美国:斯坦福大学( Stanford)

    ylbtech-院校-国外-美国:斯坦福大学( Stanford) 斯坦福大学(Stanford University),全名小利兰·斯坦福大学(Leland Stanford Junior Univ ...

  6. NFine平台去掉平台介绍页面出现的一系列问题

    1.去掉平台介绍页面要做的操作:数值Index页面的下面两处内容 2.注释掉以上两处内容后出现的问题:•当打开多个页面后,最先打开的第一个页面(如下图1的位置)点击×号不能关闭页面:•当打开多个页面后 ...

  7. [hihocoder][Offer收割]编程练习赛46

    AEIOU 选出的子串中由AEI构成的子串和由OU构成的子串之间并没有什么关系,分别算出最长的加起来. #pragma comment(linker, "/STACK:102400000,1 ...

  8. Thread pool引起的程序连接数据库响应慢

    数据库版本:percona-mysql 5.6.16 ​在很长一段时间,都会出现程序连接数据库,出现响应慢的情况,正常在几到几十毫秒之间,但是偶尔会出现上百毫秒的情况: 开始由于开发重新设置并调整过程 ...

  9. shell学习第二弹-进阶

    1.linux系统中配置文件执行顺序 1)用户登录系统,首页调用/etc/profile文件初始化所有bash用户的默认特征 2)然后shell依次查找~/.bash_profile,~/.bash_ ...

  10. 性能问题解决案例01——sybase数据库内存问题

    最近湖南现场反馈问题,所有电子签章页面打不开文书(pdf格式),后台日志没报任何错误. 1.首先想到是签章的ocx控件问题,检查ocx控件安装,发现其他电脑也打不开文书,测试页面可以直接打开pdf文档 ...