亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? electric wiring.c

?? 一個自己用C編的關于貪婪算法的例子
?? C
字號:
/****************************************************************
*   File Name: Electric Wiring.c                                *
*   Purpose: To compute the nearest integer length of wire      *
*            which can have all the outlets connected.          *
*   Author: Yan Bing                                            *
*   Created date: 2005-12-19	                                *
****************************************************************/

#define OUTLETNUM 20      /* The largest number of the outlets */
#include <stdio.h>
#include <stdlib.h>

struct point              /* Define the data structure of points */
{
	float x;
	float y;
	float z;
};

struct EdgeStruct         /* Define the segment of wire between two outlets */
{
	int sp;
	int ep;
	float len;
};
typedef struct EdgeStruct *Edge;

float length,width,height;/* Parameters of the room */

/****************************************************************
*				  About Length of Edges                         *
*   Compute the shortest distance between two outlets.          *
****************************************************************/

float top,bottom,left,right,front,back,ldoor,sdoor;/* Parameters of the door */

/* Check on which wall the outlet is */
int checkwall(struct point outlet)
{
	if(outlet.y==0.0)             /* On the first wall */
	   return 0;
	if(outlet.x==length)          /* On the second wall */
		return 1;
	if(outlet.y==width)           /* On the third wall */
        return 2;
	if(outlet.x==0.0)             /* On the fourth wall */
		return 3;
	printf("The posion of the outlet is wrong.\n");
	return -1;                    /* Not on the wall, and the outlet is not legal */
}				

/* compute the lenth of the shortest path from a outlet to the Z-axis deasil */
float lengthz(struct point tmp)
{
	switch(checkwall(tmp)){
		case 0:
            return(tmp.x);                    /* The outlet on the first wall */
		case 1:
            return(length+tmp.y);             /* The outlet on the second wall */
		case 2:
            return(2*length+width-tmp.x);     /* The outlet on the third wall */
		case 3:
            return(2*length+2*width-tmp.y);   /* The outlet on the fourth wall */
		default:
            return -1;                        /* The outlet not on any wall */
	}
}

/* Return the absoolute value of x */
float abso(float x)
{
	if(x>0)
		return x;
	else
		return -x;
}

/* Return the larger one of a and b */
float Max(float a,float b)
{
	if(a>b)
	   return a;
	else
	   return b;
}

/* Return the smaller one of a and b */
float Min(float a,float b)
{
    if(a>b)
        return b;
    else
        return a;
}

/* Compute the parameters of the door */
void PositionDoor(struct point door[])
{
    right=Max(Max(door[0].x,door[1].x),Max(door[2].x,door[3].x));
	left=Min(Min(door[0].x,door[1].x),Min(door[2].x,door[3].x));
    front=Max(Max(door[0].y,door[1].y),Max(door[2].y,door[3].y));
	back=Min(Min(door[0].y,door[1].y),Min(door[2].y,door[3].y));
    top=Max(Max(door[0].z,door[1].z),Max(door[2].z,door[3].z));
	bottom=Min(Min(door[0].z,door[1].z),Min(door[2].z,door[3].z));
	ldoor=Max(Max(lengthz(door[0]),lengthz(door[1])),Max(lengthz(door[2]),lengthz(door[3])));
	sdoor=Min(Min(lengthz(door[0]),lengthz(door[1])),Min(lengthz(door[2]),lengthz(door[3])));
}

