algorithm

5 篇文章 1 订阅
#include "deploy.h"
#include <stdio.h>
#include <iostream>
#include<stack>		//后进先出
#include<queue>		//先进先出
#include<vector>

#define MAX_NUM 1000000001

using namespace std;
clock_t start, finish;
double duration;

int con_need;
int  dis[1500];
struct EDGE {			// struct of edge 1000*20
	int ivex, jvex;
	struct EDGE *ilink , *jlink;
	int id;
	int width;	//带宽
	int price;	//单价,反向用负价
	int flow;	//可修改,现有流
	int flag;	//flow的方向,1:X->y   0 :y->x ,2:无方向
	int orient; //使用的方向
			
	};
struct EDGE Edge[10000];
struct EDGE *ip;

struct VEX{
	int id;
	struct VEX *father;
	struct EDGE *faedge;
	bool mark;
	struct EDGE *p; //网络节点<1000,边数<20
	stack<int> way;  //可修改,到达服务器经过的点
	stack<int> edge; //可修改,到达服务器经过的边
	vector<int>near; 
	
	} Vex[1000];
struct SubVex{   //大规模:依据顶点的邻接点数排序,小规模:赋值.来安放服务器,
	int num;	//邻接点数
	int vex;	//顶点号,为服务器点
	}servex[1000];	
struct CONSUM{	//消费节点<500
	int node;
	int need;
	};
struct CONSUM Con[600];	
struct CONSUM subcon[600];   //只是用来带宽排序

struct PATH{
	int price;		 //单价累加
	int flow;   		// 可用流
	queue <int> edge;  //路过的边
	queue <int> vex;  //路过的点
}Path ;
int pathnum=0;
bool mark[1000];//SPFA中的顶点标记
string res;//先打印网络条数,再打印具体路径;
int server_price=0;
int vex_num=0,edge_num=0,con_num=0;   //文件首行参数
int edgenum=0,vexnum=0;//作为edge_num 的备份,
int ser_used=0;		//服务器的使用数量
int allcost=0;
int mincost=MAX_NUM;
char a[20];
int test;
//	cout<<"cin"; cin>>test; cout<<endl;
int allflow=0;
void code();
int	min_num=0;
bool fmin=false;
vector <int> site;  //服务器位置
vector <int> copysite;  //服务器位置
vector <int> source;  //服务器的备选位置

void con_sort()		//按消费带宽排序
{
	struct CONSUM temp;
    int i,j;
    for (  j = 0; j < con_num - 1; j++)
        for (  i = 0; i < con_num - 1 - j; i++)
        {
            if(subcon[i].need < subcon[i + 1].need)
            {
                temp = subcon[i];
                subcon[i] = subcon[i + 1];
                subcon[i + 1] = temp;
            }
        }
//    for ( int i = 0; i < con_num; i += 1)
//    {
//    	cout<<"subcon width need: "<<subcon[i].need<<endl;
//    }
}

void vex_sort()			//按邻边数,排序顶点
{
	struct SubVex temp;
	int i,j;
	for (  j = 0; j < vex_num - 1; j++)
	    for (  i = 0; i < vex_num - 1 - j; i++)
	    {
	        if(servex[i].num < servex[i + 1].num)
	        {
	            temp = servex[i];
	            servex[i] = servex[i + 1];
	            servex[i + 1] = temp;
	        }
	    }
//	for ( int i = 0; i < vex_num/2; i += 1)
//	{
//		cout<<"servex "<<servex[i].vex<<" : "<<servex[i].num<<endl;
//	}
}
void Print( )
{
	int temp=0;
	pathnum++;
	while ( !Path .vex.empty())
	{
		temp=Path.vex.front();
		sprintf(a, "%d ", temp);//节点放入路径中
		res+=a;
		Path.vex.pop();//删除顶点
	}
	for ( int i = 0; i < con_num; i += 1)
	{
		if (Con[i].node==temp)
		{
			sprintf(a, "%d ", i);//节点放入路径中
			res+=a;
		}
	}
	sprintf(a, "%d\n", Path.flow  );//节点放入路径中
	res+=a;	
}




