同样的代码 每次交的结果都不一样

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct
Node
{

int
from;
int
to;
int
w;
}
Edges[]; int father[],ans; int find(int x)
{

int
root = x;
while
(root != father[root])
{

root = father[root];
}
int z;
while
(x != root)
{

z = father[x];
father[x] = root;
x = z;
}

return
root;
}
bool cmp(Node a,Node b)
{

return
a.w < b.w;
}
//int cmp(const void * a, const void * b)
//{
// return ((Node *)a)->w - ((Node *)b)->w;
//}

bool
Kruskal(int N,int M,int k)
{

sort(Edges,Edges+M,cmp);
// qsort(Edges,M,sizeof(Node),cmp);
int x,y;
for
(int i =; i < M; i++)
{

x = find(Edges[i].from);
y = find(Edges[i].to);
if
(x != y)
{

father[y] = x;
k++;
ans += Edges[i].w;
if
(k == N-)
return
true;
}
}

if
(k == N-)
return
true;
else
return
false;
}

int
main()
{

int
T,k,t,a,b,N,M,K;
scanf("%d",&T);
while
(T--)
{

scanf("%d%d%d",&N,&M,&K);
for
(int i =; i <= N; i++)
father[i] = i;
for
(int i =; i < M; i++)
{

scanf("%d%d%d",&Edges[i].from,&Edges[i].to,&Edges[i].w);
}

k =,ans =;
for
(int i =; i < K; i++)
{

scanf("%d%d",&t,&a);
a = find(a);
t--;
while
(t--)
{

scanf("%d",&b);
b = find(b);
if
(a != b)
{

father[b] = a;
k++;
}
}
}

if
(Kruskal(N,M,k))
printf("%d\n",ans);
else

printf("-1\n");
}
return;
}

hdu 3371 有毒的卡时间题目的更多相关文章

  1. hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)

    HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包 ...

  2. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  3. HDUOJ--2079选课时间(题目已修改,注意读题)

    选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 2079 选课时间(题目已修改,注意读题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2079 背包 #include <cstdio> #include <cstring> # ...

  5. hdu 2079 选课时间(题目已改动,注意读题) (母函数)

    代码: #include<cstdio> #include<cstring> using namespace std; int main() { int t; scanf(&q ...

  6. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  7. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  8. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  9. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

随机推荐

  1. arcgis根据表字段进行数据合并

    第一步 1.地理处理-----2.数据管理工具----3.制图综合----4.融合 第二步 打开融合面板,选择输入要素,要融合的字段,选择统计字段数量,完成融合.

  2. Embedded based learning

    简单整理了一些嵌入式底层需要接触的相关概念.   # CPU  CU. Control Unit. send need-clac-data -> ALU clac -> get resul ...

  3. css3实现jQuery的slideUp和slideDown效果

    最近打算做一些交互优化方面的轮子.虽然轮子别人都弄过,但是自己没弄过.重复造轮子对知识理解还是有好处的.本次轮子如题目.直接代码. <!DOCTYPE html> <html lan ...

  4. Linux Mint设置自定义快捷键

    我使用的是 Linux Mint 19.2 Tina 先搜索键盘,把键盘的功能调出来 快捷键--->>自定义快捷键--->>添加自定义快捷键 名称可自定义(这里我定义的是“截图 ...

  5. delphi 中如何从数据库中读取数据自生成TreeView,只有两个字段,数据库结构如下。急急!!

    我的数据库结构如下:UnitId      UnitName01          中国 (根节点)0101        河北省(二级树)010101      河北省沧州市(三级树)0101010 ...

  6. mysql删除一条记录

    mysql如何删除一条记录 delete from 表名 where 条件 实例: use db1 delete from tb1 where id = 1;

  7. Spring Aop(六)——@DeclareParents介绍

    转发:https://www.iteye.com/blog/elim-2395410 6 @DeclareParents介绍 @DeclareParents注解也是Aspectj提供的,在使用基于As ...

  8. linux双机热备份

    使用HeartBeat实现高可用HA的配置过程详解 一.写在前面 HA即(high available)高可用,又被叫做双机热备,用于关键性业务.简单理解就是,有2台机器 A 和 B,正常是 A 提供 ...

  9. Flutter 实现简单搜索功能

    先建立一个主文件,继承StatelessWidget,然后在home属性中加入SearchBarDemo,这是一个自定义的Widget,主要代码都在这个文件中. import 'package:flu ...

  10. MacOSX的JDK版本快捷切换

    修改.bash_profile # JDK8 export JAVA_8_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Conten ...