题目链接:传送门

题意:

有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. Linux中修改系统时间

    #date //显示当前日期 #date -s //设置当前时间,只有root权限才能设置,其他只能查看. #date -s 20061010 //设置成20061010,这样会把具体时间设置成空00 ...

  2. androidstudio集成ijkplayer教程

      介绍 ijkplayer是一款非常火的开源视频播放器,android和IOS通用.关于怎么编译怎么导入android Studio中自己的项目,其中坑很多,本篇记录下自己的操作记录.ijkplay ...

  3. 2017-3-11 leetcode 217 219 228

    ji那天好像是周六.....吃完饭意识到貌似今天要有比赛(有题解当然要做啦),跑回寝室发现周日才开始233333 =========================================== ...

  4. npm run dev 出现警告

    WARNING in ./node_modules/_webpack@3.10.0@webpack/buildin/global.js There are multiple modules with ...

  5. How to read a paper efficiently

    How to read a paper efficiently Structure of a Journal a Journal Article Title Keywords Abstract Int ...

  6. NPOI导出功能

    利用NPOI组件将数据中想要的数据导出到excel表格中. demo如下 using System; using System.Collections.Generic; using System.Li ...

  7. SQLServer修改表字段时进行表连接

    update A set A.XXX='XXXX'from TableA  Ainner join TableB B on B.XX=A.XXwhere XXXXX

  8. ES6变量的解构赋值

    变量的解构赋值 1.数组的解构赋值 2.对象的解构赋值 3.字符串的解构赋值 4.数值和布尔值的解构赋值 5.函数参数的解构赋值 6.圆括号问题 7.用途 1.数组的解构赋值 ES6 允许写成下面这样 ...

  9. wpf 错误 执行了 QueryInterface 调用,请求提供 COM 可见的托管类“BoilerMonitoringV1._0.MapControl”的默认 IDispatch 接口。

    在做wpf嵌入地图时,在自定义的WebBrowser 里面使用JavaScript调用外部方法的时报的错误 在原来的WinForm里 我们只要在窗体类设置的头部设置个 [System.Runtime. ...

  10. 第八周读书笔记(人月神话X月亮与六便士)——到底什么才是一个程序员的自我修养?

    写了这么久的读书笔记,涉及到问题大多是一些如何把软件工程做好,如何把自己的职业生涯做好.但总感觉逻辑链上缺了一环,亦即:我们为什么要把软件工程做好,我们成为一名优秀的职业生涯的意义到底在于什么?我觉得 ...