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

#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. pwn学习日记Day6 基础知识积累

    知识杂项 ELF:在计算机科学中,是一种用于二进制文件.可执行文件.目标代码.共享库和核心转储格式文件. char fgets(char buf, int bufsize, FILE stream); ...

  2. shell命令:getevent和sendevent

    1.getevent 输出所有event设备的基本信息 输出格式: 设备名: 事件type 事件code 事件value 注意:这里的数字都是16进制. getevent -c 10 //输出10条信 ...

  3. Maven 3.6.1 下载及安装配置

    Step1:Download You can download Maven3.6.1 from maven.apche.org , or from here. Step2:Unzip and add ...

  4. 性能调优 | 如何通过性能调优突破 MySQL 数据库性能瓶颈?

    本文出自头条号老王谈运维,转载请说明出处. MySQL 数据库瓶颈对 DBA 程序员而言,是非常棘手的问题.要正确的优化SQL,我们需要快速定位能性的瓶颈点,也就是说快速找到我们SQL主要的开销在哪里 ...

  5. linux中如何配置vim的别名为vi?

    答: 向~/.bashrc中添加如下内容: alias vi=vim

  6. Linux设备驱动程序学习----目录

    目录 设备驱动程序简介 1.设备驱动程序简介 构造和运行模块 2.内核模块和应用程序的对比 3.模块编译和装载 4.模块的内核符号表  5.模块初始化和关闭  6.模块参数  7.用户空间编写驱动程序 ...

  7. Flask模拟实现CSRF攻击的方法

    https://www.jb51.net/article/144371.htm https://www.cnblogs.com/888888CN/p/9489345.html http://xiaor ...

  8. win2008 r2下配置IIS7(ASP.net运行环境)

    一.常规配置: 1.先要设置应用程序池(ApplicationPool)为Classic.NETAppPool. 2.选中左侧的“应用程序池”,选中“Classic.NETAppPool”,单击右侧的 ...

  9. [activemq]+spring的使用

    一.生产端(Producer) applicationContext.xml中的配置: <?xml version="1.0" encoding="UTF-8&qu ...

  10. stm32第一章cortex-M3处理器概述

    处理器特点 哈弗结构3级流水线内核 实现Thumb-2指令集,告别切换32位的arm指令和16位的Thumb指令,优化性能和代码密度 结合可配置的嵌套向量中段控制器Nvic,提供非屏蔽中断NMI和32 ...