Example #1
0
int main()
{
    int u, v, w1, w2;
    //printf("%lld %lld\n", LLMAX, LLMIN);
    scanf("%d", &n);
    for (int i = 1; i < n; ++i)
    {
        scanf("%d %d %d %d", &u, &v, &w1, &w2);
        AddEdge(u, v, w1, w2);
    }
    scanf("%d", &m);
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d", &v);
        vip[v] = true;
    }
    memset(dp, -1, sizeof(dp));
    printf("%lld\n", min(DFS2(1, 0, 0), DFS2(1, 0, 4)));
    /* for (int i = 1; i <= n; ++i)
    {
        for (int j = 0; j <= 4; ++j)
            printf("%4lld%c", dp[i][j], j == 4 ? '\n' : ' ');
    } */
    return 0;
}
Example #2
0
void topological_sort (struct digraph *g, vertex v) {

	/* discovered, as before */

	int discovered[g->n];

	/* make a list of nodes to be output as the topological sort */

	struct list_node *L = NULL;

	/* all zeros */

	memset (discovered, 0, g->n * sizeof (int));

	/* depth first search, pushing nodes onto the list */

	DFS2 (g, v, discovered, &L);

	/* print the list, freeing it at the same time */

	printf ("{ ");
	while (L) {
		struct list_node *p = L->next;
		printf ("%d ", L->x);
		free (L);
		L = p;
	}
	printf ("}\n");
}
void DFSTraverse(MTGraph G)
//先深搜索一邻接表表示的图G;而以邻接矩阵表示G时,算法完全相同.
{   
	int i , count = 1;
    for ( i = 0; i < G.n; i++ )
        visited [i] =FALSE; 		//标志数组初始化
    for ( i = 0; i < G.n; i++ )
        if ( ! visited[i] )
            DFS2( &G, i ); //从顶点i 出发开始搜索search() DFSX(G, i )
}
/* Намиране на един цикъл спрямо намереното покриващо дърво */
void DFS2(unsigned v, unsigned u)
{ unsigned k, i;
  if (v == u) { printCycle(); return; }
  used[v] = 1;
  for (i = 0; i < n; i++)
    if (!used[i] && 2==A[v][i]) {
      cycle[d++] = i;
      DFS2(i, u);
      d--;
    }
}
void DFS2(MTGraph *G, int i)//以vi为出发点对邻接矩阵表示的图G进行深度优先搜索
{ 
    int j;
	static int count=0;
    cout<<G->vexlist[i]; 	//访问定点vi
    visited[i]=TRUE; 	//标记vi已访问
    dfn[i]=count; 		//对vi进行编号
    count ++; 		//下一个顶点的编号
    for( j=0; j<G->n; j++) 	//依次搜索vi的邻接点
        if((G->edge[i][j] == 1) && !visited[j] ) 	//若vj尚未访问
            DFS2( G, j );
}//DFS2
Example #6
0
std::vector<Node> leaders(Graph<Node> &graph){
  std::set<Node> visited;
  std::vector<Node> finished;
  finished.reserve(graph.nb_nodes());
  for(auto it=graph.nodes_crbegin(); it!=graph.nodes_crend(); ++it){
    const auto node = it->first;
    if(visited.find(node) == visited.end()){
      DFS2(graph, node, visited, finished);
    }
  }
  return finished;
}
Example #7
0
std::vector<std::unique_ptr<std::vector<Node> > > 
scc(Graph<Node> &graph, std::vector<Node>& leaders){
  std::set<Node> visited;
  std::vector< std::unique_ptr<std::vector<Node>> > scc;
  for(auto it=leaders.crbegin(); it!=leaders.crend(); ++it){
    const auto node = *it;
    if(visited.find(node) == visited.end()){
      scc.push_back(std::make_unique<std::vector<Node> >());
      DFS2(graph, node, visited, *(scc.back()));
    }
  }
  return scc;
}
int main(void) {
  unsigned i, j, k;
  DFS(0);
  printf("Елементарните(простите) цикли в графа са: \n");
  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++)
      if (1 == A[i][j]) {
        for (k = 0; k < n; k++) used[k] = 0;
        d = 1;
        cycle[0] = i;
        DFS2(i, j);
      }
  return 0;
}
Example #9
0
void Graph::makeGraph() {
 for (iplist<Function>::iterator It = M->getFunctionList().begin(); It != M->getFunctionList().end(); ++It) {
  Function * Fn = It;
  for (iplist<BasicBlock>::iterator I = Fn->getBasicBlockList().begin(); I != Fn->getBasicBlockList().end(); ++I) {
    BBList.push_back(I); 
    for (pred_iterator PI = pred_begin(I); PI != pred_end(I); ++PI){
     Pred_Vec[I].push_back(*PI);
     Succ_Vec[*PI].push_back(I);
    }
  }
 }
 DFS();
 DFS2();
}
Example #10
0
void DFS2(int i)
{
    node *p;

    printf("\n%d",i);
    p=G[i];
    visited[i]=1;
    while(p!=NULL)
    {
       i=p->vertex;

       if(!visited[i])
            DFS2(i);
        p=p->next;
    }
}
int main() {

    int j;

    int start_node = 1;

    readGraph();

    displayMatrixAdjacency(); 

    visited[ start_node ] = 1;  

    DFS2( start_node );

    displaySol2();

    return(0);
};
Example #12
0
void DFS2 (struct digraph *g, vertex v, int discovered[], struct list_node **L) {
	vertex w;

	if (v > g->n) return;
	discovered[v] = 1;
	//for (w=0; w<g->n; w++) {
	for (w=g->n-1; w>=0; w--) {
		if (!discovered[w] && edge_exists (g, v, w))
			DFS2 (g, w, discovered, L);
	}

	/* we are finished with node v; push it onto the list */

	struct list_node *e = malloc (sizeof (struct list_node));
	e->next = *L;
	e->x = v;
	*L = e;
}
void DFS2( node ) {

     int i;  

     sol[++sol[0]] = node;

     visited[ node ] = 1;      

     for(i = 1; i <= num_nodes; i++) {

         if(matrix_of_adjacency[ node ][ i ] == 1 && !visited[ i ]) {

                      visited[i] = 1;

                      DFS2( i ); 
         }

     }           

};
Example #14
0
ll DFS2(int u, int fa, int s)
{
    if (dp[u][s] != -1) return dp[u][s];
    dp[u][0] = dp[u][1] = dp[u][2] = dp[u][3] = dp[u][4] = 0;
    ll t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t1s = 0, t2s = 0, cn = 0;
    ll t41 = 0, t42 = 0, t411 = LLMAX, t412 = 0, t421 = LLMAX, t422 = 0, t41key = -1, t42key = -2;
    for (int e = head[u]; e; e = Edge[e].next)
    {
        int v = Edge[e].v;
        if (v == fa) continue;
        DFS2(v, u, 0);
        if (!vip[v]) continue; else vip[u] |= vip[v];
        t0 = min(t0, dp[v][0] - Edge[e].w1 - dp[v][1]);
        t1 = (Edge[e].w1 << 1) + dp[v][1];
        t2 = min((Edge[e].w1 << 1) + dp[v][1], (Edge[e].w2 << 1) + dp[v][2]);
        t3 = min(t3, dp[v][3] + Edge[e].w1 + Edge[e].w2 - t2);
        t4 = min(t4, dp[v][0] + Edge[e].w1 - t2);
        t4 = min(t4, dp[v][4] + Edge[e].w2 - t2);
        t1s += t1, t2s += t2;
        t41 = Edge[e].w1 + Edge[e].w2 + dp[v][3] - t2;
        t42 = Edge[e].w1 + dp[v][0] - t2;
        if (t41 < t411) { t412 = t411, t411 = t41, t41key = v; }
        else t412 = min(t412, t41);
        if (t42 < t421) { t422 = t421, t421 = t42, t42key = v; }
        else t422 = min(t422, t42);
        ++cn;
    }
    if(cn>1) t4 = t41key == t42key ? min(min(t4, t412 + t421), min(t4, t411 + t422)) : min(t4, t411 + t421);
    dp[u][0] = t1s + t0;
    dp[u][1] = t1s;
    dp[u][2] = t2s;
    dp[u][3] = t2s + t3;
    dp[u][4] = t2s + t4;
    // dp[u][4] = min(dp[u][4], dp[u][3]);
    return dp[u][s];
}