/* Find the shortest length of edge between two outlets */
float shortlen(struct point outlet1, struct point outlet2)
{
    float lout,sout;
    float path1,path2,path3,dist;
    
    lout=Max(lengthz(outlet1),lengthz(outlet2));
	sout=Min(lengthz(outlet1),lengthz(outlet2));
    switch((int)abso(checkwall(outlet1)-checkwall(outlet2))){
        case 0:                                              /* The outlets are on the same wall */
        case 1:                                              /* The outlets are on two adjacent walls */
            if(outlet1.z<=bottom || outlet2.z<=bottom || outlet1.z>=top || outlet2.z>=top)
                return lout-sout+abso(outlet1.z-outlet2.z);
            else if(ldoor<lout && sdoor>sout){
                path1=lout-sout+abso(top-outlet1.z)+abso(top-outlet2.z);
                path2=lout-sout+abso(bottom-outlet1.z)+abso(bottom-outlet2.z);
                path3=2*(length+width)-(lout-sout)+abso(outlet1.z-outlet2.z);
                return Min(Min(path1,path2),path3);
            }else
                return lout-sout+abso(outlet1.z-outlet2.z);
        case 2:                                              /* The outlets are on two opposite walls */
            if((sout>sdoor && sout<ldoor) || (lout>sdoor && lout<ldoor)){
                path1=lout-sout+abso(outlet1.z-outlet2.z);
                path2=2*(length+width)-(lout-sout)+abso(outlet1.z-outlet2.z);
                path3=Min(path1,path2);
                if(outlet1.z>=top || outlet2.z>=top){      /* One of the outlets is on the door */
                    if(checkwall(outlet1)==0 || checkwall(outlet1)==2){
                        path1=outlet1.z+outlet2.z+width+abso(outlet1.x-left)+abso(outlet2.x-left);
                        path2=outlet1.z+outlet2.z+width+abso(outlet1.x-right)+abso(outlet2.x-right);
                    }else if(checkwall(outlet1)==1 || checkwall(outlet1)==3){
                        path1=outlet1.z+outlet2.z+width+abso(outlet1.y-front)+abso(outlet2.y-front);
                        path2=outlet1.z+outlet2.z+width+abso(outlet1.y-back)+abso(outlet2.y-back);
                    }
                }else                                      /* One of the outlets is under the door */
                    path1=path2=outlet1.z+outlet2.z+width+abso(outlet1.x-outlet2.x);
            }else{
                if(outlet1.z<=bottom || outlet2.z<=bottom || outlet1.z>=top || outlet2.z>=top){
                    path1=lout-sout+abso(outlet1.z-outlet2.z);
                    path2=2*(length+width)-(lout-sout)+abso(outlet1.z-outlet2.z);
                }else if(ldoor<lout && sdoor>sout){
                    path1=lout-sout+abso(top-outlet1.z)+abso(top-outlet2.z);
                    path2=lout-sout+abso(bottom-outlet1.z)+abso(bottom-outlet2.z);
                    path1=Min(path1,path2);
                    path2=2*(length+width)-(lout-sout)+abso(outlet1.z-outlet2.z);
                }else{
                    path1=2*(length+width)-(lout-sout)+abso(top-outlet1.z)+abso(top-outlet2.z);
                    path2=2*(length+width)-(lout-sout)+abso(bottom-outlet1.z)+abso(bottom-outlet2.z);
                    path1=Min(path1,path2);
                    path2=lout-sout+abso(outlet1.z-outlet2.z);
                }
                if(checkwall(outlet1)==0 || checkwall(outlet1)==2)
                    path3=outlet1.z+outlet2.z+width+abso(outlet1.x-outlet2.x);
                else if(checkwall(outlet1)==1 || checkwall(outlet1)==3)
                    path3=outlet1.z+outlet2.z+length+abso(outlet1.y-outlet2.y);
            }
            return Min(Min(path1,path2),path3);
        case 3:                                              /* The outlets are on two adjacent walls */
            if(outlet1.z<bottom || outlet2.z<bottom || outlet1.z>top || outlet2.z>top)
                return 2*(length+width)-(lout-sout)+abso(outlet1.z-outlet2.z);
            else if(ldoor<sout || sdoor>lout){
                path1=2*(length+width)-(lout-sout)+abso(top-outlet1.z)+abso(top-outlet2.z);
                path2=2*(length+width)-(lout-sout)+abso(bottom-outlet1.z)+abso(bottom-outlet2.z);
                path3=lout-sout+abso(outlet1.z-outlet2.z);
                return Min(Min(path1,path2),path3);
            }else
                return 2*(length+width)-(lout-sout)+abso(outlet1.z-outlet2.z);
    }
}

/****************************************************************
*				  About Heap                                    *
*   Build a heap with the edges between the outlets             *
****************************************************************/

struct HeapStruct         /* Define the data structure of the collection of the segments of wire */
{
	int Capacity;
	int Size;
	Edge Elements;
};
typedef struct HeapStruct *PriorityQueue;

/* Initialize the PriorityQueue composed of MaxElements of edges */
PriorityQueue InitializeHeap(int MaxElements)
{
    PriorityQueue H;
    H=(PriorityQueue)malloc(sizeof(struct HeapStruct));
    if(H==NULL){
        printf("Fail to malloc space for heap.\n");
        exit(0);
    }
    H->Elements=(Edge)malloc(sizeof(struct EdgeStruct)*(MaxElements+1));
    if(H->Elements==NULL){
        printf("Fail to malloc space for heap.\n");
        exit(0);
    }
    H->Capacity=MaxElements;
    H->Size=0;
    H->Elements[0].len=-1;
    return H;
}