bool  DFS( ) 
{
	queue<int> q; 
	q.push(vex_num-2);  
	
	Path.flow=1000;
	int vex=-1;          //队列中的顶点名
//	for ( int i = 0; i < vex_num; i += 1)
//	{
//		Vex[i].mark=false;
//	}
	Vex[vex_num-2].mark=true;
	
	loops:
	
	while (!q.empty() )
	{
		vex=q.front();   q.pop(); //访问对首元素 //  清除第一个元素
 		//cout<<vex<<"的邻接点:"<<endl;
	  	ip=Vex[vex].p;  
	  //cout<<"cin"; cin>>test; cout<<endl;
		while(ip!=NULL)  //顶点 vex 的所有邻接点,并把邻接点入队
	  	{ 
	  		
	  		int next=(ip->ivex != vex ? ip->ivex:ip->jvex);
			int orient=(ip->ivex == vex ?1:0);//规定:第一个位置是vex 为正向,值1
//	  		cout<<"遇到终点:"<<next<<endl;
//	  		if (Vex[next].mark==false)
//	  		{
				//cout<<"边:"<<ip->id<<"  : "<<ip->ivex<<","<<ip->jvex<< " .  "<<vex<<","<<next<<"  ori: "<<orient<<endl;
				if(next==vex_num-1)
				{
					return true;
				}
				
				if (ip->flow>0 && orient==ip->flag )
				{
					//cout<<"点入队"<<next<<" flag: "<< ip->flag<<endl;
					Path.vex.push(next);
					Path.edge.push(ip->id);
					Path.price+=ip->price;
					Path.flow=(Path.flow < ip->flow? Path.flow : ip->flow) ;
					ip= Vex[next].p;
					vex=next;
					q.push(next); //顶点入队
				 
					goto loops;
				}
				Vex[next].mark=true;	   //标记,访问过.
//	  		 } 
	 		 ip=(ip->ivex == vex?ip->ilink:ip=ip->jlink);   ///从下一条边,找下一个邻接点
	  	}
	}
	//cout<<"边72  : " <<Edge[72].ivex<<","<<Edge[72].jvex<< " .flow  "<<Edge[72].flow<<",flag " <<Edge[72].flag<<endl;
	//cout<<"can not find way   "<<endl;
	return false;	
}

void ControlDFS()
{
	while (DFS()==true)//遇到终点返回true
	{
		Print();//打印路径
		
		while (!Path.edge.empty() )
		{
			Edge[Path.edge.front()].flow -= Path.flow;
//			cout<<"edge "<<Edge[Path.edge.front()].id<<"  剩余流: "<<Edge[Path.edge.front()].flow<<endl;
			Path.edge.pop();
		}
		//cout<<"cin:"; cin>>test; cout<<endl;
	}
}




bool  SPFA()//zui 
{
	queue<int> q;  
	q.push(vex_num-2);  //超级源节点入队	
	int vex=-1;          
	for(int i=0;i<=vex_num+1;i++)//从0开始
	{
		dis[i]=MAX_NUM;
//		Vex[i].father=NULL;//父点
//	    Vex[i].faedge=NULL;     //父边
//	    mark[i]=false;
	}
	dis[vex_num-2]=0;
	while (!q.empty() )
	{
		vex=q.front();   q.pop(); //访问对首元素 //  清除第一个元素
		mark[vex]=false;          //出队,做记录
 
	  	ip=Vex[vex].p;             // 顶点指向的第一条边,ip是边ip 
		while(ip!=NULL)  
	  	{ 		  		
 			int next=(ip->ivex != vex ? ip->ivex:ip->jvex);//顶点 vex 指向这个边ip , 这个边ip 有2个值,一个vex ,另一个next .
	  		int orient=(ip->ivex == vex ?1:0);//规定:第一个位置是vex 为正向,值1
			//cout<<"next: "<< next<< endl;
			int tprice=ip->price;
			if (ip->flow> 0 )
			{
				tprice=( ip->flag ==orient? ip->price: -ip->price);
			}
			//cout<<vex<<" dis:"<<dis[vex]<<"   "<<next <<"  dis:"<<dis[next]<<endl;
			//cout<<vex<<" tprice:"<<tprice<<endl;
 			// cout<<vex<<" vexdis:"<<dis[vex]<<"  tprice:"<<tprice<<"  "<<next <<" ndis:"<<dis[next]<<endl;
	  		if (   (ip->width - ip->flow) >0 && dis[next]>dis[vex]+tprice)//有剩余流量就可过
	  		{
	  			
		        dis[next]=dis[vex]+tprice;
 		        //cout<<vex<<" vexdis:"<<dis[vex]<<"  tprice:"<<tprice<<"  "<<next <<" ndis:"<<dis[next]<<endl;
	           	ip->orient=orient;       //存这次路过的方向,不改流的方向
	        	Vex[next].father=&Vex[vex];//父点
	        	Vex[next].faedge=ip;     //父边
		        	
		        if( (mark[next]!=true) ) //不在队
		        {
		        	q.push(next);  
		            mark[next]=true;
		        }
	  		} 
	  		ip=(ip->ivex == vex?ip->ilink:ip=ip->jlink);   ///从下一条边,找下一个邻接点
	  	}
	}
	return  (dis[vex_num-1]==MAX_NUM?false:true);
}

