zoukankan      html  css  js  c++  java
  • c++ 随机生成带权联通无向图

    提示

    1、请使用c++11编译运行
    2、默认生成100个输出文件,文件名为data1.in到data100.in,如有需要自行修改
    3、50000以下的点1s内可以运行结束,50000-300000的点在30s内运行结束,300000以上看人品
    4、生成的图为无向连通图,因此如果边数小于点数-1,程序自动终止
    5、如果边数点数大于10000000或者为负数,程序自动终止
    6、如果运算量过大,程序自行终止
    7、默认带有边权以及参数k
    8、4到7条可以自行修改
    9、输出到屏幕中的文字不会输出到文件中

    代码

    #include<bits/stdc++.h>
    #include<cstdio>
    #include<fstream>
    using std::cout; using std::endl;
    using std::default_random_engine;
    using std::uniform_int_distribution;
    using namespace std;
    const int maxn=10000015;
    int n,m,cnt,fa[maxn];
    char s[1000][1000];
    int zhao(int xx){
      if(fa[xx]==xx) return xx;
      else return fa[xx]=zhao(fa[xx]);
    }
    int main(){
    	fprintf(stderr,"This program is used to generate undirected connected graphs with n nodes, m edges and K weights.
    ");
    	fprintf(stderr,"The program generates 100 input files by default.
    ");
    	fprintf(stderr,"For each input file, you must enter three parameters, N, m, and K.
    ");
    	fprintf(stderr,"Please make sure that 0<=n<=10000000 0<=m<=10000000 m>=n-1.
    ");
    	fprintf(stderr,"N is the number of nodes in the graph, M is the number of edges in the graph
    ");
    	fprintf(stderr,"and K is some parameters in the graph. If you do not want to output K.
    ");
    	fprintf(stderr,"Please delete the code yourself.
    ");
    	fprintf(stderr,"MADE BY HZOI_LIUCHANG.
    ");
        default_random_engine engine(time(NULL));
        srand(time(NULL));
        for(int i=1;i<=100;i++){
           int cntt[9];
           memset(cntt,0,sizeof(cntt));
           int Num=-1;
           int now=i;
           while(now){
              cntt[++Num]=now%10;
              now/=10;
           }
           s[i][0]='d';
           s[i][1]='a';
           s[i][2]='t';
           s[i][3]='a';
           for(int j=4;j<=Num+4;j++){
              s[i][j]=cntt[Num+4-j]+'0';
           }
           s[i][Num+5]='.';
           s[i][Num+6]='i';
           s[i][Num+7]='n';
        }
        for(int kk=1;kk<=100;kk++){
       	   long long now=0;
       	if(kk>=2) fprintf(stderr,"Last num:%d is OK
    .",kk-1);
       	fprintf(stderr,"Now the %dth set of data is generated.
    ",kk);
       	fprintf(stderr,"please enter n m k
    ");
       	fprintf(stderr,"And then waiting....
    ");	
        freopen(s[kk],"w",stdout);
        cnt=0;
        int jl=0;
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        uniform_int_distribution<> dis(1,0x3f3f3f3f);
    	printf("%d %d %d
    ",n,m,k);
    	if(m<n-1){
    		fprintf(stderr,"Wrong!m must be bigger than n-1.
    ");
    		fprintf(stderr,"The %dth data is not generated.
    ",kk);
    		continue;
    	}
    	if(m<0 || m>10000000){
    		fprintf(stderr,"Wrong!m is too big or too small.
    ");
    		fprintf(stderr,"The %dth data is not generated.
    ",kk);
    		continue;
    	}
    	if(n<0 || n>10000000){
    		fprintf(stderr,"Wrong!n is too big or too small.
    ");
    		fprintf(stderr,"The %dth data is not generated.
    ",kk);
    		continue;
    	}
    	for(int i=1;i<=n;i++){
          fa[i]=i;
        }
    	while(cnt<n-1){
    		now++;
    		if(now>=10000000000){
    			fprintf(stderr,"Too large to build
    ");
    			jl=1;
    			break;
    		}
            int x=dis(engine)%n+1,y=dis(engine)%n+1;
    		int x1=zhao(x),y1=zhao(y);
    		int z=rand()*rand()%10000000+1;
    		if(x1!=y1){
            fa[x1]=y1,cnt++;
            printf("%d %d %d
    ",x,y,z);
        }
    	}
    	if(jl){
    		fprintf(stderr,"The %dth data is not generated.
    ",kk);
    		continue;
    	}
        m=m-(n-1);
        for(int i=1;i<=m;i++){
    	   int x=dis(engine)%n+1,y=dis(engine)%n+1;
           int z=rand()*rand()%10000000+1;
           printf("%d %d %d
    ",x,y,z);
    	}
        }
    	return 0;
    }
    
  • 相关阅读:
    hibernate基础18:HQL
    hibernate基础17:cascade 级联 与 Inverse 反转
    hibernate基础16:有序集合映射
    hibernate基础15:组合主键3
    hibernate基础15:组合主键2
    hibernate基础15:组合主键1
    hibernate基础14:OpenSessionInView(抽取web访问时对数据库开关事务)
    hibernate基础13:关联映射之组件映射
    hibernate基础12:关联映射之基于主键的双项多对多
    Jmeter CSV数据文件设置使用之一
  • 原文地址:https://www.cnblogs.com/liuchanglc/p/12907194.html
Copyright © 2011-2022 走看看