这题目数据应该是有毒了交了至少几十发,最后想了半天加上各种博客,直到在某个博客里看见他将输入的数据都减去1,并引起了我的注意,最后犹豫了半天将数据减一后,竟神奇的ac了。
题目:
Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.
Input
The first line contains the number of test cases.
Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities.
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
Sample Input
1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
Sample Output
1
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<string>
#include<algorithm>
#define ll long long
#define INF 0x3f3f3f3f
#define qx std::ios::sync_with_stdio(false)
using namespace std;
int n,m,k,p[510],w[255003],u[255003],v[255003],r[255003];
int cmp(const int i,const int j){return w[i]<w[j];}
int f(int x){return p[x]==x?x:p[x]=f(p[x]);}
int main(){
int t;
qx;
cin>>t;
while(t--){
cin>>n>>m>>k;
for(int i=0;i<m;i++){
cin>>u[i]>>v[i]>>w[i];
u[i]--,v[i]--;//就是这!!!!!!
}
for(int i=0;i<n;i++) p[i]=i;
for(int i=0;i<k;i++){
int z,b,s;cin>>z>>b;
for(int i=m;i<m+z-1;i++){
u[i]=b;
cin>>s;
v[i]=s;
u[i]--,v[i]--;//还有这!!!!!!
w[i]=0;
}
m+=z-1;
}
for(int j=0;j<m;j++) r[j]=j;
sort (r,r+m,cmp);
int ans=0,k=0;
for(int i=0;i<m;i++){
int e=r[i],x=f(u[e]),y=f(v[e]);
if(x!=y){
ans+=w[e];
p[x]=y;k++;
}
}
if(k!=n-1)cout<<-1<<endl;
else cout<<ans<<endl;
}
return 0;
}