Пример #1
0
void dij(int x,int y)
{
    if(!mat[x][y]||vis[x][y])return;
    vis[x][y]=1;
    dij(x-1,y-1);dij(x-1,y);dij(x-1,y+1);
    dij(x,y-1);             dij(x,y+1);
    dij(x+1,y-1);dij(x+1,y);dij(x+1,y+1);
}
Пример #2
0
void c_osi2_switch::send_tick() {
	for (auto & pcg : m_outbox){
		auto const & dest = pcg.m_dst;

		c_osi2_switch &dest_switch = m_world.find_object_by_uuid_as_switch(dest);
		
		c_dijkstry01 dij(*this,dest_switch,m_world);
		std::list<t_osi3_uuid> route_list = dij.get_last_routeList();
		if(route_list.size() >= 2) {
			c_osi2_nic & nic = dij.get_next_nic();
			m_draw_outbox.emplace(std::make_pair<t_osi3_uuid,double>(dij.get_next_uuid(),0.));
			nic.add_to_nic_outbox(std::move( pcg )); // move this packet there
		} else {
			_dbg1("Packet hit detination: ok");
		}

//		size_t nic_ix = m_world.route_next_hop_nic_ix( dynamic_cast<c_object&>(*this), dest_switch );
//		_dbg1("*this sw: " << get_uuid_any() <<"\ndest sw:" << dest_switch.get_uuid_any());

//		if (size_t_is_ok(nic_ix)) {
//			c_osi2_nic & nic = * m_nic.at(nic_ix); // send through this nic
//			std::cout << "old nic adrr: " << &nic << std::endl;
//			nic4.add_to_nic_outbox(std::move( pcg )); // move this packet there
//		} else if(get_uuid_any() == dest_switch.get_uuid_any()) {

//		}
//		else {
//			_warn("Can not find the route between from me " << (*this) << " to dest_switch=" << dest_switch);
//			_dbg1("nic_ix = " << nic_ix);
//		}
	}
	m_outbox.clear();
}
Пример #3
0
int main(void)
{
    int n;
    scanf("%d",&n);
    char s[MAX];
    for(int i=0;i<n;i++){
        scanf("%s",s);
        for (int j=0;j<n;j++){
            mat[i+1][j+1]=s[j]-'0';
        }
    }
    for(int i=0;i<n+2;i++){
        for (int j=0;j<n+2;j++){
            printf("%d ",mat[i][j]);}
        printf("\n");
    }
    int count =0;
    for(int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            if(mat[i][j]&&!vis[i][j]){count++;dij(i,j);}
        }
    }
    printf("the number is:%d",count);
    return 0;
}
Пример #4
0
TEST_F(DynSSSPGTest, testDynamicDijkstra) {
 /* Graph:
    0    3   6
     \  / \ /
      2 -- 5
     /  \ / \
    1    4   7

    Edges in the upper row have weight 3,
    the edge in the middle row has weight 1.5,
    edges in the lower row have weight 2.
 */
	count n = 8;
	Graph G(n, true);

	G.addEdge(0, 2, 3);
	G.addEdge(1, 2, 2);
	G.addEdge(2, 3, 3);
	G.addEdge(2, 4, 2);
	G.addEdge(2, 5, 1.5);
	G.addEdge(3, 5, 3);
	G.addEdge(4, 5, 2);
	G.addEdge(5, 6, 3);
	G.addEdge(5, 7, 2);

	Dijkstra dij(G, 0);
	dij.run();
	DynDijkstra ddij(G, 0);
	ddij.run();
	std::vector<GraphEvent> batch(3);
	batch[0].type = GraphEvent::EDGE_ADDITION;
	batch[0].u = 0;
	batch[0].v = 4;
	batch[0].w = 1.0;
	batch[1].type = GraphEvent::EDGE_ADDITION;
	batch[1].u = 1;
	batch[1].v = 4;
	batch[1].w = 1.0;
	batch[2].type = GraphEvent::EDGE_ADDITION;
	batch[2].u = 6;
	batch[2].v = 7;
	batch[2].w = 3.0;
	for (GraphEvent edge : batch) {
		G.addEdge(edge.u, edge.v, edge.w);
	}
	ddij.update(batch);
	dij.run();
	G.forNodes([&] (node i) {
		EXPECT_EQ(dij.distance(i), ddij.distance(i));
		EXPECT_EQ(dij.numberOfPaths(i), ddij.numberOfPaths(i));
	});

}
Пример #5
0
TEST_F(DynSSSPGTest, testDynamicDijkstraBatches) {
	METISGraphReader reader;
	std::default_random_engine random_generator;
  	std::normal_distribution<double> distribution(100,10);
	DorogovtsevMendesGenerator generator(100);
	Graph G1 = generator.generate();
	Graph G(G1, true, false);
	DEBUG("Generated graph of dimension ", G.upperNodeIdBound());
	// add random normal weights to G

	G.forNodes([&] (node source) {
		DynDijkstra dyn_dij(G, source, true);
		Dijkstra dij(G, source);
		dyn_dij.run();
		dij.run();
		DEBUG("Before the edge insertion: ");
		GraphEvent ev;
		count batchSize = 8;
		count nBatches = 1, i = 0;
		for (count j=0; j<nBatches; j++) {
			std::vector<GraphEvent> batch;
			i = 0;
			while (i < batchSize) {
				DEBUG("Sampling a new edge");
				node v1 = Sampling::randomNode(G);
				node v2 = Sampling::randomNode(G);
				if (v1 != v2 && !G.hasEdge(v1, v2)) {
					i++;
					double number = distribution(random_generator);
					G.addEdge(v1, v2, number);
					batch.push_back(GraphEvent(GraphEvent::EDGE_ADDITION, v1, v2, number));
				}
			}
			DEBUG("batch size: ", batch.size());
			DEBUG("Updating with dynamic dijkstra");
			dyn_dij.update(batch);
			DEBUG("Running from scratch with dijkstra");
			dij.run();
			G.forNodes([&] (node i) {
			//	std::cout<<"Node "<<i<<":"<<std::endl;
			//	std::cout<<"Actual distance: "<<dij.distance(i)<<", computed distance: "<<ddij.distance(i)<<std::endl;
			//	std::cout<<"Actual number of paths: "<<dij.numberOfPaths(i)<<", computed one: "<<ddij.numberOfPaths(i)<<std::endl;
				EXPECT_EQ(dyn_dij.distance(i), dij.distance(i));
				EXPECT_EQ(dyn_dij.numberOfPaths(i), dij.numberOfPaths(i));
				if (i != source)
					assert(dyn_dij.distance(i) != 0);
			//	EXPECT_EQ(dyn_dij.getPredecessors(i).size(), dij.getPredecessors(i).size());
			});
		}
	});
}
Пример #6
0
size_t calculate_reach (const vis_graph& g, my_graph::vertex_id vid, reach_map &reaches_rec)
{
    my_graph::old_dijkstra<vis_graph> dij (g, get_weight);
    //reach_map reaches_rec, reaches_stack;
    
    struct stack_record
    {
        my_graph::vertex_id id;
        bool finished;
    };

    DWORD dij_time=0, rec_time=0, stack_time = 0;

    time_calc dij_tc (dij_time);
    for (dij.init(vid); !dij.done(); dij.iterate()) {}
    dij_tc.end();

/*
    for (vis_graph::v_const_iterator it = g.v_begin(); it != g.v_end(); ++it)
    {
        reaches_stack[it->second.get_id()] = 0;
        reaches_rec[it->second.get_id()] = 0;
    }
*/


    
    time_calc rec_tc (rec_time);
    calculate_reach_recursive (g, vid, dij, reaches_rec);
    rec_tc.end();

/*
    time_calc stack_tc (stack_time);
    calculate_reach_stack (g, vid, dij, reaches_stack);
    stack_tc.end();
*/

/*
    std::cout << dij_time << " ms dijkstra\n";
    std::cout << rec_time << " ms rec\n";
    std::cout << stack_time << " ms stack\n";
    std::cout << ::g_checked_edges << " edges checked\n";
    std::cout << ::g_check_time << " ms check time\n";
    std::cout << "\n";
*/
    

    return 0;

}
Пример #7
0
int main()
{
	int a, b, c, m;
	while(scanf("%d %d", &n, &m) != EOF)
	{
		memset(first, -1, sizeof(first));
		tot = 0;
		while(m--)
		{
			scanf("%d %d %d", &a, &b, &c);
			add(a, b, c);
		}
		dij();
	}
	return 0;
}
Пример #8
0
int main()
{
    int i,j,k;
    char s[100];
    int len;
    while(scanf("%d",&N)!=EOF)
    {
        if(N==0) break;
        for(k=0; k<N; k++)
        {
            scanf("%d%s",&dic[k].T,s);
            len=strlen(s);
            for(i=0,j=len-1; i<4; i++, j--)
            {
                dic[k].front[i]=s[i];
                dic[k].back[3-i]=s[j];
            }
            dic[k].front[4]=dic[k].back[4]='\0';
        }
        for(i=0; i<N; i++)
        {
            for(j=0; j<N; j++)
            {
                map[i][j]=INF;
                if(i==j) continue;
                if(strcmp(dic[i].back, dic[j].front)==0)
                    map[i][j]=dic[i].T;
            }
        }
        for(i=0; i<N; i++)
        {
            dist[i]=map[0][i];
            vis[i]=0;
        }
        vis[0]=1;
        dist[0]=0;
        dij();
        if(dist[N-1]==INF) printf("-1\n");
        else printf("%d\n",dist[N-1]);
    }
    return 0;
}
Пример #9
0
void main()
{
 int n,v,i,j,cost[10][10],dist[10];
 printf("\n Enter the number of nodes:");
 scanf("%d",&n);
 printf("\n Enter the cost matrix:\n");
 for(i=1;i<=n;i++)
	  for(j=1;j<=n;j++)
	  {
		scanf("%d",&cost[i][j]);
		if(cost[i][j]==0)
		    cost[i][j]=infinity;
	  }
	  printf("\n Enter the source matrix:");
	  scanf("%d",&v);
	  dij(n,v,cost,dist);
	  printf("\n Shortest path:\n");
	  for(i=1;i<=n;i++)
	     if(i!=v)
		printf("%d->%d,cost=%d\n",v,i,dist[i]);
} // End of program
Пример #10
0
//你要完成的功能总入口
void search_route(char *topo[5000], int edge_num, char *demand)
{
	Graph graph(topo, edge_num, demand);
	int s = graph.getS();
	int t = graph.getT();
	
	//graph.toString();
	Dijstra dij(graph);
	//dij.dijstraSK(graph, s, t);
	//dij.dijstraSK66(graph, s, t);
	dij.dijstraTwo(graph, s, t);
	//dij.toString(s, t);
	//KM km(graph);
	//km.doKM(dij);
	//km.toString();
	//two.toString();
	
	//two.toString();
	//dij.toString();
	//dij.toString(s, t);
	//Sk66 sk66(graph);
	//sk66.doSk66(dij, s, t);
	//sk66.toString(s);
	/*Link lhs(0, 3, 3, vector<int>{0, 2, 3}, vector<int>{1, 6});
	Link rhs(3, 1, 1, vector<int>{3, 1}, vector<int>{4});
	Link res = lhs + rhs;
	vector<int> link = res.getID();
	for (auto w : link)
	{
		cout << w << " ";
	}
	cout << res.getCost() << endl;
	*/
	
	
	//unsigned short result[] = { 2, 6, 3 };//示例中的一个解

	//for (int i = 0; i < 3; i++)
	//	record_result(result[i]);
}
Пример #11
0
double dijkstra(double dis[][151],int nc[],int n,char jz[][151],long x[],long y[])
{
int i,j,k,a,b,s=0;
double t,m1,min=1000000000,path[151];
for(k=0;k<n;k++)
{
   for(i=0;i<n;i++)
    for(j=0;j<n;j++)                
     if(dis[i][j]>dis[i][k]+dis[k][j])
      dis[i][j]=dis[i][k]+dis[k][j];
} 
for(i=0;i<n;i++)
   path[i]=0;
for(i=0;i<n;i++)
   for(j=0;j<n;j++)
    if(nc[i]==nc[j]&&i!=j&&path[i]<dis[i][j])
    {     
     path[i]=dis[i][j];
    }
    for(i=0;i<n;i++) 
     for(j=0;j<n;j++)
      if(nc[j]!=nc[i]&&j==nc[j]&&i==nc[i])
       {
        m1=0;
        for(k=0;k<n;k++)
         if(nc[k]==nc[i])
          {
           t=dij(dis,nc,k,j,x,y,n,path);
           if(t>m1)
            m1=t;
          }
        if(min>m1)
         min=m1;
       } 
for(i=0;i<n;i++)
   if(path[i]>min)
    min=path[i];              
return min;   
}
Пример #12
0
TEST_F(DynSSSPGTest, testDynamicDijkstraGeneratedGraph) {
	METISGraphReader reader;
	DorogovtsevMendesGenerator generator(1000);
	Graph G1 = generator.generate();
	Graph G(G1, true, false);
	DEBUG("Generated graph of dimension ", G.upperNodeIdBound());
	DynDijkstra dyn_dij(G, 0);
	Dijkstra dij(G, 0);
	dyn_dij.run();
	dij.run();
	DEBUG("Before the edge insertion: ");
	GraphEvent ev;
	count nInsertions = 10, i = 0;
	while (i < nInsertions) {
		DEBUG("Sampling a new edge");
		node v1 = Sampling::randomNode(G);
		node v2 = Sampling::randomNode(G);
		if (v1 != v2 && !G.hasEdge(v1, v2)) {
			i++;
			DEBUG("Adding edge number ", i);
			G.addEdge(v1, v2);
			std::vector<GraphEvent> batch;
			batch.push_back(GraphEvent(GraphEvent::EDGE_ADDITION, v1, v2, 1.0));
			DEBUG("Running update with dynamic dijkstra");
			dyn_dij.update(batch);
			DEBUG("Running from scratch with dijkstra");
			dij.run();
			G.forNodes([&] (node i) {
			//	std::cout<<"Node "<<i<<":"<<std::endl;
			//	std::cout<<"Actual distance: "<<dij.distance(i)<<", computed distance: "<<ddij.distance(i)<<std::endl;
			//	std::cout<<"Actual number of paths: "<<dij.numberOfPaths(i)<<", computed one: "<<ddij.numberOfPaths(i)<<std::endl;
				EXPECT_EQ(dyn_dij.distance(i), dij.distance(i));
				EXPECT_EQ(dyn_dij.numberOfPaths(i), dij.numberOfPaths(i));
			});
		}
	}
}
main()
{

    FILE *fp;
    fp=fopen("graph.txt","r+");

    fscanf(fp,"%d",&n);
    printf("\n\n\t\tThe adjency matrix is : \n\t\t\t");
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            fscanf(fp,"%d",&a[i][j]);
            printf("%d ",a[i][j]);
        }
        printf("\n\t\t\t");
    }

    printf("\n\n\t\tEnter Source Vertex : ");
    scanf("%d",&s);

    for(i=0; i<n; i++)
        if(i==s-1)
            d[i]=0;
        else
            d[i]=infinity;
    dij();

    for(i=0; i<n; i++)
    {
        if(i!=s-1)
            printf("\n\n\t\tShortest Distance from %d to %d =%d\n",s,i+1,d[i]);


    }
    getch();
}
Пример #14
0
int main() {
	int n,v,i,j,cost[10][10],dist[10];

	setbuf(stdout, NULL);

	printf("\n Enter the number of nodes:");
	scanf("%d",&n);
	printf("\n Enter the cost matrix:\n");
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++) {
			scanf("%d",&cost[i][j]);
			if(cost[i][j]==0)
				cost[i][j]=infinity;
		}
	printf("\n Enter the source :");
	scanf("%d",&v);
	dij(n,v,cost,dist);
	printf("\n Shortest path:\n");
	for(i=1;i<=n;i++)
		if(i!=v)
			printf("%d->%d,cost=%d\n",v,i,dist[i]);

	return 0;
}
Пример #15
0
 int main(int argc, char argv[]){
     
     /*Read input*/
      scanf("%d",&nodes);
      scanf("%d",&edges);
      int i,j,k,l,A,B,C,L;
      int topo8esies[nodes];
      
      /*pinakas kombwn/kostous*/
      matrix = malloc(nodes*sizeof(int*));
      for(i=0;i<nodes;i++){
      matrix[i] = malloc(nodes*sizeof(int));
      }
      
      /*apostaseis*/
      distA= malloc(nodes*sizeof(int));
      distB= malloc(nodes*sizeof(int));      
      distC= malloc(nodes*sizeof(int));
      expDistA=malloc(nodes*sizeof(komvos));
       
      for(k=0;k<nodes;k++)
       for(l=0;l<nodes;l++)
          matrix[k][l]=infinity;
      
      for(k=0;k<edges;k++){
      scanf("%d",&i);
      scanf("%d",&j);
      scanf("%d",&matrix[i-1][j-1]);
      matrix[j-1][i-1] = matrix[i-1][j-1];
      }
      
      scanf("%d",&A);
      scanf("%d",&B);
      scanf("%d",&C);
      scanf("%d",&L);
      
     
     //topo8esies pros erwthsh
      for(k=0;k<L;k++)
      scanf("%d",&topo8esies[k]);
      
      dij(nodes,A,matrix,distA,expDistA);
      dijNodes(nodes,B,matrix,distB);
      dijNodes(nodes,C,matrix,distC);
     
      free(matrix);
      free(distA);
  
       
      //fill expandedDistA with the appropriate info about nodes
	  for(k=0;k<nodes;k++){
			expDistA[k].familyNodeB= distB[k]; 
			expDistA[k].familyNodeC= distC[k];
	  }
      
      quicksort(expDistA,0,(nodes-1));
      
    /* for(i=0;i<nodes;i++){
			printf("Id %d cost:%d\tId %d cost:%d\tId %d cost:%d\t\n",expDistA[i].node_id,expDistA[i].node_value,i+1,distB[i],i+1,distC[i]);
		}
		printf("\n");
		
      for(i=0;i<nodes;i++){
			printf("Id %d cost:%d, childNodes:B(%d) C(%d)\n",expDistA[i].node_id,expDistA[i].node_value,expDistA[i].familyNodeB,expDistA[i].familyNodeC);
		} 
    */  
      /*Loop getting an answer from all locations*/
      for(i=0;i<L;i++){
		int	found=0;
		     k=0;
			
			/*find the element of expDistA that corresponds to each location*/
			do{
				if(topo8esies[i]==expDistA[k].node_id){
				 found=1;
				 }
				  k++;
			}while(!found);
			--k;
		    
		    int m;
			int lost=0;
			l,j=0;
			
	 
		/*find if there is a purely better nod*/
			while (j<k && lost ==0) {
			      if (expDistA[j].familyNodeB>=expDistA[k].familyNodeB) j++;
			      else if ( expDistA[j].familyNodeC>= expDistA[k].familyNodeC) j++;
			      else lost =1 ;}
			      
	      if(lost==0)printf("True\n");
		  else printf("False\n");	
      }
						   
	
      
      }