HDU 3157 Crazy Circuits (the smallest stream at the upper and lower bounds of the source sink)

Source: Internet
Author: User

HDU 3157 Crazy Circuits (the smallest stream at the upper and lower bounds of the source sink)
HDU 3157 Crazy Circuits

Question Link

A circuit board with N wiring bars (Number 1 ~ N), there are two power supply terminals +-to provide some lines, each line has a lower limit to find a total current that can make all parts work normally, then output impossible

Ideas:
The source sink has the upper and lower bounds to find the smallest stream. The modeling method is as follows:
Create a graph by no source sink, run the super source sink ss-> tt once, then add t-> s, the capacity INF side, and run ss-> tt once. If the stream is full, it can be resolved to the current traffic at the t-> s side.

Write the maximum stream. For the maximum stream, add t-> s and run it directly. The traffic of t-> s is enough.

Code:

#include 
 
  #include 
  
   #include 
   
    #include using namespace std;const int MAXNODE = 65;const int MAXEDGE = 10005;typedef int Type;const Type INF = 0x3f3f3f3f;struct Edge {int u, v;Type cap, flow;Edge() {}Edge(int u, int v, Type cap, Type flow) {this->u = u;this->v = v;this->cap = cap;this->flow = flow;}};struct Dinic {int n, m, s, t;Edge edges[MAXEDGE];int first[MAXNODE];int next[MAXEDGE];bool vis[MAXNODE];Type d[MAXNODE];Type flow;int cur[MAXNODE];void init(int n) {this->n = n;memset(first, -1, sizeof(first));m = 0;flow = 0;}void add_Edge(int u, int v, Type cap) {edges[m] = Edge(u, v, cap, 0);next[m] = first[u];first[u] = m++;edges[m] = Edge(v, u, 0, 0);next[m] = first[v];first[v] = m++;}bool bfs() {memset(vis, false, sizeof(vis));queue
    
      Q;Q.push(s);d[s] = 0;vis[s] = true;while (!Q.empty()) {int u = Q.front(); Q.pop();for (int i = first[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (!vis[e.v] && e.cap > e.flow) {vis[e.v] = true;d[e.v] = d[u] + 1;Q.push(e.v);}}}return vis[t];}Type dfs(int u, Type a) {if (u == t || a == 0) return a;Type flow = 0, f;for (int &i = cur[u]; i != -1; i = next[i]) {Edge& e = edges[i];if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {e.flow += f;edges[i^1].flow -= f;flow += f;a -= f;if (a == 0) break;}}return flow;}Type Maxflow(int s, int t) {this->s = s; this->t = t;while (bfs()) {for (int i = 0; i < n; i++)cur[i] = first[i];flow += dfs(s, INF);}return flow;}bool judge(int s) {for (int i = first[s]; i + 1; i = next[i])if (edges[i].flow != edges[i].cap) return false;return true;}} gao;const int N = 65;int n, m, s, t, ss, tt, du[N];char c[15];int main() {while (~scanf("%d%d", &n, &m) && n || m) {gao.init(n + 4);s = 0; t = n + 1; ss = n + 2; tt = n + 3;int u, v, w;memset(du, 0, sizeof(du));while (m--) {scanf("%s", c);if (c[0] == '+') u = s;else sscanf(c, "%d", &u);scanf("%s", c);if (c[0] == '-') v = t;else sscanf(c, "%d", &v);scanf("%d", &w);gao.add_Edge(u, v, INF);du[u] -= w;du[v] += w;}for (int i = s; i <= t; i++) {if (du[i] > 0) gao.add_Edge(ss, i, du[i]);if (du[i] < 0) gao.add_Edge(i, tt, -du[i]);}gao.Maxflow(ss, tt);gao.add_Edge(t, s, INF);gao.Maxflow(ss, tt);if (!gao.judge(ss)) printf("impossible\n");else printf("%d\n", gao.edges[gao.m - 2].flow);}return 0;}
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.