#include<stdio.h> #include<iostream> #include<algorithm> #include<cstring> #include<math.h> #include<vector> #include<queue> #include<map> #define ll long long #define inf 0x3f3f3f3f using namespace std; int n,m,r; int per[20005]; bool flag[20005]; struct edge { int u; int v; int cost; bool vis; }; edge es[50005]; void init() { for(int i=0;i<=n+m;i++) par[i]=i; } int seek(int x) { if(x==par[x]) return x; else return par[x]=seek(par[x]); } void unite(int x,int y) { int xx=seek(x); int yy=seek(y); if(xx!=yy) par[xx]=yy; } bool same(int x,int y) { int xx=seek(x); int yy=seek(y); return (xx==yy); } int boss()//找有多少个集合,即有多少连通图 { int res=0; for(int i=0;i<=n+m;i++) { if( seek(i)==i ) res++; } return res; } bool cmp(edge e1,edge e2) { if( flag[e1.u]==false && flag[e2.u]==false ) return e1.cost<e2.cost; else if( flag[e1.u]==false ) } int Kruskal() { sort(es,es+n,cmp); int res=0; for(int i=0;i<r;i++) { edge e=es[i]; if(!same(e.u,e.v))//不在同一棵树就 { unite(e.u,e.v); flag[e.u]=flag[e.v]=false;//标记这些点已经被加入到集合里了 res+=e.cost; } } } int main() { int t; scanf("%d",&t); while(t--) { memset(flag,false,sizeof(flag)); init(); scanf("%d%d%d",n,m,r);//女,男,关系数,人当做点,从0开始 int x,y,d; for(int i=0;i<r;i++) { scanf("%d%d%d",&x,&y,&d); es[i].u=x; es[i].v=(n+1+y);//把男女都当做一类点 es[i].cost=10000-d; unite(x,n+1+v); } } return 0; }
招兵
猜你喜欢
转载自www.cnblogs.com/shoulinniao/p/10328054.html
今日推荐
周排行