コード例 #1
0
ファイル: p1143_std.cpp プロジェクト: alxsoares/OI
int Max_Flow()
{
   int flow=0,left;
   while(Bfs())
      while(left=Dinic(S,inf))flow+=left;
   return flow;
}
コード例 #2
0
ファイル: a.cpp プロジェクト: iamslash/learn_to_code
int main() {
  int T;
  scanf("%d", &T);

  for (int t = 0; t < T; ++t) {
    scanf("%d", &N);
    int begin = 0;
    int end = (1 << (N * 2)) - 1;
    for (int i = 0; i < 4; ++i) {
      int O;
      scanf("%d", &O);
      for (int j = 0; j < O; ++j) {
        int P;
        scanf("%d", &P);
        begin = Set(begin, P-1, i);
      }
    }
    // for (int i = 0; i < N; ++i) {
    //   printf("%d ", Get(begin, i));
    // }
    // printf("\n");
    // for (int i = 0; i < N; ++i) {
    //   printf("%d ", Get(end, i));
    // }
    // printf("\n");
    printf("%d\n", Bfs(N, begin, end));
  }
  //
  return 0;
}
コード例 #3
0
 int Maxflow(int s, int t){
     int ret = 0, i, j;
     Bfs(t);
     for (i = 0; i < n; ++i) pre[i] = NULL, arc[i] = adj[i];
     for (i = 0; i < e; ++i) edge[i].flow = edge[i].cap;
     i = s;
     while (d[s] < n){
         while (arc[i] && (d[i] != d[arc[i]->v]+1 || !arc[i]->flow)) arc[i] = arc[i]->next;
         if (arc[i]){
             j = arc[i]->v;
             pre[j] = arc[i];
             i = j;
             if (i == t){
                 int update = INF;
                 for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v])
                     if (update > p->flow) update = p->flow;
                 ret += update;
                 for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v])
                     p->flow -= update, p->re->flow += update;
                 i = s;
             }
         }
         else{
             int min = n-1;
             for (Edge *p = adj[i]; p != NULL; p = p->next)
                 if (p->flow && min > d[p->v]) min = d[p->v];
             if (--numb[d[i]] == 0) return ret;
             d[i] = min+1;
             ++numb[d[i]];
             arc[i] = adj[i];
             if (i != s) i = pre[i]->re->v;
         }
     }
     return ret;
 }
コード例 #4
0
ファイル: bfsl.c プロジェクト: gaurav594/programs
/* Input Format: Graph is directed and unweighted. First two integers must be number of vertces and edges 
   which must be followed by pairs of vertices which has an edge between them. */
int main()
{
        int graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVertices]={0};
        int vertices,edges,iter;
        /* vertices represent number of vertices and edges represent number of edges in the graph. */
        scanf("%d%d",&vertices,&edges);
        int vertex1,vertex2;
        for(iter=0;iter<edges;iter++)
        {
                scanf("%d%d",&vertex1,&vertex2);
                assert(vertex1>=0 && vertex1<vertices);
                assert(vertex2>=0 && vertex2<vertices);
                graph[vertex1][size[vertex1]++] = vertex2;
        }
        int presentVertex;
        for(presentVertex=0;presentVertex<vertices;presentVertex++)
        {
                if(!visited[presentVertex])
                {
                        Bfs(graph,size,presentVertex,visited);
                }
        }
        return 0;



}
コード例 #5
0
ファイル: 2345.cpp プロジェクト: AiHaibara/acm-icpc
	void solve(void) {
		Input();
		Cut();
		Bfs(vs[1][0], vs[1][1]);
		Work(vs[0][0], vs[0][1]);
		int t1 = tim[vt[0]][vt[1]];
		puts(t1?"YES":"NO");
	}
