hdu 3371 有毒的卡时间题目
同样的代码 每次交的结果都不一样 #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 有毒的卡时间题目的更多相关文章
- hdu 动态规划(46道题目)倾情奉献~ 【只提供思路与状态转移方程】(转)
HDU 动态规划(46道题目)倾情奉献~ [只提供思路与状态转移方程] Robberies http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包 ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- HDUOJ--2079选课时间(题目已修改,注意读题)
选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 2079 选课时间(题目已修改,注意读题)
http://acm.hdu.edu.cn/showproblem.php?pid=2079 背包 #include <cstdio> #include <cstring> # ...
- hdu 2079 选课时间(题目已改动,注意读题) (母函数)
代码: #include<cstdio> #include<cstring> using namespace std; int main() { int t; scanf(&q ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
随机推荐
- CISCO实验记录十一:switch端口安全配置
1.启用交换机端口安全 2.限制端口最大访问量为1,超出后关闭端口 1.启用交换机端口安全 #interface gigabitEthernet 0/1 #switchport mode access ...
- 攻防世界Hello,CTF writeup
解题过程 首先在ida中进行反汇编,查看main函数的代码: 代码的的36行处进行了一个字符串比较,如果v10的值等于v13的值会反馈一个success的输出.v13的值在第15行给出,因此需要知道v ...
- MySQL软件的相关操作
服务的启动与关闭 * 启动 net start mysql * 关闭 net stop mysql * 打开服务窗口 services.msc *查询端口号: netstat -ano mysql密码 ...
- js 删除 按钮所在的行
<body> <table id="delte"> <caption>简易购物车</caption> <tr> < ...
- 编程语言中的字面量在Objective-C中的举例
关于计算机编程语言中的字面量的介绍可参考:http://baike.baidu.com/view/1208327.htm?fr=aladdin. 下面就介绍Objective-C中的各种常用字面量: ...
- JAVA 基础编程练习题44 【程序 44 偶数的素数和】
44 [程序 44 偶数的素数和] 题目:一个偶数总能表示为两个素数之和. package cskaoyan; public class cskaoyan44 { @org.junit.Test pu ...
- CentOS7.5使用RPM包安装MySQL5.7.22
参考:https://blog.csdn.net/sevenkoala/article/details/76163853 1,安装环境查看 2,下载安装rpm包 下载地址:https://downlo ...
- 上传文件到CDN,上传后文件错误。
开始用xfp上传,发现文件错误.后来改用filezilla,上传后文件正确.害苦了我啊
- 使用FRP配置Windows远程控制
一款很好用的内网穿透工具--FRP 前言 使用内网穿透工具--FRP,以及一台云服务器当做服务端,进行简单配置后即可远程访问 配置 FRP 服务端的前提条件是需要一台具有公网 IP 的设备,得益于 F ...
- python使用socket向客户端发送数据的方法
在使用locust测试长连接的时候,所有的请求全部faillure了,所以想到手动写一个连接脚本测试一下是否能连通 因为centos7自带python2.7所以用python写一个比较方便. #!/u ...