zoukankan      html  css  js  c++  java
  • HDU-4280-Island Transport(网络流,最大流, ISAP)

    链接:

    https://vjudge.net/problem/HDU-4280

    题意:

    In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
      You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
      The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.

    思路:

    考虑无向图,只需给反向边加上容量,同时图的点很多,使用ISAP算法.
    找了很久,看了很久..找的代码bfs写错了(貌似?).最后很久才改出来.

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 1e5+10;
    const int INF = 1e9;
    
    struct Edge
    {
        int from, to, flow, cap;
    };
    
    int Pre[MAXN], Cur[MAXN];
    int Num[MAXN], Vis[MAXN];
    int Dis[MAXN];
    vector<int> G[MAXN];
    vector<Edge> edges;
    int n, m, s, t;
    
    void AddEdge(int from, int to, int cap)
    {
        edges.push_back(Edge{from, to, 0, cap});
        edges.push_back(Edge{to, from, 0, cap});
        int len = edges.size();
        G[from].push_back(len - 2);
        G[to].push_back(len - 1);
    }
    
    void Bfs()
    {
        memset(Vis, 0, sizeof(Vis));
        queue<int> que;
        que.push(t);
        Vis[t] = 1;
        Dis[t] = 0;
        while (!que.empty())
        {
            int u = que.front();
            que.pop();
            for (int i = 0;i < G[u].size();i++)
            {
                Edge &e = edges[G[u][i]^1];
                if (Vis[e.from] == 0 && e.cap > e.flow)
                {
                    Vis[e.from] = 1;
                    que.push(e.from);
                    Dis[e.from] = Dis[u]+1;
                }
            }
        }
    }
    
    int Augment()
    {
    //    cout << 1 << endl;
        int x = t, flow = INF;
        while (x != s)
        {
            Edge &e = edges[Pre[x]];
    //        cout << e.from << ' ' << e.to << endl;
            flow = min(flow, e.cap-e.flow);
            x = e.from;
        }
    //    cout << flow << endl;
        x = t;
        while (x != s)
        {
            edges[Pre[x]].flow += flow;
            edges[Pre[x]^1].flow -= flow;
            x = edges[Pre[x]].from;
        }
        return flow;
    }
    
    int MaxFlow()
    {
        int flow = 0;
        Bfs();
        memset(Num, 0, sizeof(Num));
        for (int i = 0;i < n;i++)
            Num[Dis[i]]++;
        int x = s;
        memset(Cur, 0, sizeof(Cur));
        while (Dis[s] < n)
        {
            if (x == t)
            {
                flow += Augment();
                x = s;
            }
            bool ok = false;
            for (int i = Cur[x];i < G[x].size();i++)
            {
                Edge &e = edges[G[x][i]];
                if (e.cap > e.flow && Dis[x] == Dis[e.to]+1)
                {
                    ok = true;
                    Pre[e.to] = G[x][i];
                    Cur[x] = i;
                    x = e.to;
                    break;
                }
            }
            if (!ok)
            {
                int line = n-1;
                for (int i = 0;i < G[x].size();i++)
                {
                    Edge &e = edges[G[x][i]];
                    if (e.cap > e.flow)
                        line = min(line, Dis[e.to]);
                }
                if (--Num[Dis[x]] == 0)
                    break;
                Dis[x] = line+1;
                Num[Dis[x]]++;
                Cur[x] = 0;
                if (x != s)
                    x = edges[Pre[x]].from;
            }
        }
        return flow;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while (T--)
        {
            scanf("%d %d", &n, &m);
            for (int i = 1;i <= n;i++)
                G[i].clear();
            edges.clear();
            int mmin = INF, mmax = -INF;
            int u, v, w;
            for (int i = 1;i <= n;i++)
            {
                scanf("%d %d", &u, &v);
                if (u < mmin)
                    s = i, mmin = u;
                if (u > mmax)
                    t = i, mmax = u;
            }
            for (int i = 1;i <= m;i++)
            {
                scanf("%d %d %d", &u, &v, &w);
                AddEdge(u, v, w);
            }
            int res = MaxFlow();
            printf("%d
    ", res);
        }
    
        return 0;
    }
    
  • 相关阅读:
    Python从菜鸟到高手(18):类与方法的私有化
    小程序云开发实战
    第一行代码:以太坊(3)-使用更多的工具编写和测试智能合约
    第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约
    第一行代码:以太坊(1)-创建自己的私有区块链
    用量子物理学原理解释为什么振金可以吸收能量(论发散思维的重要性)
    Python网络爬虫实战:根据天猫胸罩销售数据分析中国女性胸部大小分布
    Python从菜鸟到高手(8):print函数、赋值与代码块
    基于ArcPy的ArcGIS python设计与开发实战--GIS二次开发
    tensorflow数据增强
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11329074.html
Copyright © 2011-2022 走看看