Example #1
0
Edge Addedge(int from, int to, int capacity_f, int capacity_b, Edge edge_arr) {
  Edge edge_f, edge_b;

  edge_f = edge_arr;
  edge_b = edge_f + 1;

  if (from == to)
    return edge_arr;

  if (from < to) {  // from is small
    edgecnt += 2;
    // add edge to from.adj_list
    edge_f->capacity = capacity_f;
    edge_f->endpoint = g_node + to;
    edge_f->mateedge = edge_b;
    edge_f->nextedge = g_node[from].adj_list;

    g_node[from].adj_list = edge_f;

    // add edge to to.adj_list
    edge_b->capacity = capacity_b;
    edge_b->endpoint = g_node + from;
    edge_b->mateedge = edge_f;
    edge_b->nextedge = g_node[to].adj_list;

    g_node[to].adj_list = edge_b;

  } else {  // to is small

    // search to->adj_list
    edge_f = search_adj(g_node + from, g_node[to].adj_list);
    // edge_f =  search_adj(g_node+to, g_node[from].adj_list );

    if (edge_f != NULL) {

      // from->to does exist. update its capacity
      edge_f->mateedge->capacity += capacity_f;
      return edge_arr;

    } else {
      return Addedge(to, from, capacity_b, capacity_f, edge_arr);
    }
  }

  return edge_arr + 2;
}
Example #2
0
int main(){
  int n,m,i,p1,p2;
  while(~scanf("%d%d",&n,&m)){
    top=0;
    Mem0(head),Mem0(vis),Mem0(out_de),Mem0(in_de);
    for(i=1;i<=n;++i)scanf("%d",v+i);
    while(m--){
      scanf("%d%d",&p1,&p2);
      Addedge(p2,p1);
      out_de[p1]++;
      in_de[p2]++;
    }
    int max_v=-INF;
    for(i=1;i<=n;++i)
      if(!out_de[i])
        max_v=Max(max_v,dfs(i));
    printf("%d\n",max_v);
  }
}
Example #3
0
int main() {
    int N;
    while (scanf("%d", &N) == 1) {
        for (int i = 0; i < N; i++)
            scanf("%s", s[i]);
        
        init(N, 0.85f);
        for (int i = 0; i < N; i++) {
        	outdeg[i] = 0;
            for (int j = 0; j < N; j++) {
                if (s[i][j] == '1')
                    Addedge(j, i), outdeg[i]++;
            }
        }
        
        compute();
        
        for (int i = 0; i < N; i++)
            printf("%.2lf%c", r[i], i == N - 1 ? '\n' : ' ');
    }
    return 0;
}
Example #4
0
int main(){
  int n,i,j,k,len;
  while(~scanf("%d",&n)){
    Mem0(head),Mem0(flag),top=0;
    for(i=0;i<n;++i){
      Mem0(num);
      scanf("%s",s);
      len=strlen(s);
      for(j=k=0;j<len;++j)
        if(s[j]==')')k++;
        else if(s[j]>='A' && s[j]<='Z')
          num[k]=num[k]*seed+s[j];
      Addedge(BKRD_Hash(num,k));
      flag[k]=1;
    }
    Mem0(num);
    scanf("%s",s);
    len=strlen(s);
    for(i=j=0;i<len;++i)
      if(s[i]==')')j++;
      else if(s[i]>='A' && s[i]<='Z')
        num[j]=num[j]*seed+s[i];
    len=j;
    int cnt=0;
    for(i=1;i<=Min(30,len);++i){
      if(!flag[i])continue;
      ll tmp=1,hash=BKRD_Hash(num,i);
      cnt+=find(hash);
      for(j=0;j<i;++j)tmp*=seed;
      for(j=i;j<len;++j){
        hash=hash*seed+num[j]-num[j-i]*tmp;
        cnt+=find(hash);
      }
    }
    printf("%d\n",cnt);
  }
}
Example #5
0
int main(long argc, char** argv) {
  int i, j;
  long t;
  FILE* fp;

  double duration;
  clock_t time;
  struct timeval tpstart, tpend;
  long iTimeInterval;

  int Test = 0;

  if (argc != 4) {
    printf("usage: %s .max_graph_file num_threads GR frequency\n", argv[0]);
    exit(-1);
  }
  num_threads = atoi(argv[2]);
  fprintf(stderr, "Threads: \t\t%d\n", num_threads);

  // initialize the graph, will read the graph from a file.
  fp = fopen(argv[1], "r");
  if (!fp) {
    printf("Cannot open %s\n", argv[1]);
    exit(-1);
  }

  char tag;
  char str[5];
  int from, to, capacity;

  fscanf(fp, "%c", &tag);
  while (tag != 'p') {
    while (getc(fp) != '\n')
      ;
    fscanf(fp, "%c", &tag);
  }
  fscanf(fp, "%s %d %d", str, &g_num_nodes, &g_num_edges);

  gr_threshold = (int)(atof(argv[3]) * g_num_nodes);
  fprintf(stderr, "GR Freq: \t\t%2.2f\n", atof(argv[3]));
  fprintf(stderr, "%d nodes, %d edges\n\n", g_num_nodes, g_num_edges);

  // initialize graph
  init_graph();

  // read and sort edges
  Edge edge = (Edge)malloc(2 * g_num_edges * sizeof(struct edge_entry));
  if (edge == NULL)
    exit(-1);
  edge_sym = edge;
  edgecnt = 0;
  for (i = 0; i < g_num_edges;) {
    if (getc(fp) == 'a') {
      fscanf(fp, "%d	%d	%d/n", &from, &to, &capacity);
      edge = Addedge(from - 1, to - 1, capacity, 0, edge);
      i++;
    }
  }
  assert(i == g_num_edges);

  SortEdges();
  fprintf(stderr, "Edge sorted!\n");

  // initialize thread parameters
  init_threads(num_threads);
  pthread_t threads[num_threads];

  // initial barrier and lock
  pthread_mutex_init(&(gr_mutex), NULL);
  node_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t) * g_num_nodes);
  if (node_mutex == NULL)
    exit(-1);
  thread_mutex =
      (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t) * num_threads);
  if (thread_mutex == NULL)
    exit(-1);
  for (i = 0; i < num_threads; i++) {
    pthread_mutex_init(&(thread_mutex[i]), NULL);
  }
  for (i = 0; i < g_num_nodes; i++) {
    pthread_mutex_init(&(node_mutex[i]), NULL);
  }

  pthread_barrier_init(&start_barrier, NULL, num_threads);

  // now the f c e h arrays have been initialized. start the threads
  // so that they can work asynchronously.
  time = clock();
  gettimeofday(&tpstart, NULL);
  preflow(num_threads);
  for (t = 0; t < num_threads; t++) {
    pthread_create(&(threads[t]), NULL, scan_nodes, (void*)t);
  }
