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.\n");
	fprintf(stderr,"The program generates 100 input files by default.\n");
	fprintf(stderr,"For each input file, you must enter three parameters, N, m, and K.\n");
	fprintf(stderr,"Please make sure that 0<=n<=10000000 0<=m<=10000000 m>=n-1.\n");
	fprintf(stderr,"N is the number of nodes in the graph, M is the number of edges in the graph\n");
	fprintf(stderr,"and K is some parameters in the graph. If you do not want to output K.\n");
	fprintf(stderr,"Please delete the code yourself.\n");
	fprintf(stderr,"MADE BY HZOI_LIUCHANG.\n");
    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\n.",kk-1);
   	fprintf(stderr,"Now the %dth set of data is generated.\n",kk);
   	fprintf(stderr,"please enter n m k\n");
   	fprintf(stderr,"And then waiting....\n");	
    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",n,m,k);
	if(m<n-1){
		fprintf(stderr,"Wrong!m must be bigger than n-1.\n");
		fprintf(stderr,"The %dth data is not generated.\n",kk);
		continue;
	}
	if(m<0 || m>10000000){
		fprintf(stderr,"Wrong!m is too big or too small.\n");
		fprintf(stderr,"The %dth data is not generated.\n",kk);
		continue;
	}
	if(n<0 || n>10000000){
		fprintf(stderr,"Wrong!n is too big or too small.\n");
		fprintf(stderr,"The %dth data is not generated.\n",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\n");
			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\n",x,y,z);
    }
	}
	if(jl){
		fprintf(stderr,"The %dth data is not generated.\n",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\n",x,y,z);
	}
    }
	return 0;
}

c++ 随机生成带权联通无向图

上一篇:这个高仿小米商城项目太惊艳了


下一篇:无向图的联通图个数 题解(数学)