bool ControlSPFA()
{

	allcost=0;
	allflow=0;
	for ( int i = 0; i < edge_num; i += 1)   //恢复剩余带宽
	{
		Edge[i].flow = 0;
		Edge[i].flag = 2;  //初始化,无方向
	}
	while (SPFA()==true )
	{
		int limit=1000;
		struct VEX *temp=&Vex[vex_num-1];//鸡尾node
		while (temp!=NULL&&temp->faedge!=NULL)//确定可通流limit
		{
			//cout<<"edge id "<<temp->faedge->id<<endl;
			if ((temp->faedge->flag ==2)|| (temp->faedge->flag ==temp->faedge->orient ))//正向
			{
				limit= (temp->faedge->width- temp->faedge->flow)<limit? (temp->faedge->width- temp->faedge->flow):limit;
			}
			else
			{
				limit= temp->faedge->flow < limit?temp->faedge->flow:limit;
			}	
			temp=temp->father;
		}
		allflow +=limit;//累加已用流
		
		allcost+=limit*dis[vex_num-1];
 	
		temp=&Vex[vex_num-1];//从终点再次出发,
		
		//cout<<"cin:"; cin>>test; cout<<endl;
		while (temp!=NULL&&temp->faedge!=NULL)//修改边的流量,方向
		{
			if  (temp->faedge->flag ==temp->faedge->orient ) //正向
			{
			
				temp->faedge->flow +=limit;
// 				cout<<"=="<<endl;
//				cout<<"edge ("<<temp->faedge->ivex<<" "<<temp->faedge->jvex<<") flow: "<<temp->faedge->flow;
//				cout<<" width: "<<	temp->faedge->width<<endl;
//				cout<< "flag "<<temp->faedge->flag<<"  ori " << temp->faedge->orient <<endl;
				temp=temp->father;
				continue;
			}
			if  (temp->faedge->flag !=2&&(temp->faedge->flag !=temp->faedge->orient )) //f向
			{
			
				if (temp->faedge->flow ==limit)
				{
					temp->faedge->flow =0;
					temp->faedge->flag =2;
				}
				else
				{
					temp->faedge->flow -=limit;
				}
// 				cout<<"!="<<endl;
//				cout<<"edge ("<<temp->faedge->ivex<<" "<<temp->faedge->jvex<<") flow: "<<temp->faedge->flow;
//				cout<<" width: "<<	temp->faedge->width<<endl;
//				cout<< "flag "<<temp->faedge->flag<<"  ori " << temp->faedge->orient <<endl;
				temp=temp->father;
				continue;
			}
			if (temp->faedge->flag ==2)//未使用,现有flow==0
			{
			
				temp->faedge->flow =limit;
				temp->faedge->flag =temp->faedge->orient;
				temp=temp->father;
				continue;
			}
		}
	}
	allcost+=site.size()*server_price;
	return allflow<con_need? false: true;
}





vector<int> chose()
{
	
	vector<int>connode;
	for ( int i = 0; i < con_num; i += 1)
	{
		connode.push_back(Con[i].node);
	}
//	for ( int i = 0; i < con_num; i += 1)
//	{
//		cout<<" "<<connode[i];
//	}
	cout<<endl;
	int temp=10000000;
	allcost=100000000;
	int index;
	int lastcost=1000000;
	
	while( connode.size() >0)
	{
		for ( unsigned int i = 0; i <connode.size(); i += 1)
		{
			site=connode;
			site.erase(site.begin()+i);
			code();//编入服务器,消费点

			if (ControlSPFA()==true )
			{
				//cout<<"cost: "<<allcost<<endl; ;
				if (allcost < temp)
				{
					temp=allcost;
					index=i;
					//cout<<"index:"<<i<<" temp: "<<temp<<endl;
				}
			}
		}
		if (lastcost > temp )
		{
			cout<<"temps : "<<temp<<endl;
			lastcost=temp;
			connode.erase(connode.begin()+index);
		}
		else break;
		
	}
	mincost=lastcost;
	return connode;
}



