HDU3371--- Connect the Cities
来源:互联网 发布:如何不被淘宝品控 编辑:程序博客网 时间:2024/05/28 23:22
一道很蛋疼的题,这道题过不过看运气,运气好700ms,运气不好就超时。(还有一次998ms我会乱说- -)
测试很多次int cmp()速度比bool cmp()速度快一点。
不用路径压缩比用了快一点。。。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll long long#define inf 0xfffff#define MAX 25500int p[510],n,m,s,kk,flag;struct e{ int a,b,c;};e path[MAX];bool cmp(const e &a, const e &b){return a.c<b.c;}int find(int x){ while(p[x]!=x) x=p[x]; return x;}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d%d",&n,&m,&kk); for(int i=1;i<=n;i++) p[i]=i; int aa,bb,cc; for(int i=1;i<=m;i++) scanf("%d%d%d",&path[i].a,&path[i].b,&path[i].c); for(int i=0;i<kk;i++) { int op,st,te; scanf("%d",&op); scanf("%d",&st); for(int j=1;j<op;j++) { scanf("%d",&te); p[find(te)]=p[find(st)]; st=te; } } int sum=0;flag=0; for(int i=1;i<=n;i++) {if(p[i]==i) flag++; //通过判断是几个祖先 } sort(path+1,path+m+1,cmp); for(int i=1;i<=m&&flag>1;i++) //如果c==1则已经生成 { int dx=find(path[i].a); int dy=find(path[i].b); if(dx!=dy) { sum+=path[i].c; p[dx]=dy; flag--; } } if(flag==1) printf("%d\n",sum); else printf("-1\n"); }}
0 0
- hdu3371 Connect the Cities
- HDU3371--Connect the Cities
- hdu3371 Connect the Cities
- HDU3371 Connect the Cities
- hdu3371: Connect the Cities
- HDU3371--- Connect the Cities
- HDU3371-Connect the Cities
- hdu3371 Connect the Cities
- HDU3371 Connect the Cities【Kruskal】
- hdu3371 Connect the Cities (MST)
- hdu3371 Connect the Cities--kruskal
- hdu3371——Connect the Cities
- hdu3371 Connect the Cities (最小生成树)
- HDU3371 Connect the Cities 解题报告--prim
- HDU3371——Connect the Cities
- HDU3371(Connect the Cities)最小生成树
- HDU3371 Connect the Cities 【最小生成树Kruskal】
- HDU3371 Connect the Cities 题解 【图论】【最小生成树】
- jQuery EasyUI 将单元格改为链接 可点击
- 二级指针的问题汇总
- ios note(二)
- mysql 用户管理和权限设置
- MFC - Tab控件操作
- HDU3371--- Connect the Cities
- 《编码的奥秘》读后感
- 函数指针 编译问题
- 黑马程序员——Java基础之集合框架
- MYSQL配置和管理
- 攻防:文件上传漏洞的攻击与防御
- HDU1718--Rank
- HDU1181---变形课
- Java8流特性和Lambda表达式