int main()
{
    int re;
    int n, m, l, r, s, t;
    FlowNetwork fn;
    scanf("%d", &re);
    for (int ri = 1; ri <= re; ri++)
    {
        if (ri > 1) printf("\n");
        scanf("%d%d%d", &n, &m, &l);
        for (int i = 0; i < n; ++i)
        {
            scanf("%d", &r);
            if (r == 1) s = i;
            else if (r == l) t = i;
        }
        fn.init(n, s, t);
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &s, &t, &r);
            fn.AddEdge(s - 1, t - 1, r);
        }
        fn.hlpp();
        for (int i = 0; i < m; i++)
        {
            printf("%d\n", fn.edge[RE(i << 1)].c);
        }
    }
    return 0;
}
Ejemplo n.º 2
0
void Importer::_raw(FlowNetwork<> & network,
		    GraphvizAttributesHolder & config,
		    std::istream & in,
		    const std::string & nodesMessage,
		    const std::string & edgesMessage)
{
  std::string label(""), sourceNodeLabel(""), targetNodeLabel("");
  std::map<std::string, GraphTypes::node_id> labelToIdMapper;
  GraphTypes::FlowTypes::Flow minCapacity, maxCapacity, flow;
  int i, j;

  //nodes
  if(nodesMessage != "")
    std::cout << nodesMessage << std::endl;

  if( in.good() )
    in >> label;

  i=0;
  while(label != "fin.")
    {
      network.add_node(i);
      config.attributesOf(i).setLabel(label);
      labelToIdMapper[label] = i;

      if( in.good() )
	in >> label;

      ++i;
    }

  //edges
  if(edgesMessage != "")
    std::cout << edgesMessage << std::endl;

  if( in.good() )
    in >> sourceNodeLabel;

  while(sourceNodeLabel != "fin.")
    {
      in >> targetNodeLabel >> minCapacity >> maxCapacity >> flow;
      i = labelToIdMapper[sourceNodeLabel];
      j = labelToIdMapper[targetNodeLabel];
      network.add_flow(i, j, minCapacity, flow, maxCapacity);

      label = "(";
      label += StringJ::From<GraphTypes::FlowTypes::Flow>(minCapacity)+","+StringJ::From<GraphTypes::FlowTypes::Flow>(maxCapacity)+","+StringJ::From<GraphTypes::FlowTypes::Flow>(flow)+")";
      config.attributesOf( Edge(i,j) ).setLabel(label);

      if( in.good() )
	in >> sourceNodeLabel;
    }

  //source and sink
  if( in.good() )
    {
      in >> label;
      network.setSource( labelToIdMapper[label] );
    }
Ejemplo n.º 3
0
ResidualNetwork::ResidualNetwork(const FlowNetwork &g)
											                      : ResidualNetwork(g.getNumNodes()) {
	// FlowNetwork stores each arc once
	for (const Arc &arc : g) {
		addArc(arc.getSrcId(), arc.getDstId(), arc.getCapacity(), arc.getCost());
	}

	// clone balances
	for (uint32_t id = 1; id <= num_nodes; id++) {
		balances[id] = g.getBalance(id);
	}
}
bool ok(vector<string> &bridges, int an, int bn, int a1, int a2, int b1, int b2) {
    FlowNetwork F;
    int source = F.add_source();
    int sink = F.add_sink();
    int n = bridges.size();
    vector<int> nodes;
    for (int i=0; i<n; ++i) {
        nodes.push_back(F.add_node());
    }
    F.add_edge(source, nodes[a1], 2*an);
    F.add_edge(source, nodes[b1], 2*bn);
    F.add_edge(nodes[a2], sink, 2*an);
    F.add_edge(nodes[b2], sink, 2*bn);
    for (int i=0; i<n; ++i) {
        for (int j=0; j<n; ++j) {
            if (bridges[i][j] == 'N') {
                F.add_edge(nodes[i], nodes[j], inf);
            } else if (bridges[i][j] == 'O') {
                F.add_edge(nodes[i], nodes[j], 2);
            }
        }
    }
    return F.maxflow() == 2*(an+bn);
}
Ejemplo n.º 5
0
int main() {
  bool blank = false;
  int n, m;
  double l, r, lr, flow;

#ifndef __WATASHI__
  freopen("network.in", "r", stdin);
  freopen("network.out", "w", stdout);
#endif
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d%lf", &a[i], &b[i], &c[i]);
    --a[i];
    --b[i];
  }
  if (blank) {
    puts("");
  } else {
    blank = true;
  }

  l = *min_element(c, c + n);
  r = *max_element(c, c + n);
  while (r - l > EPS) {
    fn.init(n, 0, n - 1);
    lr = (l + r) / 2;
    flow = 0;
    for (int i = 0; i < m; ++i) {
      if (c[i] <= lr) {
        flow += c[i] - lr;
      } else {
        fn.addEdge(a[i], b[i], c[i] - lr);
      }
    }
    flow += fn.sap();
    if (flow > 0) {
      l = lr;
    } else {
      r = lr;
    }
  }

  fn.init(n, 0, n - 1);
  for (int i = 0; i < m; ++i) {
    if (c[i] <= r) {
      fn.addEdge(0, 0, 0);
    } else {
      fn.addEdge(a[i], b[i], c[i] - r);
    }
  }
  fn.sap();
  fn._bfs();
  vector<int> ans;
  for (int i = 0; i < m; ++i) {
    if (c[i] <= r || ((fn.d[a[i]] == fn.n) ^ (fn.d[b[i]] == fn.n))) {
      ans.push_back(i + 1);
    }
  }
  printf("%d\n", (int)ans.size());
  for (int i = 0; i < (int)ans.size(); ++i) {
    if (i > 0) {
      putchar(' ');
    }
    printf("%d", ans[i]);
  }
  puts("");

  return 0;
}