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

#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. 安装RabbitMQ管理插件失败

    运行 rabbitmq-plugins.bat enable rabbitmq_management后提示失败信息  是因为erlang和RabbitMQ版本冲突导致

  2. kotlin嵌套类

    就是类中定义类 package loaderman.demo class Outer { var name: String = "name" inner class inner { ...

  3. Visual Studio2013的C语言编译器对C99标准的支持情况

    Visual Studio2013终于开始比较良好地支持C99特性了.在此之前,如果用C语言写代码的话,变量名都需要放到函数体的前面部分,代码写起来十分别扭. 而Visual Studio2013中的 ...

  4. 002-jdk-数据结构-工具类Collections、Arrays、System.arraycopy

    常用备注 一.LIst to Array List<String> list = new ArrayList<String>(); Object[] array=list.to ...

  5. c++ string操作

    #include <iostream>#include <string> using namespace std; int main(){ string str1(" ...

  6. Linux下如何安装Nginx

    看这就够了 https://segmentfault.com/a/1190000012435644 注意如果是远程浏览器访问是否启动了nginx,出现无法访问 服务器能够启动.访问不了页面 很大可能是 ...

  7. sed例子

    以care.log这个log文件为例, care.log: 05:44:31,816 DEBUG RawAggregationWorker:70 - LTS is working on Raw Dat ...

  8. Centos7系统下以RPM方式如何安装mysql-5.7

    检查系统是否装有mariadb rpm -qa | grep mariadb 卸载mariadb 强制卸载mariadb rpm -e --nodeps mariadb-libs-5.5.35-3.e ...

  9. 集群架构04·NFS服务,环境安装

    初识 网络文件系统Netwrok File System,类似于wiin10的网络共享 功能:通过网络让不同主机系统之间可以共享文件或目录 客户端通过挂载的方式将服务器端共享的目录挂载到本地系统. 集 ...

  10. 33.TCP协议概念/scapy模块doos攻击

    TCP协议概念/scapy模块: 1,TCP/IP四层协议: 2,TCP数据包的构成: TCP FLAGS:TCP数据包标志位 U:URG,紧急比特 A:ACK,确认比特 P:PSH,推送比特 S:S ...