zoj2676 Network Wars (0-1 score plan, maximum flow template)

Source: Internet
Author: User

Network Wars

The title of the 07 Hubertau paper: http://wenku.baidu.com/view/87ecda38376baf1ffc4fad25.html
Code:

#include <algorithm> #include <cstdio> #include <iterator> #include <limits> #include < vector> #include <string.h>const int N = 111;const int M = 404;const double EPS = 1e-3;typedef std::vector<int& Gt Vi;int Signum (double x) {return x > EPS? 1: (X <-eps -1:0);}    Template<int N, int M, class flow>struct dinic {int N, E, first[n], cur[n], next[m], to[m], id[m], S, t;    int pre[n], level[n], q[n], sign;    Flow cap[m], flow;        void Add (int u, int v, Flow w, int i) {to[e] = V;        Cap[e] = W;        Id[e] = i;        Next[e] = First[u];    First[u] = e++;        } bool BFs (int s, int t) {Std::fill (level+1, Level + n + 1,-1);        sign = t;        Level[t] = 0;        int head = 0, tail = 0;        q[tail++] = t;            while (head! = Tail && level[s] = =-1) {int u = q[head++]; for (int it = First[u]; it! =-1; it = Next[it]) {if (cap[it ^ 1] > 0 &&                    Level[to[it]] = = 1) {Level[to[it]] = Level[u] + 1;                q[tail++] = To[it];    }}} return Level[s]! =-1;        } void Push () {Flow delta = Std::numeric_limits<flow>::max ();        int u, p;            for (U = t; u = s; u = to[p ^ 1]) {p = pre[u];        Delta = std::min (Delta, cap[p]);            } for (U = t; u = s; u = to[p ^ 1]) {p = pre[u];            CAP[P]-= Delta;            if (!cap[p]) {sign = to[p ^ 1];        } cap[p ^ 1] + + Delta;    } flow + + Delta;        } void Dfs (int u) {if (U = = t) {push (); } else {for (int & it = Cur[u]; it! =-1; it = Next[it]) {if (Cap[it] > 0 && le                    Vel[u] = = Level[to[it]] + 1) {Pre[to[it]] = it;                    DFS (To[it]);                       if (Level[sign] > Level[u]) { Return                } sign = t;        }} Level[u] =-1;        }} void init (int _n, int _s, int _t) {n = _n, s = _s, t = _t;        Std::fill (first + 1, first + n + 1,-1);    e = 0;        } flow Solve () {flow = 0;            while (BFS (s, t)) {for (int i = 1; I <= n; ++i) {cur[i] = First[i];        } dfs (s);    } return flow;    }};D inic<n,m<<1,double> AC; int left[m], right[m], w[m]; int N, M, Mark[m];d ouble judge (double key) {    Double sum = 0;    memset (mark, 0, sizeof (Mark));    Ac.init (n, 1, N);            for (int i = 1; I <= m; i + +) if (W[i] <= key) {sum + = w[i]-key;        Mark[i] = 1;            } else {Ac.add (left[i], right[i], W[i]-key, i);        Ac.add (Right[i], left[i], W[i]-key, i);    } Double add = Ac.solve (); Sum + = Add ; return sum;}    void Solve () {double L = 0, R = (double) m * 1E7;        while (Signum (r-l) > 0) {Double mid = (L + r)/2.0;        Double k = Judge (mid);        if (Signum (k) >= 0) L = mid;    else R = Mid;    } ac.bfs (1, N);        for (int i = 0; i < AC.E; i + +) {int u = ac.to[i^1], v = ac.to[i];    if (ac.level[u] = =-1 && ac.level[v]! =-1) mark[ac.id[i] = 1;    } int ans = 0;    for (int i = 1; I <= m; i + +) if (Mark[i]) ans + +;    printf ("%d\n", ans);    for (int i = 1; I <= m; i + +) if (Mark[i]) printf ("%d", I); Puts ("");}    int main () {//Freopen ("Network.in", "R", stdin);//Freopen ("Network.out", "w", stdout);    int flag = 0; while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 1; I <= m; i + +) scanf ("%d%d        %d ", &left[i], &right[i], &w[i]);        if (flag) puts (""); Flag =1;    Solve (); } return 0;}


zoj2676 Network Wars (0-1 score plan, maximum flow template)

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.