zoukankan      html  css  js  c++  java
  • Kruskal模板

    【代码】

    Kruskal模板

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<set>
    using namespace std;
    #define f(i,n) for(int i=1;i<=(n);i++)
    #define ll long long
    #define INF 1<<30
    #define N 100010
    int read()
    {
     	int x=0,f=1;char c=getchar();
     	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
     	while( isdigit(c)){x=x*10+c-'0';c=getchar();}
     	return x*f;
    }
    int n,m; 
    struct xj
    {
    	int x;
    	int y;
    	int v;
    }a[N];
    bool cmp(xj a,xj b)
    {
    	return a.v<b.v;
    }
    int fa[N];
    int find(int x)
    {
    	if(x==fa[x])return fa[x];
    	return fa[x]=find(fa[x]);
    }
    int main()
    {
    	n=read();
    	m=read();
    	f(i,n)fa[i]=i;
    	f(i,m)a[i].x=read(),a[i].y=read(),a[i].v=read();
    	sort(a+1,a+1+m,cmp);
    	int cnt=0;
    	f(i,m)
    	{
    		int u=find(a[i].x),v=find(a[i].y);
    		if(u==v)continue;
    		fa[u]=v;
    		cnt++;
    		if(cnt==n-1)break;
    	}
    }
    
    
  • 相关阅读:
    练习5.6.3节
    size_t
    练习3.43
    use include to read a file
    ACM数学(转)
    POJ 2039 To and Fro
    poj 1716 差分约束
    poj 3159 差分约束
    hdu 4571 floyd+动态规划
    poj 1364 差分约束
  • 原文地址:https://www.cnblogs.com/qwerfcxs/p/7807696.html
Copyright © 2011-2022 走看看