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

#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. BufferedWriter中write与close函数使用

    BufferedWriter 是一个缓冲字符输出流,可以将要输出的内容先缓冲到一个字符数组中,等字符数组满了才一次性写到输出流内,默认的字符数组长度为8192.使用BufferedWriter 时需要 ...

  2. ArcGIS超级工具SPTOOLS-拓扑错误处理

    1.1  删除线面直线上的点 操作视频: https://weibo.com/tv/v/Hxjgmuv6F?fid=1034:4379388532225679 删除面要素.线要素一条边直线上的点. 1 ...

  3. Python全国二级等级考试(2019)

    一.前言 2018年9月随着全国计算机等级考试科目中加入“二级Python”,也确立了Python在国内的地位,猪哥相信Python语言势必会像PS那般普及.不久的将来,谁会Python谁就能获得女神 ...

  4. 跨平台(win和unix)的线程封装类

    #ifdef WIN32 #include <Windows.h> #include <process.h> #else #include <pthread.h> ...

  5. [MyBatis]浅谈如何实现事务处理

    要实现事务处理,就得从SqlSession中取出connection来,然后对connection采用setAutoCommit,commit,rollback等操作,最后的时候,不能像JDBC一样关 ...

  6. vue问题五:element ui组件的开始时间-结束时间验证

    <el-date-picker v-model="seach.before" type="date" placeholder="开始时间&quo ...

  7. 003-log-jul,jdk自带日志服务

    一.简介 java官方日志jul,位于java.util.logging包下. 1.1.POM依赖 无依赖 1.2.配置 JUL的默认配置文件是logging.properties ,在 $JAVA_ ...

  8. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_17-CMS前端工程创建-单页面应用介绍

    查看运行起来的页面的源代码 这个webpck打包生成的文件. 单页面应用的优缺点: 优点: 1.用户操作体验好,用户不用刷新页面,整个交互过程都是通过Ajax来操作. 2.适合前后端分离开发,服务端提 ...

  9. 1-3 RHEL7操作系统的安装

    RHEL7操作系统的安装 本节所讲内容: q  RHEL7.2操作系统的安装 第1章 RHEL7系统安装 1.1 安装软件准备: 需要的软件如下: Vmware workstation 12(含注册码 ...

  10. HDU 1087 最大递增子序列

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...