【题目大意】:给出一幅图,再这幅图上加上一幅图,再删去几个点,求最小生成树。求不到输出“what a pity!”
【解题思路】:prim直接写。一时没注意。原来有重边。
【代码】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
int n,ans,m,tmp;
int mapp[250][250],dis[250],vis[250];
void init(){
for(int i=0; i<250; i++)
for(int j=0; j<250; j++)
mapp[i][j]=inf;
memset(vis,0,sizeof(vis));
}
bool prim(){
int now,minn,minedge;
for(int i=0; i<n; i++) dis[i]=inf;
now=n;
for(int i=0; i<n; i++)
if(vis[i]==0) {now=i; break;}
ans=0;
for(int i=1; i<n-tmp; i++){
dis[now]=-1;
minedge=inf;
for(int j=0; j<n; j++)
if(vis[j]==0 && now!=j && dis[j]>0){
dis[j]=min(dis[j],mapp[now][j]);
if(dis[j]<minedge){
minedge=dis[j];
minn=j;
}
}
if(minedge==inf) return false;
now=minn;
ans+=minedge;
}
return true;
}
int main(){
int T;
int u,v,w;
cin >> T;
while(T--){
init();
scanf("%d%d",&n,&m);
for (int i=0; i<m; i++){
scanf("%d%d%d",&u,&v,&w);
if(w>=mapp[u][v]) continue;
mapp[u][v]=mapp[v][u]=w;
}
scanf("%d%d",&tmp,&m);
n+=tmp;
for (int i=0; i<m; i++){
scanf("%d%d%d",&u,&v,&w);
if(w>=mapp[u][v]) continue;
mapp[u][v]=mapp[v][u]=w;
}
scanf("%d",&tmp);
for (int i=0; i<tmp; i++){
scanf("%d",&u);
vis[u]=1;
}
if(!prim()) cout << "what a pity!" << endl;
else cout << ans << endl;
}
return 0;
}