• Crazy Circuits HDU


     给出每条边的下界 求最小流

    板题  

    提供两个板子代码 虽然这个题 第一个比较快

    但在loj上https://loj.ac/problem/117

    的板题  第一个1700+ms

    第二个才600+ms  用输入挂后400+ms

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 1e5 + 10, INF = 0x7fffffff;
    int n, m, s, t;
    int head[maxn], cur[maxn], vis[maxn], nex[maxn << 1], in[maxn];
    int d[maxn];
    int cnt;
    struct node
    {
        int u, v;
        int c;
    }Node[maxn << 1];
    
    void add_(int u, int v, int c)
    {
        Node[cnt].u = u;
        Node[cnt].v = v;
        Node[cnt].c = c;
        nex[cnt] = head[u];
        head[u] = cnt++;
    }
    
    void add(int u, int v, int c)
    {
        add_(u, v, c);
        add_(v, u, 0);
    }
    
    bool bfs()
    {
        queue<int> Q;
        mem(d, 0);
        Q.push(s);
        d[s] = 1;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = nex[i])
            {
                int v = Node[i].v;
                if(!d[v] && Node[i].c > 0)
                {
                    d[v] = d[u] + 1;
                    Q.push(v);
                    if(v == t) return 1;
                }
            }
        }
        return d[t] != 0;
    }
    
    int dfs(int u, int cap)
    {
        int ret = 0;
        if(u == t || cap == 0)
            return cap;
        for(int &i = cur[u]; i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(d[v] == d[u] + 1 && Node[i].c > 0)
            {
                int V = dfs(v, min(cap, Node[i].c));
                Node[i].c -= V;
                Node[i ^ 1].c += V;
                ret += V;
                cap -= V;
                if(cap == 0) break;
            }
        }
        if(cap > 0) d[u] = -1;
        return ret;
    }
    
    int Dinic()
    {
        int ans = 0;
        while(bfs())
        {
            memcpy(cur, head, sizeof head);
            ans += dfs(s, INF);
        }
        return ans;
    }
    
    int tranf(string x)
    {
        if(x == "+") return 1;
        if(x == "-") return n + 2;
        int ans = 0, len = x.size();
        for(int i = 0; i < len; i++)
        {
            ans += (x[i] - '0') * pow(10, len - i - 1);
        }
        return ans + 1;
    }
    
    
    
    int main()
    {
        int w;
        string u, v;
        int u_, v_;
        while(~scanf("%d%d", &n, &m))
        {
            if(n == 0 && m == 0) break;
            mem(head, -1);
            cnt = 0;
            mem(in, 0);
            int sum = 0;
            s = 0, t = n + 3;
            rap(i, 1, m)
            {
                // scanf(" %c %c %d", &u, &v, &w);
                cin >> u >> v >> w;
                u_ = tranf(u);
                v_ = tranf(v);
                add(u_, v_, INF - w);
                in[v_] += w;
                in[u_] -= w;
            }
            for(int i = 1; i <= n + 2; i++)
            {
                if(in[i] > 0)
                {
                    sum += in[i];
                    add(s, i, in[i]);
                }
                else
                    add(i, t, -in[i]);
            }
            int ans = Dinic();
            add(n + 2, 1, INF);
            ans += Dinic();
            if(sum == ans)
                cout << Node[head[(n + 2)] ^ 1].c << endl;
            else
                cout << "impossible" << endl;
    
        }
        return 0;
    }
    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 1e5 + 10, INF = 0x7fffffff;
    int n, m, s, t;
    int head[maxn], cur[maxn], vis[maxn], nex[maxn << 1], in[maxn];
    int d[maxn];
    int cnt;
    struct node
    {
        int u, v, bz;
        int c;
    }Node[maxn << 1];
    
    void add_(int u, int v, int c, int bz)
    {
        Node[cnt].u = u;
        Node[cnt].v = v;
        Node[cnt].c = c;
        Node[cnt].bz = bz;
        nex[cnt] = head[u];
        head[u] = cnt++;
    }
    
    void add(int u, int v, int c, int bz)
    {
        add_(u, v, c, bz);
        add_(v, u, 0, bz);
    }
    
    bool bfs()
    {
        queue<int> Q;
        mem(d, 0);
        Q.push(s);
        d[s] = 1;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = nex[i])
            {
                int v = Node[i].v;
                if(!d[v] && Node[i].c > 0)
                {
                    d[v] = d[u] + 1;
                    Q.push(v);
                    if(v == t) return 1;
                }
            }
        }
        return d[t] != 0;
    }
    
    int dfs(int u, int cap)
    {
        int ret = 0;
        if(u == t || cap == 0)
            return cap;
        for(int &i = cur[u]; i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(d[v] == d[u] + 1 && Node[i].c > 0)
            {
                int V = dfs(v, min(cap, Node[i].c));
                Node[i].c -= V;
                Node[i ^ 1].c += V;
                ret += V;
                cap -= V;
                if(cap == 0) break;
            }
        }
        if(cap > 0) d[u] = -1;
        return ret;
    }
    
    int Dinic()
    {
        int ans = 0;
        while(bfs())
        {
            memcpy(cur, head, sizeof head);
            ans += dfs(s, INF);
        }
        return ans;
    }
    
    int tranf(string x)
    {
        if(x == "+") return 1;
        if(x == "-") return n + 2;
        int ans = 0, len = x.size();
        for(int i = 0; i < len; i++)
        {
            ans += (x[i] - '0') * pow(10, len - i - 1);
        }
        return ans + 1;
    }
    
    
    
    int main()
    {
        int w;
        string u, v;
        int u_, v_;
        while(~scanf("%d%d", &n, &m))
        {
            if(n == 0 && m == 0) break;
            mem(head, -1);
            cnt = 0;
            mem(in, 0);
            int sum = 0;
            s = 0, t = n + 3;
            rap(i, 1, m)
            {
                // scanf(" %c %c %d", &u, &v, &w);
                cin >> u >> v >> w;
                u_ = tranf(u);
                v_ = tranf(v);
                add(u_, v_, INF - w, 1);
                in[v_] += w;
                in[u_] -= w;
            }
            for(int i = 1; i <= n + 2; i++)
            {
                if(in[i] > 0)
                {
                    sum += in[i];
                    add(s, i, in[i], 0);
                }
                else
                    add(i, t, -in[i], 0);
            }
            add(n + 2, 1, INF, 0);
            if(sum != Dinic())
                ps("impossible");
            else
            {
                sum = Node[head[n + 2] ^ 1].c;
                for(int i = 0; i < cnt; i++)
                {
                    if(!Node[i].bz) Node[i].v = 0;
                }
                head[s] = head[t] = -1;
                s = n + 2;
                t = 1;
                cout << sum - Dinic() << endl;
            }
    
        }
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    java.lang.IllegalStateException: getOutputStream() has already been called for this response
    html5之table嵌入form表单布局(务必注意:table标签必须在form表单内部,不能再form表单外部!)
    两张图搞清楚Eclipse上的Web项目目录
    使用VS 2019发布.net core程序并部署到IIS的最新教程
    国外便宜虚拟主机有哪些?
    怎样收用电汇来收adsense的广告费呢
    程序员同性交友网站github在国内不能访问了
    直接引用MrAdvice.dll文件不能实现AOP拦截,教你1分钟解决这个问题
    VS2019打包WPF安装程序最新教程
    通过VS2017发布.net core程序并使用Web 部署到远程服务器最新教程
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9992001.html
Copyright © 2020-2023  润新知