#include<cstdio>
#include<algorithm>
#include<string.h>
#include<functional>
#include<cmath>
#include<iostream>
using namespace std;
class node
{
public:
int u,v;
int dist;
}edge[55002];
int num[509];
int find(int x)
{
return x==num[x]?x:num[x]=find(num[x]);
}
bool cmp(node xx,node yy)
{
return xx.dist<yy.dist;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<=508;i++)
{
num[i]=i;
}
int a,b,x,mn=1,nn,nx,mx;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&x);
edge[i].u=a;edge[i].v=b;edge[i].dist=x;
}
while(k--)
{
int tt;
int con[510]={0};
scanf("%d",&tt);
for(int i=0;i<tt;++i)
{
scanf("%d",&con[i]);
}
for(int i=1;i<tt;++i)
num[find(con[i])]=find(con[i-1]);//连接幸存的道路
}
int xn=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)//xn为幸存的道路数
{
if(i==j)
continue;
if(num[j]==i)
xn++;
}
sort(edge+1,edge+1+m,cmp);
int sum=0;
for(int i=1;xn<n&&i<=m;i++)
{
int cx=find(edge[i].u),cy=find(edge[i].v);
if(cx!=cy)
{
num[cx]=cy;
sum+=edge[i].dist;
xn++;
}
}
if(xn<n)//因为初始化为1所以如果能连通,xn==n
sum=-1;
printf("%d\n",sum);
}
return 0;
}
相关推荐
杭电OJ 大家一起AC吧
杭电OJ 大家一起AC吧 杭电OJ的网址: http://acm.hdu.edu.cn/ 毕业几年了,以前在大学里面没有努力,现在想再自己学习一下,就又开始在 OJ上刷题了,虽然说现在只会一些水题,但是还是想努力一下,希望大家一起AC吧,我后面会经常share一些自己在做题时的想法跟感受。 ...
各大OJ刷题平台汇总
本文介绍了国内外知名的在线编程刷题平台,包括力扣(LeetCode)、洛谷、C语言网、Codeforces、牛客、杭电OJ和ICPC等。这些平台各有特点:力扣支持多语言且有付费服务;洛谷适合新手且有比赛奖励;C语言网适合入门;Codeforces适合高手但访问较慢;牛客与大公司合作提供面试资源;杭电OJ题库丰富但全英文;ICPC则是全球性大学生竞赛。文章建议新手从力扣和洛谷入手,逐步提升算法能力,同时强调刷题需结合经验总结,掌握核心算法才是关键。
ACM站点搜集
欢迎大家补充 杭电 http://acm.hdu.edu.cn 力扣 https://leetcode-cn.com codeforces https://codeforces.com/problemset 计蒜客 https://nanti.jisuanke.com/acm 落谷 https://www.luogu.com.cn/problem/list 北大 htt...
HDU杭电ACMOJ训练指南
杭电OJ训练指南
http://acm.hdu.edu.cn/showproblem.php?pid=3371
这是今天xd弄的一个专题,,,,很是坑爹,,,c++过了,但是g++就是过不了,,,让我纠结了半个多小时,,,悲催,,, prim算法: #include #include #define N 505 #define M 99999999 #define FOR(i,s,t) for(int i=(s);i<=(t);++i) using namespace std; int n,m
http://acm.hdu.edu.cn/showproblem.php?pid=1034
题目意思:有输入N在第一行,然后接下来的有N行,分别是N个人的糖果数,而且老师一吹口哨,每个人都给自己右边的人二分之一自己的糖果数,如果分后糖果数量为奇数,则老师在给他一颗糖,直到每人的糖果数相等为止,问:这样老师要吹多少次口哨,并且此时糖果数为多少? 思路:首先由题可知:a[1]=a[1]/2+a[n]/2;则a[i]=a[i]/2+a[i-1]/2;这样循环,(直到i==n)为止。 一下为...
http://acm.hdu.edu.cn/showproblem.php?pid=1272。。。
思路:此题有题意可知,首先你输入的一组数字,并非原来就确定的,因此在这里就有必要在每次输入一组数据时,就有必要判断大小,方便之后的巡查,其次:就是在寻找父亲时,要注意什么时候用压缩查询,此处似乎有点出入,数据必须要标记,因为你输入的每组数字,并非在最大与最小的区间存在,每组数据都是不确定的,因此这样 的区间也是不断变化的,因此就必须时刻改变最大值与最小值,知道最后才确定区间大小,因此在之前的标记中...
acm第一次训练
第一次训练 %希望能有坚持下去的定力
各大OJ网址汇总(致力于爱刷OJ的小伙伴们)
POJ(1):http://poj.org/ POJ(2):http://poj.openjudge.cn/ HDU:http://acm.hdu.edu.cn/ 哈理工OJ:http://acm.hrbust.edu.cn/index.php# BNUOJ:http://www.bnuoj.com/ BUAAOJ:https://buaacoding.cn/index FuZhouOJ:htt...
http://acm.hdu.edu.cn/showproblem.php?pid=1114&&完全背包
题意:硬币的数量不限制,让储蓄罐装满时得到的最少价值。 思路:先将完全背包转化成多重背包,再转化成0-1背包来解,不过这里要进行二进制优化,因为这里要保证储蓄罐要装满,所以需要先把容量为0时,价值赋为0,因为让求最小价值,因此需要把大于0的容量都赋为无穷大,如果让求的是装满时的最大价值则要把大于0的容量都赋为无穷小。。。 #include #include #include using n
http://acm.hdu.edu.cn/showproblem.php?pid=1394求逆序数
这题用线段树求逆序数很巧妙,在数据输入时查询和更新线段树; 线段树果断强大;在这有个不好想到的地方就是题目给出的数字是连续的0---n-1;比如给出的序列是3,2,4,1,0;则逆序数等于8,然后把3移到后面,结果新的序列变成2,4,1,0,3;新的逆序数是8+(5-3-1)-3;其中5-3-1表示在(3,2,4,1,0)序列中比3大的数有几个;再减去3表示在(3,2,4,1,0)中3的右边比3
http://acm.hdu.edu.cn/showproblem.php?pid=1907,与杭电2509题类似。
题意:有一盒糖,里面有M中颜色的糖果,每种有Mi个,Jone和他Brother两个人轮流拿糖,每次可以拿任意颗一种颜色糖,不能不拿,谁拿到最后一颗谁输。 解题思路:利用SG函数,与NIM,求出SG的值!此题与杭电2509题类似,主要注意 条件不同,所产生的SG就不同,并且要注意当它的每堆数量为1时,要分析清楚!!! #include<iostream> #include<c...
http://acm.hdu.edu.cn/showproblem.php?pid=2579
#include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1, x2, y1, y2; char map[N][N]; int v[N][N][N];//当时间是k的倍数时,障碍消失,之后又重现,所以在平时使用的二维标记数组中再加...
http://acm.hdu.edu.cn/showproblem.php?pid=1247
木有输出东西,竟AC,,有木有。。 #include #include #include using namespace std; map Q; string s[50005]; int main() { int n=0; while(cin>>s[n]) Q[s[n++]]=1; for(int i=0;i<n;++i) { int len=s[i
http://acm.hdu.edu.cn/showproblem.php?pid=1020大水题一个不解释
#include #include #include #include #include #include #include using namespace std; int main() { int t; scanf("%d",&t); string str; while(t--) { mapM; cin>>str; for(int i=0;i<str.si
2242




被折叠的 条评论
为什么被折叠?



