码迷,mamicode.com
首页 > 编程语言 > 详细

CCF(引水入城:60分):最大流+ISAP算法

时间:2019-09-08 11:17:55      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:you   use   include   NPU   ||   数据   view   cti   turn   

引水入城

201703-5

  • 这从题目分析来看很像最大流的问题,只需要增加一个超级源点和一个超级汇点就可以按照题意连边再跑最大流算法。
  • 因为数据量太大了,肯定会超时。但是没有想到可行的解决方法。
#include<bits/stdc++.h>
using namespace std;
const long long INF=0XFFFFFFFF;
const int maxn=4500016;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Edge {
  int from, to;long long cap, flow;
  Edge(int u, int v, long long c, long long f) : from(u), to(v), cap(c), flow(f) {}
};
bool operator<(const Edge& a, const Edge& b) {
  return a.from < b.from || (a.from == b.from && a.to < b.to);
}

struct ISAP {
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool vis[maxn];
  int d[maxn];
  int cur[maxn];
  int p[maxn];
  int num[maxn];

  void AddEdge(int from, int to, long long cap) {
    edges.push_back(Edge(from, to, cap, 0));
    edges.push_back(Edge(to, from, 0, 0));
    m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
  }

  bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(t);
    vis[t] = 1;
    d[t] = 0;
    while (!Q.empty()) {
      int x = Q.front();
      Q.pop();
      for (int i = 0; i < G[x].size(); i++) {
        Edge& e = edges[G[x][i] ^ 1];
        if (!vis[e.from] && e.cap > e.flow) {
          vis[e.from] = 1;
          d[e.from] = d[x] + 1;
          Q.push(e.from);
        }
      }
    }
    return vis[s];
  }

  void init(int n) {
    this->n = n;
    for (int i = 0; i <= n; i++) G[i].clear();
    edges.clear();
  }

  int Augment() {
    int x = t;long long a = INF;
    while (x != s) {
      Edge& e = edges[p[x]];
      a = min(a, e.cap - e.flow);
      x = edges[p[x]].from;
    }
    x = t;
    while (x != s) {
      edges[p[x]].flow += a;
      edges[p[x] ^ 1].flow -= a;
      x = edges[p[x]].from;
    }
    return a;
  }

 long long Maxflow(int s, int t) {
    this->s = s;
    this->t = t;
    long long flow = 0;
    BFS();
    memset(num, 0, sizeof(num));
    for (int i = 0; i <= n; i++) num[d[i]]++;
    int x = s;
    memset(cur, 0, sizeof(cur));
    while (d[s] < n) {
      if (x == t) {
        flow += Augment();
        x = s;
      }
      int ok = 0;
      for (int i = cur[x]; i < G[x].size(); i++) {
        Edge& e = edges[G[x][i]];
        if (e.cap > e.flow && d[x] == d[e.to] + 1) {
          ok = 1;
          p[e.to] = G[x][i];
          cur[x] = i;
          x = e.to;
          break;
        }
      }
      if (!ok) {
        int m = n - 1;
        for (int i = 0; i < G[x].size(); i++) {
          Edge& e = edges[G[x][i]];
          if (e.cap > e.flow) m = min(m, d[e.to]);
        }
        if (--num[d[x]] == 0) break;
        num[d[x] = m + 1]++;
        cur[x] = 0;
        if (x != s) x = edges[p[x]].from;
      }
    }
    return flow;
  }
}ek;
long long a,b,mod,x;
int n,m;
int compute(){
    return x=(a*x+b)%mod;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m>>a>>b>>mod>>x;
    int s=0,t=n*m+1;
    ek.init(n*m+1);
    for(int i=0;i<n-1;i++){
        for(int j=0;j<m;j++){
            int from=i*m+j+1;
            int to=from+m;
            int cost=compute();
            ek.AddEdge(from,to,cost);
            ek.AddEdge(to,from,INF);
        }
    }
    for(int i=1;i<n-1;i++){
        for(int j=0;j<m-1;j++){
            int from=i*m+j+1;
            int to=from+1;
            int cost=compute();
            ek.AddEdge(from,to,cost);
            ek.AddEdge(to,from,cost);
        }
    }
    for(int i=0;i<m;i++){
        ek.AddEdge(s,i+1,INF);
    }
    for(int i=0;i<m;i++){
        int from=(n-1)*m+i+1;
        int to=t;
        ek.AddEdge(from,to,INF);
    }
    cout<<ek.Maxflow(s,t)<<endl;
    return 0;
}

CCF(引水入城:60分):最大流+ISAP算法

标签:you   use   include   NPU   ||   数据   view   cti   turn   

原文地址:https://www.cnblogs.com/GarrettWale/p/11484773.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!