void small()
{
	
	site=chose();

	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	int index=0;
	copysite = site;//保存改动
	while(duration<88)//邻边搜索
	{
		//cout<<"time:"<<duration<<endl;
		int tt=finish;

			again:
		index=tt %(site.size()-1);
		if ( Vex[site[index]].near.size()<2)
		{
		finish = clock();
		tt=finish;
		goto again;
		}
		int near_index= tt% ( Vex[site[index]].near.size()-1);
		//cout<< " site: "<<site[index]<<" near_in:"<<near_index<<endl;
		site[index] = Vex[site[index]].near[near_index ];
		code(); //编入服务器,消费点

		if (ControlSPFA()==true && allcost<mincost)
		{
			//cout<<"allcost: "<<allcost<<" mincost:"<<mincost<<endl;
			mincost = allcost;
			copysite = site;//保存改动
		}
		else
		{
			 site=copysite;//复原
		}
		/// 时间 //
		finish = clock();
		duration = (double)(finish - start) / CLOCKS_PER_SEC;
	}
	
 	site= copysite;//接受最优解






	code();//编入服务器,消费点
	ControlSPFA();//按最优解找路
	ControlDFS();//查找,打印已有的路径
}

vector<int> big_chose()
{
	
	vector<int>connode;
	for ( int i = 0; i < 4*con_num/4; i += 1)
	{
		connode.push_back(subcon[i ].node);
	}
	for ( int i = 0; i < connode.size(); i += 1)
	{
		cout<<" "<<connode[i];
	}
	cout<<endl;
	int temp=10000000;
	allcost=100000000;
	int index;
	int lastcost=1000000;
	int it=0;
	while(it<5)
	{
	

		for ( unsigned int k = 1; k <5; k += 1)
		{
	
			for ( unsigned int i = (k-1)/4; i <connode.size()*k/4; i += 1)
			{
				//int tt=finish;
			//index=tt %(site.size()-1);
				site=connode;
				site.erase(site.begin()+i);
				//site.erase(site.begin()+(int)(connode.size()/6));

				code();//编入服务器,消费点

				if (ControlSPFA()==true )
				{
					 cout<<"cost: "<<allcost<<endl; ;
					if (allcost < temp)
					{
						temp=allcost;
						index=i;
						 cout<<"index:"<<i<<" temp: "<<temp<<endl;
					}
				}
				finish = clock();
				duration = (double)(finish - start) / CLOCKS_PER_SEC;
				if (duration>85)
				{
					return connode;
				}
			}
			if (lastcost > temp )
			{
				cout<<"temps : "<<temp<<endl;
				lastcost=temp;
				connode.erase(connode.begin()+index);
			}
			else break;
		}
		it++;
	}
	mincost=lastcost;
	//cin>>test;
	return connode;
}

//大图专用
void norm_s()
{
	
	cout<<"norm "<<endl;
	
	site=big_chose();

	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	int index=0;
	copysite = site;//保存改动
	while(duration<87)//邻边搜索
	{
		cout<<"time:"<<duration<<endl;
		int tt=finish;
		index=tt %(site.size()-1);
		int near_index= tt% ( Vex[site[index]].near.size()-1);
		
		
		cout<< " site: "<<site[index]<<" near_in:"<<near_index<<endl;
		site[index] = Vex[site[index]].near[near_index ];
		code(); //编入服务器,消费点

		if (ControlSPFA()==true && allcost<mincost)
		{
			cout<<"allcost: "<<allcost<<" mincost:"<<mincost<<endl;
			mincost = allcost;
			copysite = site;//保存改动
		}
		else
		{
			 site=copysite;//复原
		}
		/// 时间 //
		finish = clock();
		duration = (double)(finish - start) / CLOCKS_PER_SEC;
	}
	
 	site= copysite;//接受最优解

	code();//编入服务器,消费点
	ControlSPFA();//按最优解找路
	cout<<"最优地址: ";
	for ( unsigned int i = 0; i < site.size(); i += 1)
	{
		cout<<site[i]<<"  ";
	}
	cout<<"  \n最优数量:"<<site.size()<<endl;
	ControlDFS();//查找,打印已有的路径
}




