题目链接:传送门

题意:

有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. luogu1315 观光公交

    题目大意 一个公交车在一条线上从1站.2站走到n站,站站间有距离.另有m个乘客在时间点t在a站等候要到b站.对于一个站,如果车比人早到,则车等人,如果人比车早到,则人等车(我一开始做题时把这个情况当作 ...

  2. c18---数组和指针

    // // main.c // day09 #include <stdio.h> int main(int argc, const char * argv[]) { ; int *numP ...

  3. eclipse中的.project 和 .classpath文件的具体作用

    .project是项目文件,项目的结构都在其中定义,比如lib的位置,src的位置,classes的位置 .classpath的位置定义了你这个项目在编译时所使用的$CLASSPATH 这些文件你用文 ...

  4. Android开机logo修改方法 【转】

    本文转载自:http://blog.csdn.net/qq258711519/article/details/7766303 一体机平台开机logo修改方法 1:修改Kernel中的Logo: 若是要 ...

  5. Session版购物车+MVC局部刷新

     效果图: 大致代码: <script type="text/javascript"> $(function () { LoadOrderDetailList(); } ...

  6. C#解除某类警告。。。。。。。。。。

    C#预处理器指令取消不必要的警告 今天将自己写的一个类库生成一个DLL后,想把注释也加进去.... 方法:在属性->生成选项卡->XML文档文件(勾选)(生成的文件名不能修改,使用时必须跟 ...

  7. json属性(Jackson)

    Jackson相关:使用Jackson相关的注解时一定要注意自己定义的属性命名是否规范. 命名不规范时会失去效果.(例如Ename ,Eage 为不规范命名.“nameE”,“ageE”为规范命名). ...

  8. LCA 离线的Tarjan算法 poj1330 hdu2586

    LCA问题有好几种做法,用到(tarjan)图拉算法的就有3种.具体可以看邝斌的博客.http://www.cnblogs.com/kuangbin/category/415390.html 几天的学 ...

  9. 解决启动httpd报: apr_sockaddr_info_get() failed for错误

    我测试库里 service httpd start 时报 下面错误 httpd: apr_sockaddr_info_get() failed for fengxin.wzjzt.centoshttp ...

  10. sqlserver 时间测试

    select * from GropBy where [date] BETWEEN '2010-10' and '2015-10' --从字符串转换日期和/或时间时,转换失败. select * fr ...