#ifdef MONITOR
  int num_idle_threads;
  int totalQsize, global_Q_size;
  while (!flow_done()){
    gettimeofday(&tpend, NULL);
    iTimeInterval = 1000000 * (tpend.tv_sec - tpstart.tv_sec);
    iTimeInterval += tpend.tv_usec - tpstart.tv_usec;
    duration = (double)iTimeInterval / 1000000;
    totalQsize = 0;
    num_idle_threads = 0;
    for (j = 0; j < num_threads; j++) {
      totalQsize += threadEnv[j].Qsize;
      if (threadEnv[j].Qsize == 0)
        num_idle_threads++;
    }
    if (duration > 10.0) {
                fprintf(stderr, " totalQsize: %8d globalQsize %4d idle %2d\n",
       totalQsize, global_Q_size, num_idle_threads);
              fflush(stderr);
                  exit(-1);
    }
    // printf("%12d %12d \t\t totalQsize: %8d GR %4d idel %2d\n",
    // g_node[0].excess,g_node[g_num_nodes-1].excess, totalQsize, GRcnt,
    // num_idle_threads);
    // printf("%d\n", totalQsize);
  }
  printf("\n");
#endif

  for (t = 0; t < num_threads; t++) {
    pthread_join(threads[t], NULL);
  }
  gettimeofday(&tpend, NULL);
  time = clock() - time;

  iTimeInterval = 1000000 * (tpend.tv_sec - tpstart.tv_sec);
  iTimeInterval += tpend.tv_usec - tpstart.tv_usec;
  duration = (double)iTimeInterval / 1000000;

  fprintf(stderr, "FLOW: \t\t\t%ld\n", sink->excess);
  fprintf(stderr, "Wall Time: \t\t%5.3f\n", duration);
  fprintf(stderr,
          "CPU Time: \t\t%5.3f\n",
          (double)time / CLOCKS_PER_SEC / num_threads);
  fprintf(stderr, "Push: \t\t\t%ld\n", totalPushes);
  fprintf(stderr, "Push Efcy: \t\t%.1f\n", totalPushes / duration);
  fprintf(stderr, "Lift: \t\t\t%ld\n", totalLifts);
  fprintf(stderr, "Lift Efcy: \t\t%.1f\n", totalLifts / duration);
  fprintf(stderr, "GR: \t\t\t%d\n", GRcnt);
  fprintf(stderr, "HELP: \t\t\t%d\n", HPcnt);
  fflush(stderr);

  check_violation();

  pthread_exit(NULL);
}