void big()
{
	vex_sort();      //call 顶点,邻变数排序
	ser_used=70;
	
	for ( int i = 0; i < 70; i += 1)
 	{
		site.push_back(subcon[i].node);
		//cout<<"i "<<i<<" con "<<subcon[i].node<<endl;
 	}	
	
//	for ( int i = 0; i < 5; i += 1)
// 	{
//		site.push_back(servex[i].vex);
		cout<<"i "<<i<<" vex: "<<servex[i].vex<<endl;
// 	}	
	
	start:
		
	site.push_back(subcon[ser_used].node);
	ser_used++;
	
	code();//编入服务器,消费点
	if (ControlSPFA()==false)	
	goto start;	//循环

	mincost = allcost;   //allcost由ControlSPFA() 函数修改.
	copysite= site;
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	int itor=0;
	while(itor<50 &&duration<75)//最优数量
	{
	
		itor++;
		//cout<<"time:"<<duration<<endl;
	
		site.push_back(subcon[ser_used].node);
		ser_used++;
		
		//cout<<"ser_used "<<ser_used<<endl;
		code();//编入服务器,消费点
	
	 
		ControlSPFA();
		if (allcost<mincost)//费用减小
		{
			//cout<<"allcost:"<<allcost<<"  mincost:"<<mincost<<endl;
			mincost = allcost;
			copysite = site;
			//cin>>test;
		}
		//else break;
		finish = clock();
		duration = (double)(finish - start) / CLOCKS_PER_SEC;
	}
	int index=0;
	site= copysite;//保存改动
	while(duration<88)//邻边搜索
	{
		cout<<"time:"<<duration<<endl;
		int tt=finish;
		
		again:
		index=tt %(site.size()-1);
		if ( Vex[site[index]].near.size()<2)
		{
		finish = clock();
		tt=finish;
		goto again;
		}
		int near_index= tt% ( Vex[site[index]].near.size()-1);
		
		
		cout<< " site: "<<site[index]<<" near_in:"<<near_index<<endl;
		site[index] = Vex[site[index]].near[near_index ];
		code(); //编入服务器,消费点

		if (ControlSPFA()==true && allcost<mincost)
		{
			cout<<"allcost: "<<allcost<<" mincost:"<<mincost<<endl;
			mincost = allcost;
			copysite = site;//保存改动
		}
		else
		{
			 site=copysite;//复原
		}
		/// 时间 //
		finish = clock();
		duration = (double)(finish - start) / CLOCKS_PER_SEC;
	}
	site= copysite;
	
	cout<<"exit"<<endl;
	code();//编入服务器,消费
	ControlSPFA();
	ControlDFS();//查找,打印已有的路径
}


void CONTROL()
{

	if (vex_num<500)
	{
		small();
	}
	else
	{
		big();
	}
}



//	for ( int i = 0; i < edge_num; i += 1)
//	{
//		cout<<"edge "<<i<<"  i: "<<Edge[i].ivex<<"  j:"<<Edge[i].jvex<<"  flow:"<<Edge[i].flow<<" flag:"<<Edge[i].flag << endl;
//	}