コード例 #6
0
ファイル: 1128.c プロジェクト: Todev3/URI-CODs
int main(void){
	int Ver, Are, Cont, X, Y, Dir;
	
	while( scanf("%d %d", &Are, &Ver) && !(Ver == 0 && Are == 0) ){
		
		memset(Arestas, FALSE, sizeof(Arestas));
		memset(ArestasEsp, FALSE, sizeof(ArestasEsp));
		
		for(Cont=0; Cont < Ver; Cont++){
			
			scanf("%d %d %d", &X, &Y, &Dir);
			
			if( Dir == 2 ){
				
				Arestas[X - 1][Y - 1] = TRUE;
				Arestas[Y - 1][X - 1] = TRUE;
				
				ArestasEsp[X - 1][Y - 1] = TRUE;
				ArestasEsp[Y - 1][X - 1] = TRUE;
				
			}
			else{
				
				Arestas[X - 1][Y - 1] = TRUE;
				ArestasEsp[Y - 1][X - 1] = TRUE;
				
			}
		}
		
		memset(Vertices, FALSE, sizeof(Vertices));
		
		if( Bfs( Arestas, Are) == 1 ){
			memset(Vertices, FALSE, sizeof(Vertices));
			if( Bfs( ArestasEsp, Are) == 1 ){
				printf("1\n");
			}
			else
				printf("0\n");
		}
		else
			printf("0\n");
			
	}
	
	return 0;
}
コード例 #7
0
int main() {
  adj.resize(N);
  adj[0].push_back(1);
  adj[1].push_back(2);
  adj[1].push_back(3);
  adj[2].push_back(3);

  std::vector<int> r = Bfs(0);

  PrintVInt(r);

  return 0;
}
コード例 #8
0
ファイル: a.cpp プロジェクト: Roper/403-2016
int main(){
	freopen("a.in", "r", stdin), freopen("a.out", "w", stdout);
	
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= m; i ++){
		int x, y, z;
		scanf("%d %d %d", &x, &y, &z);
		Link(y, x, z);
		Deg0[x] ++, Deg1[x] ++;
	}
	
	Bfs();
	printf("%.2f", f[1]);
	
	return 0;
}
コード例 #9
0
ファイル: 2791.cpp プロジェクト: AiHaibara/acm-icpc
	inline void Init(void) {
		int tail = 0, head = 1;
		for (int i = 1; i <= n; i++) if (!inp[i]) d[++tail] = i;
		while (head <= tail) {
			int now = d[head++], f = father[now];
			if (!--inp[f]) d[++tail] = f;
		}
		for (int i = 1; i <= n; i++) if (inp[i] > 0) {
			int tmp = i, &l = len[++ncir];
			do {
				inp[tmp] = -1;
				Bfs(tmp);
				tmp = father[tmp];
				pos[tmp] = ++l;
			} while (tmp != i);
		}
	}
コード例 #10
0
ファイル: s819.c プロジェクト: Todev3/URI-CODs
int main(void){
	int Vert, Arest, CidAtu, GrauMax, X, Y, Lin, Col, Vez = 0;
	
	while( scanf("%d %d %d %d", &Vert, &Arest, &CidAtu, &GrauMax) && !( Vert == 0 && Arest == 0 && CidAtu == 0 && GrauMax == 0)){
		
		Vez++;
		
		for(Lin = 0; Lin < Vert; Lin++)
			for(Col = 0; Col < Vert; Col++)
				Arestas[Lin][Col] = FALSE;
			
		for(Lin = 0; Lin < Arest; Lin++){
			
			scanf("%d %d", &X, &Y);
			
			Arestas[X - 1][Y - 1] = TRUE;
			Arestas[Y - 1][X - 1] = TRUE;
			
		}
		
		memset(Vertices, FALSE, sizeof(char) * (Vert + 1));
		
		Bfs(CidAtu - 1, Vert, GrauMax);
		
		printf("Teste %d\n", Vez);
		
		X = FALSE;
		
		for(Lin = 0; Lin < Vert; Lin++){
			if( Vertices[Lin] == TRUE && (Lin != CidAtu - 1) ){
				if(X != FALSE)
					printf(" ");
				printf("%d", Lin+1);
				X = TRUE;
			}
		}
		
		printf("\n\n");
		
	}
	
	return 0;
}
コード例 #11
0
void findDiameter(int vertexID){
	int vertexCluster = vertices[vertexID]->ClusterBelonging;
	int temp=0,ans=0;
	int *visited=(int *) calloc(vertexNum,sizeof(int));/*an array to check if
	the vertex was aleady checked or not, 0= wasn't checked, 1=checked*/
	if(clusters[vertexCluster-1]->diameter==(-1)){
		return;
	}
	if(visited==NULL){
		perror("Problem in function 'findDiameter':\n");
		allFail(1);
	}
	ans=Bfs(vertexID,visited,vertexCluster);
	if (ans>clusters[vertexCluster-1]->diameter || ans==-1){ /*if the diameter bfs found a bigger distance in the cluster 
									   this is the new cluster diameter*/
		(clusters[vertexCluster-1]->diameter)=ans;
	}
	free(visited);
}
コード例 #12
0
ファイル: tou.cpp プロジェクト: AiHaibara/acm-icpc
	inline void BCC(void) {
		low = new int[n + 2];
		for (int i = k + 1; i <= n; i++) if (!dfs[i]) 
			Dfs(i);
		memset(father, 0, sizeof father);
		memset(low, 0, sizeof low);
		memset(dfs, 0, sizeof dfs);
		for (int i = 1; i <= n; i++) if (!belong[i]) {
			if (i <= k) belong[i] = ++color;
			else Bfs(i);
		}
		delete[] low;
		vis = new bool[n + 2]; memset(vis, 0, sizeof(bool) * (n + 1));
		for (int i = 1; i <= color; i++) if (!vis[i]) {
			Bfs2(i);
		}
		printf("%d\n", ans);
		memset(tag, 0, sizeof tag); memset(vis, 0, sizeof(bool) * (n + 1));
		for (int i = 1; i <= color; i++) if (!vis[i]) {
			Bfs2(i, 1);
		}
	}
