[BZOJ]3624: [Apio2008]免费道路 最小生成树
来源:互联网 发布:jmeter 安装mac版 编辑:程序博客网 时间:2024/06/12 22:01
Description
题解:
乱搞,首先求出哪些鹅卵石路是必须要选的,方法是先用水泥路连,用完水泥路之后用的鹅卵石路就是必须要选的。然后若不足k条,则随便选几条鹅卵石路,最后随便选几条水泥路就好了。还要判断一下几个无解的情况。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int Maxn=20010;const int Maxm=100010;struct Edge{int x,y,c;}e[Maxm],e1[Maxm],Ans[Maxn];bool cmp(Edge a,Edge b){return a.c>b.c;}int n,m,k,f[Maxn],l=0,num=0,la=0;int Find(int x){return f[x]==x?x:f[x]=Find(f[x]);}bool mark[Maxm];int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].c); for(int i=1;i<=n;i++)f[i]=i; sort(e+1,e+1+m,cmp); for(int i=1;i<=m;i++) { int fx=Find(e[i].x),fy=Find(e[i].y); if(fx!=fy) { f[fx]=fy; if(!e[i].c)e1[++l]=e[i],e1[l].c=i; num++; } } if(l>k||num!=n-1){puts("no solution");return 0;} for(int i=1;i<=n;i++)f[i]=i; for(int i=1;i<=l;i++)//必须用的边 { mark[e1[i].c]=true;Ans[++la]=e[e1[i].c]; int fx=Find(e1[i].x),fy=Find(e1[i].y); if(fx!=fy)f[fx]=fy; } k-=l; for(int i=m;k;i--)//随便几条鹅卵石路 if(!mark[i]) { int fx=Find(e[i].x),fy=Find(e[i].y); if(fx!=fy)f[fx]=fy,k--,Ans[++la]=e[i]; } for(int i=1;i<=m;i++)//水泥路 { int fx=Find(e[i].x),fy=Find(e[i].y); if(fx!=fy)f[fx]=fy,Ans[++la]=e[i]; } for(int i=1;i<=la;i++) printf("%d %d %d\n",Ans[i].x,Ans[i].y,Ans[i].c);}
阅读全文
2 0
- [BZOJ 3624][Apio2008]免费道路:最小生成树
- [BZOJ]3624: [Apio2008]免费道路 最小生成树
- 【bzoj 3624】: [Apio2008]免费道路
- bzoj 3624: [Apio2008]免费道路
- 【BZOJ】【P3624】【Apio2008】【免费道路】【题解】【生成树】
- bzoj 3624: [Apio2008]免费道路(并查集+生成树+乱搞)
- BZOJ 3264 [Apio2008]免费道路
- BZOJ P3624[Apio2008]免费道路
- 3624: [Apio2008]免费道路
- 3624: [Apio2008]免费道路
- APIO2008免费道路 Bsoj2714 并查集+两次最小生成树
- BZOJ3624 [Apio2008]免费道路
- bzoj3624 [Apio2008]免费道路
- 【OI做题记录】【BZOJ】【Apio2008】免费道路
- bzoj 3624 免费道路
- 【并查集】【生成树】【APIO 2008】【bzoj 3624】免费道路
- bzoj 3206: [Apio2013]道路费用 最小生成树
- [BZOJ3624][Apio2008]免费道路(贪心+并查集)
- HTTP简介和总结
- Django rest framework API Guide的 Requests 翻译
- codevs 2147 数星星 离散化?解题报告
- 深度探索c++对象模型(7)
- Postman 根据环境变量进行接口整理
- [BZOJ]3624: [Apio2008]免费道路 最小生成树
- 每日一练 心态整理(二)
- 什么情况下析构函数需要定义为虚函数?
- GDB调试基本使用方法
- MySQL之连接查询
- HDU2481:Toy(Burnside)
- Codeforces 864E Fire (DP)
- Codeforces Round #436 (Div. 2)
- 两个栈实现队列