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

#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. Linux生成key

    [root@centos7 ~]# ssh-keygen -b [ -t rsa #这里的-b 2048 是密钥加密的长度,最好设大点 Generating public/private rsa ke ...

  2. CLR 虚方法调用和接口方法调用

    不知接口方法和虚方法分发有什么区别?似乎在CIL中都是callvirt指令. 对,MSIL里都是callvirt,但JIT的时候得到了不同的处理:对虚方法的分发是编译成这样: mov  ecx, es ...

  3. Custom Roles Based Access Control (RBAC) in ASP.NET MVC Applications - Part 1 (Framework Introduction)

    https://www.codeproject.com/Articles/875547/Custom-Roles-Based-Access-Control-RBAC-in-ASP-NET Introd ...

  4. 【SpringBoot】转载 springboot使用thymeleaf完成数据的页面展示

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_36380516/artic ...

  5. Servlet 的三种跳转方式

    1.Forword request.getRequestDispatcher (“url”).forword (request,response) 是请求转发,也就是说,一个 Servlet 向当前的 ...

  6. nodejs连接mongodb(密码)

    const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://user:password@localhost:2 ...

  7. 按下F2编辑dxDBTreeView的节点

    在TdxDBTreeView控件的OnKeyDown事件中写入if Key = VK_F2 thenbegin  if DBTreeMain.DBSelected = nil then    Exit ...

  8. nginx 配置 https 并强制跳转(lnmp一键安装包)

    目录 一.安装包 二.配置 三.查看配置文件 3.1.设定强制跳转 https 3.2 Rewrite 常用全局变量举例 一.安装包 安装大家按照官方说的安装即可. ./install.sh lnmp ...

  9. 解决报错Fatal error in launcher

    换电脑重装python,打算安装第三方库的时候出现错误: Fatal error in launcher 然而在网上搜到的大多数是解决 —— Fatal error in launcher: Unab ...

  10. 《剑指offer》数组专题 (牛客10.22)

    目录 // Q01 二维部分有序数组查找 [善用性质] // Q06 旋转数组中的最小元素 [二分 || 暴力] Q13 调整数组顺序使奇数位于偶数前 / Q19 顺时针打印矩阵 [使用边界变量] / ...