コード例 #13
0
ファイル: 1412.cpp プロジェクト: AiHaibara/acm-icpc
	long Dinic(void) {
		long t, r = 0;
		while (Bfs()) 
			while (t = Extend(vs, inf)) r += t;
		return r;
	}
コード例 #14
0
ファイル: 1977.cpp プロジェクト: AiHaibara/acm-icpc
	void solve(void) {
		Input();
		Prepare();
		Bfs();
		Work();
	}
コード例 #15
0
ファイル: bfs_omp.c プロジェクト: zichelle/project
int main()
{
	void Bfs(int** graph, int *size, int presentVertex,int *visited);
	struct timespec diff(struct timespec start, struct timespec end);
  	struct timespec time1, time2, fintime;
	
	srand(time(NULL));
	//int *visited;
	if ((visited=(int*)calloc(VERTICES, sizeof(int)))==NULL)
	{
		printf("Could not allocate memory for visited array\n");
		exit(1);
	}
	int size[VERTICES]={0};
	
	//Graph is 2 dimensional array of pointers
	int **graph;

	//printf("Total vertices = %d, Max edges = %d\n", VERTICES, MAX_EDGES);

	//Generate dynamic array
	if ((graph=(int**)malloc(sizeof(int *) * VERTICES))==NULL)
	{
		printf("Could not allocate memory for graph\n");
		exit(1);
	}


	//Generate space for edges
	int vertexCounter = 0;
	//Generate space for vertices
	int edgeCounter = 0;

	//Link the graph
	for(vertexCounter=0; vertexCounter<VERTICES; vertexCounter++)
	{
		size[vertexCounter] = rand()%MAX_EDGES;
		
		if((graph[vertexCounter]=(int*)malloc(sizeof(int)*size[vertexCounter]))==NULL)
		{
			printf("Could not allocate memory for edges\n");
			exit(1);
		}
		
		//printf("%d:\t", vertexCounter);
		
		for(edgeCounter=0; edgeCounter<size[vertexCounter]; edgeCounter++)
		{
				int linkedVertex = rand()%VERTICES;
				graph[vertexCounter][edgeCounter]=linkedVertex;
				//printf("%d, ", linkedVertex);
		}

		//printf("\n");

	}

	
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

	int presentVertex;
    for(presentVertex=0;presentVertex<VERTICES;presentVertex++)
    {
    	if(!visited[presentVertex])
    	{   		
			Bfs(graph,size,presentVertex,visited); 
        }
    }


    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);


	fintime = diff(time1,time2);

    printf("%f, \n", (double)(CPG)*(double)(GIG*fintime.tv_sec
				+fintime.tv_nsec));
	return 0;
}
コード例 #16
0
ファイル: 1532.cpp プロジェクト: AiHaibara/acm-icpc
	int Dinic(void) {
		int r = 0, t;
		while (Bfs()) while (t = Extend(vs, inf)) r += t;
		return r;
	}