void code()
{		
		//2种选位置的方向,1,servex[i].vex;根据邻边个数,2,subcon[i].node 根据消费节点带宽,直接诶接在消费节点.
		vex_num=vexnum;
		edge_num=edgenum;
		for (unsigned int i = edgenum-1; i <edgenum+ site.size() +con_num+1; i += 1)
		{
			Edge[i].ilink=NULL;
			Edge[i].jlink=NULL;
		}
		for ( unsigned int i = 0; i < site.size() ; i += 1)//源点编号vex_num(文件顶点数),链接ser_used条直接到达服务器的边
		{
			Edge[edge_num].ivex=vex_num;
			Edge[edge_num].jvex=site[i];
			
			Edge[edge_num].price=0;
			Edge[edge_num].width=1000000;
			Edge[edge_num].id=edge_num;
			edge_num++;
		}
		vex_num++;
		for ( int i = 0; i < con_num; i += 1)//终点,消费节点直接接在vex_num-1节点
		{
			Edge[edge_num].ivex=vex_num;
			Edge[edge_num].jvex=Con[i].node;
			Edge[edge_num].price=0;
			Edge[edge_num].width=Con[i].need;
			Edge[edge_num].id=edge_num;
			edge_num++;
		}
		vex_num++;
			
//	Edge[edge_num-con_num-1].ilink=NULL;
//	Edge[edge_num-con_num-1].jlink=NULL;
		for (int id= 0; id< vex_num; id += 1)  //节点编号 Vex[id] 数组的索引, 同时id 也是节点的名称
		{
			int tem=-1;
			for (int j = 0; j < edge_num; j += 1)  // j 是数组Edge[j] 的索引
			{
				if (Edge[j].ivex ==id ||Edge[j].jvex ==id )
				{
					if (tem!=-1)
					{
						if (Edge[tem].ivex ==id )
						{
							Edge[tem].ilink = &Edge[j];
						}
						else
						{
							Edge[tem].jlink = &Edge[j];						
						}								
					}
					else
					{
						Vex[id].p=&Edge[j];
					}
					tem=j;
				}
			}
		}
		for ( int i = 0; i < vex_num; i += 1)
		{
			Vex[i].id=i;
		}
//		for ( int i = 0; i <vex_num; i += 1)
//		{
//		 	cout<<"\nvex"<<i<<"的邻接点:  ";
//			ip=Vex[i].p;
//			int k=0;
//			while(ip!=NULL)
//		  	{
//		  		
//		  		if (ip->ivex == i)
//		  		{
//		  			   cout <<"("<<ip->ivex <<", "<<ip->jvex <<")"<<endl;
//		  			ip=ip->ilink;
//		  		}
//		  		else
//		  		{
//		  			   cout <<"(" <<ip->ivex <<", " <<ip->jvex <<")"<<endl;
//		  			ip=ip->jlink;
//		  		}	
//		  		k++;  		
//		  	}
//		   	 cout<<"  共"<<k<<" 个邻接点."<<endl;;
//		}	
//		cin>>test;
}



