zoukankan      html  css  js  c++  java
  • F

    题目链接:https://vjudge.net/contest/299467#problem/F

    这个是一个很简单的题目,但是读入很有意思,通过这个题目,我学会了一种新的读入方式。

    一个旧的是(%d,%d)%d  这个,但是这个题目不知道为什么,用不了。

    还有一个就是用sscanf   可以看这个博客学习一下 https://blog.csdn.net/yanyanwenmeng/article/details/82753014

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int INF = 0x3f3f3f3f;
    const int maxn = 5e4 + 10;
    
    struct edge
    {
        int u, v, c, f;
        edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
    };
    vector<edge>e;
    vector<int>G[maxn];
    int level[maxn];//BFS分层,表示每个点的层数
    int iter[maxn];//当前弧优化
    int m;
    void init(int n)
    {
        for (int i = 0; i <= n; i++)G[i].clear();
        e.clear();
    }
    void add(int u, int v, int c)
    {
        e.push_back(edge(u, v, c, 0));
        e.push_back(edge(v, u, 0, 0));
        m = e.size();
        G[u].push_back(m - 2);
        G[v].push_back(m - 1);
    }
    void BFS(int s)//预处理出level数组
    //直接BFS到每个点
    {
        memset(level, -1, sizeof(level));
        queue<int>q;
        level[s] = 0;
        q.push(s);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            for (int v = 0; v < G[u].size(); v++)
            {
                edge& now = e[G[u][v]];
                if (now.c > now.f && level[now.v] < 0)
                {
                    level[now.v] = level[u] + 1;
                    q.push(now.v);
                }
            }
        }
    }
    int dfs(int u, int t, int f)//DFS寻找增广路
    {
        if (u == t)return f;//已经到达源点,返回流量f
        for (int &v = iter[u]; v < G[u].size(); v++)
            //这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
            //在每次找增广路的时候,数组要清空
        {
            edge &now = e[G[u][v]];
            if (now.c - now.f > 0 && level[u] < level[now.v])
                //now.c - now.f > 0表示这条路还未满
                //level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
            {
                int d = dfs(now.v, t, min(f, now.c - now.f));
                if (d > 0)
                {
                    now.f += d;//正向边流量加d
                    e[G[u][v] ^ 1].f -= d;
                    //反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
                    return d;
                }
            }
        }
        return 0;
    }
    int Maxflow(int s, int t)
    {
        int flow = 0;
        for (;;)
        {
            BFS(s);
            if (level[t] < 0)return flow;//残余网络中到达不了t,增广路不存在
            memset(iter, 0, sizeof(iter));//清空当前弧数组
            int f;//记录增广路的可增加的流量
            while ((f = dfs(s, t, INF)) > 0)
            {
                flow += f;
            }
        }
        return flow;
    }
    
    int main()
    {
        int n, np, nc, m;
        while(~scanf("%d %d %d %d ",&n,&np,&nc,&m))
        {
            char cs[100];
            int u, v, c;
            int s = n, t = n + 1;
            init(n);
            while(m--)
            {
                scanf("%s", cs);
                sscanf(cs, "(%d,%d)%d", &u, &v, &c);
                add(u, v, c);
            }
            while(np--)
            {
                scanf("%s", cs);
                sscanf(cs, "(%d)%d", &u, &c);
                add(s, u, c);
            }
            while(nc--)
            {
                scanf("%s", cs);
                sscanf(cs, "(%d)%d", &u, &c);
                add(u, t, c);
            }
            int ans = Maxflow(s, t);
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    [算法题] 汉诺塔问题
    ubuntu导入torch模块报错
    深度问答之提取语料2
    深度问答之提取语料,导入了yml模块
    python读取文件存到excel中
    zipfile.BadZipFile: File is not a zip file
    查看linux系统时间
    tensorflow:typeerror:‘noneType’ object is not callable
    正则匹配中文标点符号
    re.sub用法
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10830015.html
Copyright © 2011-2022 走看看