/* Delete the minimun element in the PriorityQueue H */
Edge DeleteMin(PriorityQueue H)
{
    int i,Child;
    struct EdgeStruct MinElement;
   
    MinElement.len=H->Elements[1].len;             /* Save the min element */
    MinElement.sp=H->Elements[1].sp;
    MinElement.ep=H->Elements[1].ep;
    for(i=1;i*2<H->Size;i=Child){                  /* Find smaller child */
        Child=i*2;
        if(Child<H->Size-1 && H->Elements[Child+1].len<H->Elements[Child].len)
            Child++;
        if(H->Elements[H->Size].len>H->Elements[Child].len){/* Percolate one level */
			H->Elements[i].len=H->Elements[Child].len;
			H->Elements[i].sp=H->Elements[Child].sp;
			H->Elements[i].ep=H->Elements[Child].ep;
		}else
            break;                                  /* Find the proper position */
    }
    H->Elements[i].len=H->Elements[H->Size].len;
    H->Elements[i].sp=H->Elements[H->Size].sp;
    H->Elements[i].ep=H->Elements[H->Size].ep;
    H->Elements[H->Size].len=MinElement.len;        /* Save the min element at the last element*/
    H->Elements[H->Size].sp=MinElement.sp;
    H->Elements[H->Size].ep=MinElement.ep;
    return &H->Elements[H->Size--];
}

/* Percolate down the element on the positin p in the PriorityQueue H */
void PercolateDown(int p, PriorityQueue H)
{
	int child,Tmpsp,Tmpep;
	float Tmp;
	
    Tmp=H->Elements[p].len;
	Tmpsp=H->Elements[p].sp;
	Tmpep=H->Elements[p].ep;
    for(;p*2<=H->Size;p=child){           /* Find smaller child */
        child=p*2;
		if(child!=H->Size&&H->Elements[child].len>H->Elements[child+1].len)
            child++;
		if(H->Elements[child].len>Tmp)    /* Percolate one level */
		    break;          
		else{                             /* Find the proper position */
		    H->Elements[p].len=H->Elements[child].len;
		    H->Elements[p].sp=H->Elements[child].sp;
            H->Elements[p].ep=H->Elements[child].ep;
        }
	}
	H->Elements[p].len=Tmp;
	H->Elements[p].sp=Tmpsp;
	H->Elements[p].ep=Tmpep;
}

/* Build the heap in the Initialized PriorityQueue H */
void BuildHeap(PriorityQueue H, int num, struct point outlet[])
{
	int i,j;
    int edgnum=1;
	for(i=0;i<num;i++){
		for(j=0;j<i;j++){
			H->Elements[edgnum].sp=i;
			H->Elements[edgnum].ep=j;
			H->Elements[edgnum].len=shortlen(outlet[i],outlet[j]);/* Compute the length of the edge between i and j */
			edgnum++;
		}
	}
	H->Size=--edgnum;
	for(i=edgnum/2;i>0;i--)
	   PercolateDown(i,H);                                        /* Keep the heap order of the PriorityQueue H */
}

/* Free the priorityQueue H */
void FreeHeap(PriorityQueue H)
{
    if(H!= NULL)
    {
         free(H->Elements);
         free(H);
    }
}

/****************************************************************
*				  About Kruskal's Algorithm                     *
*   Find the minimum spanning tree, and compute its length      *
****************************************************************/

/* Initialize the elements in the set */
void InitializeSet(int set[],int num)
{
	int i;
	for(i=0;i<num;i++)
        set[i]=-1;
}	

/* Find the root of the tree in which the x1 is */
int find(int arr[],int x1)                                   
{
	while(arr[x1]>=0)
		x1=arr[x1];
	return x1;
}

/* Connect the points x1 and x2 */
void unit(int arr[],int x1,int x2)
{                          
	arr[find(arr,x2)]=find(arr,x1);
}

/* Find the minimum spanning tree, and return its length */
float kruskal(int num, struct point outlet[])
{
    int Edges=0;
	float res=0;
	int set[OUTLETNUM];
	PriorityQueue H;
	Edge temp;
	
	H=InitializeHeap(num*(num-1)/2);         /* Initialize the PriorityQueue */
	BuildHeap(H,num,outlet);                 /* Build the heap with the edge between every two outlets */
	InitializeSet(set,num);                  /* Initialize the set */
	while(Edges<num-1){
		temp=DeleteMin(H);                   /* Delete the edge in the PriorityQueue H with the smallest length */
		if(find(set,temp->sp)!=find(set,temp->ep)){
			unit(set,temp->sp ,temp->ep);    /* Accept the edge */
			res+=temp->len;
			Edges++;
		}
	}
	FreeHeap(H);                             /* Free the space of the priorityQueue H*/
	return res;
}