void deploy_server(char * topo[MAX_EDGE_NUM], int line_num,char * filename)
{
	start = clock();
    cout <<"first line: "<<topo[0]<<endl;
 	char *c= topo[0];		//read net node ,edge num, consumer num;
    int spaceCount = 0;
    while (*c != '\0' && *c != '\n' && *c != '\r')//  读取第一行获得,顶点,边,消费点数目
    {
        if (spaceCount==3)
        {
        	break;
        }
        if (*c == ' ') 
        {
            c++;
            spaceCount++;
            continue;
        }      
        if (spaceCount == 0)  
        {
            vex_num = *c - '0' + vex_num * 10;
        }
  		if (spaceCount == 1)  
        {
            edge_num = *c - '0' + edge_num * 10;
        }
        if (spaceCount == 2) 
        {
            con_num = *c - '0' + con_num * 10;
        }
        c++;
    }
    edgenum=edge_num;
    vexnum=vex_num;
	c= topo[2];		                            //read server price
	while (*c != '\0' && *c != '\n' && *c != '\r') 
    {
 
        if (*c == ' ') 
        {
            break;
        }
        server_price = *c - '0' + server_price * 10;
        c++;
    }
    for (int i = 4; i < edge_num+4; i++)		//read dege
    {
    	Edge[i-4].id = i-4 ; 					//后期增加的数据,方便从边找到id
        c = topo[i];
        spaceCount = 0;
        while (*c != '\0' && *c != '\n' && *c != '\r') 
        {
			if (spaceCount==4)
     	    {
        		break;
			}
		    if (*c == ' ') 
		    {
		        c++;
		        spaceCount++;
		        continue;
		    }
		    if (spaceCount == 0)  
		    {
		        Edge[i-4].ivex  = *c - '0' + Edge[i-4].ivex * 10;
		    }
	  		if (spaceCount == 1)  
		    {
		        Edge[i-4].jvex = *c - '0' + Edge[i-4].jvex  * 10;
		    }
		    if (spaceCount == 2)   
		    {
		        Edge[i-4].width = *c - '0' + Edge[i-4].width * 10;
		    }
		     if (spaceCount == 3) 
		    {
		        Edge[i-4].price = *c - '0' + Edge[i-4].price * 10;
		    }
		    
		    Edge[i-4].ilink=Edge[i-4].jlink=NULL;   
		    c++;     
        } 
     }   
	 for (int i = edge_num+5; i < edge_num+5+con_num; i++)		//read consumer
    {
        c = topo[i];
        spaceCount = 0;
        int temp=0;
        while (*c != '\0' && *c != '\n' && *c != '\r') 
        {
			if (spaceCount==3)
     	    {
        		break;
			}
		    if (*c == ' ') 
		    {
		        c++;
		        spaceCount++;
		        continue;
		    }
		    
		    if (spaceCount == 0)  
		    {
		        temp  = *c - '0' + temp * 10;
		    }
	  		if (spaceCount == 1)  
		    {
		        Con[i-edge_num-5].node = *c - '0' + Con[i-edge_num-5].node  * 10;
		    }
		    if (spaceCount == 2) 
		    {
		        Con[i-edge_num-5].need = *c - '0' +   Con[i-edge_num-5].need * 10;
		    }
		    c++;     
        }
	}   
	/// read data finfish /
	for ( int i = 0; i < con_num; i += 1)
	{
		con_need += Con[i].need;//统计总流量
		subcon[i].node=Con[i].node;
		subcon[i].need=Con[i].need;
	}
	
	///
	
	
	
	// etsablish connection  ///
	for (int id= 0; id< vex_num; id += 1)  //节点编号 Vex[id] 数组的索引, 同时id 也是节点的名称
	{
		int tem=-1;
		for (int j = 0; j < edge_num; j += 1)  // j 是数组Edge[j] 的索引
		{
			if (Edge[j].ivex ==id ||Edge[j].jvex ==id )
			{
				if (tem!=-1)
				{
					if (Edge[tem].ivex ==id )
					{
						Edge[tem].ilink = &Edge[j];
//						cout<<"i--link: "<<"("<<Edge[tem].ivex<<", "<<Edge[tem].jvex<<")"<<"("<<Edge[tem].ilink->ivex<<","<<Edge[tem].ilink->jvex<<")"<<endl;
					}
					else
					{
						Edge[tem].jlink = &Edge[j];						
//						cout<<"j--link: "<<"("<<Edge[tem].ivex<<", "<<Edge[tem].jvex<<")"<<"("<<Edge[tem].jlink->ivex<<","<<Edge[tem].jlink->jvex<<")"<<endl;
					}								
				}
				else
				{
					Vex[id].p=&Edge[j];
				}
				tem=j;
			}
		}
	}
	///读取数据结束
	//cout<<"检查邻接点的个数:"<<endl;//按邻接点数量对顶点排序
	vector <int> vec;
	for ( int i = 0; i <vex_num; i += 1)
	{
	 	// cout<<"\nvex"<<i<<"的邻接点:  ";
		ip=Vex[i].p;
		servex[i].vex=i;
		int k=0;
		while(ip!=NULL)
	  	{
	  		
	  		if (ip->ivex == i)
	  		{
	  			//   cout << ip->jvex <<", ";
	  			Vex[i].near.push_back(ip->jvex ); 
	  			ip=ip->ilink;//指针传递后可能为空,不能直接用
	  		}
	  		else
	  		{ 
	  			// cout << ip->ivex <<", ";
	  			Vex[i].near.push_back(ip->ivex );
	  			ip=ip->jlink;
	  		}	
	  		k++;  		
	  	}
	  	servex[i].num=k;
	   // cout<<"  共"<<k<<"个邻接点:  ";
	  	// cout<<endl;
	}	
	
//	for ( int i = 0; i < vex_num; i += 1)
//	{
//		for ( int j = 0; j < Vex[i].near.size(); j += 1)
//		{
//			cout<<Vex[i].near[j]<<"  ";
//		}
//		cout<<endl;
//	}


	
	con_sort();	

	CONTROL();
	
	cout<<"\nser_used:  "<<site.size()<<endl;
	cout<<"all flow:  "<<allflow<<endl;
	cout<<"con_need:  "<<con_need<<endl;
	cout<<"pathnum:  "<<pathnum<<endl;
	cout<<"费用:   "<<allcost<<endl;
	
	
	finish = clock();
	duration = (double)(finish - start) / CLOCKS_PER_SEC;
	printf( "The time of deploy was used is: %f seconds\n", duration );
	
	sprintf(a, "%d\n\n", pathnum  );//节点放入路径中
	res=a+ res;	
    
	char * topo_file = (char *)res.c_str();
	write_result(topo_file, filename);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值