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 ...
随机推荐
- 响应式css样式
<div class="a"> 123 </div> @media(orientation:portrait) and (max-width:600px){ ...
- mongodb的安装及使用
1.MongoDB安装 安装包下载地址: https://www.mongodb.com/download-center/community 启动数据库:进入到mongd所在的bin目录,执行mong ...
- 数据库访问接口之ODBC
ODBC API 实现数据库操作的手段是句柄.在ODBC中,使用不同的句柄(HANDLE)来标志环境(environment).连接(Connection).语句(statement).描述符(des ...
- rc.local 注意事項,call python script, file position
如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...
- 美国top200药品2
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- Linux学习笔记之系统中的分区和文件系统
转自 http://blog.csdn.net/hanxuehen/article/details/8229472
- word内存不足 解决办法
word2013内存不足 解决办法具体操作如下: 尝试了很多方法和经验表示都没有效果,最后找到个有效果的分享给大家: 点击“文件”——选项——加载项“ 点左下的”管理“-” 转到“ 把加载项前面的 ...
- 利用Anaconda搭建TensorFlow环境并在Jupyter Notebook使用
打开Anaconda Prompt 创建一个tensorflow 虚拟环境:conda create -n tensorflow python=3.6 激活tensorflow虚拟环境activate ...
- ubuntu16.04 下通过rc.d(rc.local)实现开机启动(未登录)anydesk
先编辑anydesk-X.X.X/init/anydesk文件,将"DAEMON=//usr/bin$NAME"改成"DAEMON=/XXX/anydesk-5.1.1/ ...
- Mac下Unity使用Jenkins自动化打包
重要的事情说三遍:不要使用jenkins dmg安装包直接安装,用brew安装 PS:会有权限问题 重要的事情说三遍:不要使用jenkins dmg安装包直接安装,用brew安装 重要的事情说三遍: ...