/****************************************************************
*				  About the Mian Function                       *
*   Read the input file, and record the answer in output file   *
****************************************************************/

int main(void)
{
	FILE *inputfp,*outputfp;
	
	struct point outlet[OUTLETNUM],door[4];
	int i,N;
	float result;
	
	if((inputfp=fopen("input.txt","r"))==NULL){                  /* Open the input file for reading */
        printf("Cannot open \"input.txt\" file\n");              /* Error happens when open the input file */
        exit(0);
    }
    if((outputfp=fopen("output.txt","w"))==NULL){                /* Create the output file for writing */
        printf("Cannot open \"output.txt\" file\n");             /* Error happens when open the input file */
        exit(0);
    }
	while(fscanf(inputfp,"%f%f%f",&door[0].x,&door[0].y,&door[0].z)!=EOF){
		for(i=1;i<4;i++)                                         /* Obtain the coordinates of the door */
			fscanf(inputfp,"%f%f%f",&door[i].x,&door[i].y,&door[i].z);
		fscanf(inputfp,"%f%f%f",&length,&width,&height);         /* Obtion the size of the room */
		PositionDoor(door);                                      /* Compute the parameters of the door */
		fscanf(inputfp,"%d",&N);                                 /* Obtion the number of the outlets */
		for(i=0;i<N;i++)                                         /* Obtion the coordinates of every outlet */
			fscanf(inputfp,"%f%f%f",&outlet[i].x,&outlet[i].y,&outlet[i].z);
		result=kruskal(N,outlet);                                /* Compute the length of wire */
		fprintf(outputfp,"%d\n",(result-(int)result>0)?((int)result+1):(int)result);
	}
	fclose(inputfp);
	fclose(outputfp);
	return 0;
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩精品中文字幕一区二区三区| 91久久精品午夜一区二区| 国产一区在线看| 99精品在线观看视频| 欧美一区二区视频在线观看2022| 久久久久青草大香线综合精品| 亚洲日本电影在线| 国产一二三精品| 7777精品伊人久久久大香线蕉| 中文字幕一区二区5566日韩| 五月综合激情日本mⅴ| 91视频.com| 欧美变态tickling挠脚心| 亚洲精品视频在线观看免费| 国内成+人亚洲+欧美+综合在线| 在线中文字幕不卡| 国产精品久久久一本精品| 国产在线不卡一区| 欧美α欧美αv大片| 日韩 欧美一区二区三区| 色又黄又爽网站www久久| 亚洲国产精品成人综合| 国产一区二区三区四区五区美女 | 精油按摩中文字幕久久| 在线视频欧美精品| 亚洲欧洲中文日韩久久av乱码| 国产成人aaa| 国产日韩欧美精品电影三级在线| 另类成人小视频在线| 91精品国产91久久久久久最新毛片| 亚洲一二三四在线观看| 色综合久久综合网欧美综合网| 国产精品国产三级国产普通话三级| 国产在线精品一区二区三区不卡| 欧美成人一区二区三区在线观看| 免费在线看成人av| 日韩午夜三级在线| 久久成人免费电影| 国产欧美综合色| 不卡的av在线| 亚洲精品高清在线| 欧美亚洲图片小说| 青青草97国产精品免费观看 | 99精品久久99久久久久| 国产精品久久久久精k8| 色先锋资源久久综合| 亚洲国产成人av网| 欧美男男青年gay1069videost| 日韩一区精品字幕| 精品免费国产一区二区三区四区| 国产一区二区免费看| 国产精品欧美一级免费| 91网上在线视频| 午夜精品国产更新| 精品日韩成人av| 不卡一区二区在线| 亚洲国产你懂的| 精品欧美一区二区三区精品久久 | 91视频一区二区| 亚洲成人av一区二区三区| 欧美一区二区免费视频| 高清不卡在线观看av| 亚洲激情自拍视频| 日韩欧美一区在线| 成人性色生活片免费看爆迷你毛片| 亚洲精品乱码久久久久久久久 | 精品久久人人做人人爰| 成人黄色电影在线| 亚洲va韩国va欧美va| 精品第一国产综合精品aⅴ| 99久久国产免费看| 日本亚洲最大的色成网站www| 国产午夜精品理论片a级大结局| 色婷婷综合视频在线观看| 美女视频一区在线观看| 国产精品久久久久久妇女6080| 欧美精选在线播放| 顶级嫩模精品视频在线看| 亚洲3atv精品一区二区三区| 中文字幕高清一区| 在线综合+亚洲+欧美中文字幕| 风流少妇一区二区| 美女精品一区二区| 亚洲日本中文字幕区| 久久中文字幕电影| 欧美视频一区二区| av电影在线不卡| 国内精品伊人久久久久影院对白| 亚洲一区二区五区| 18涩涩午夜精品.www| 亚洲精品一区二区三区影院 | 99精品视频在线观看免费| 捆绑调教美女网站视频一区| 亚洲日韩欧美一区二区在线| 久久久久久毛片| 日韩午夜在线观看| 884aa四虎影成人精品一区| 91在线视频官网| 东方欧美亚洲色图在线| 久久aⅴ国产欧美74aaa| 日产精品久久久久久久性色| 亚洲愉拍自拍另类高清精品| 欧美国产精品一区| 久久久国际精品| 精品国产伦理网| 欧美电影免费观看高清完整版在线 | 99国产精品国产精品久久| 国产成人高清在线| 国产米奇在线777精品观看| 老汉av免费一区二区三区 | 国产另类ts人妖一区二区| 日本不卡123| 麻豆一区二区三| 免费在线一区观看| 美女视频网站黄色亚洲| 麻豆成人av在线| 激情五月婷婷综合网| 精品一区二区久久| 国产一区二区在线影院| 国产在线观看一区二区| 国产成人亚洲精品青草天美| 国产成人精品免费网站| 成人精品视频一区二区三区| www.亚洲色图.com| 99re这里只有精品视频首页| 一本大道综合伊人精品热热| 99精品欧美一区| 欧美性感一区二区三区| 在线播放一区二区三区| 欧美成人一区二区| 国产精品网站在线播放| 国产精品成人在线观看| 一区二区三区高清不卡| 日韩激情一区二区| 国产精品资源在线观看| av激情成人网| 欧美日韩中文一区| 26uuu精品一区二区在线观看| 久久精品一区二区三区不卡 | 精品国产乱码久久久久久浪潮| 国产色产综合产在线视频| 国产精品亲子伦对白| 夜夜嗨av一区二区三区四季av | 欧美一区二区三区电影| 国产女主播在线一区二区| 中文字幕佐山爱一区二区免费| 亚洲小少妇裸体bbw| 狠狠色伊人亚洲综合成人| 成a人片国产精品| 91精品国产综合久久久久久久| 久久综合九色综合97婷婷| 亚洲女人的天堂| 久久精品国产精品亚洲综合| 成人av在线看| 91精品免费在线观看| 国产精品国产三级国产普通话蜜臀 | 夜夜精品浪潮av一区二区三区| 奇米一区二区三区| 91亚洲午夜精品久久久久久| 7777精品伊人久久久大香线蕉超级流畅| 久久中文字幕电影| 亚洲电影在线免费观看| 成人激情视频网站| 日韩西西人体444www| 一区二区三区欧美激情| 国产精品综合一区二区三区| 欧美三级中文字| 中文字幕在线一区免费| 狠狠v欧美v日韩v亚洲ⅴ| 欧美日韩一区二区三区免费看| 中文字幕+乱码+中文字幕一区| 天堂影院一区二区| 99久久精品国产精品久久| 久久久午夜精品| 奇米精品一区二区三区在线观看 | 久久久91精品国产一区二区三区| 亚洲一区二区精品视频| 成人18精品视频| 久久婷婷成人综合色| 欧美aaaaa成人免费观看视频| 在线观看免费成人| 亚洲日本在线观看| 国产91在线看| 久久久久久久av麻豆果冻| 奇米色一区二区| 欧美一区二区三区免费观看视频| 夜夜嗨av一区二区三区中文字幕| av在线一区二区| 国产精品理论片在线观看| 国产精品一区二区免费不卡| 欧美mv日韩mv亚洲| 久久99精品久久久久久久久久久久| 欧美三电影在线| 亚洲成人av一区二区| 欧美日本韩国一区二区三区视频| 亚洲午夜在线视频| 欧美中文字幕一二三区视频| 亚洲精品国产无套在线观| 色噜噜偷拍精品综合在线| 自拍偷拍国产亚洲|