引水入城

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算法的更多相关文章

  1. CCF CSP 201703-5 引水入城(50分)

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-5 引水入城 问题描述 MF城建立在一片高原上.由于城市唯一的水源是位于河谷地带的 ...

  2. 【五一qbxt】day7-1 引水入城

    [noip2010 洛谷p1514]引水入城 Before: 线段覆盖问题#1:(我们所需要的) 一个区间,若干条线段,现在求最少多少条线段覆盖满整个区间 区间长度8,可选的覆盖线段[2,6],[1, ...

  3. Codevs 1066 引水入城 2010年NOIP全国联赛提高组

    1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 在一个遥远的国度 ...

  4. Luogu 1514 引水入城 (搜索,动态规划)

    Luogu 1514 引水入城 (搜索,动态规划) Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,如上图 ...

  5. 洛谷P1514 引水入城

    洛谷P1514 引水入城 原题链接 一道好题...细节真多 第一次提交90分,然后就GG了,不知从何改起 其实比较简单吧... 首先,一个点的水流向最后一排,一定可以形成一个区间. 不行的话肯定GG ...

  6. luoguP1514 引水入城 x

    P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城 ...

  7. NOIP2010 引水入城

    4引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个 ...

  8. CODEVS 1066/洛谷 P1514引水入城

    1066 引水入城 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 在一个遥远的国 ...

  9. 洛谷 P1514 引水入城 解题报告

    P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 NN 行 \times M×M 列的矩形,如上图所示,其中每个格 ...

随机推荐

  1. zjnu1726 STOGOVI (lca)

    Description Mirko is playing with stacks. In the beginning of the game, he has an empty stack denote ...

  2. 【noi 2.6_4978】宠物小精灵之收服(DP)

    题意:小智有N个精灵球,皮卡丘有M的初始体力,有K个野生小精灵.要收服尽可能多的野生小精灵,并使皮卡丘的剩余体力最大. 解法:01背包问题,增多一维来存第二个条件.f[i][j][k]表示抓前i个野生 ...

  3. hdu2669Romantic (扩展欧几里德)

    Problem Description The Sky is Sprite. The Birds is Fly in the Sky. The Wind is Wonderful. Blew Thro ...

  4. Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)

    题意:有\(n\)个点,\(m\)条边,现在要给这些点赋值,,每次只能赋给某一点的四周(所连边)的最小没出现过的值.如果不能按照所给的数赋值,输出\(-1\),否则输出赋值顺序. 题解:我们用\(pa ...

  5. Educational Codeforces Round 94 (Rated for Div. 2) A. String Similarity (构造水题)

    题意:给你一个长度为\(2*n-1\)的字符串\(s\),让你构造一个长度为\(n\)的字符串,使得构造的字符串中有相同位置的字符等于\(s[1..n],s[2..n+1],...,s[n,2n-1] ...

  6. woj1019 Curriculum Schedule 输入输出 woj1020 Adjacent Difference 排序

    title: woj1019 Curriculum Schedule 输入输出 date: 2020-03-19 10:43:00 categories: acm tags: [acm,woj] 水题 ...

  7. SPOJ REPEATS Repeats (后缀数组 + RMQ:子串的最大循环节)题解

    题意: 给定一个串\(s\),\(s\)必有一个最大循环节的连续子串\(ss\),问最大循环次数是多少 思路: 我们可以知道,如果一个长度为\(L\)的子串连续出现了两次及以上,那么必然会存在\(s[ ...

  8. js sort map by key

    js sort map by key Map map to array // Array.from() Object let obj = {}; for(let key of Object.keys( ...

  9. MySQL 8.x

    MySQL 8.x SQL & NoSQL $ mysql --version # mysql Ver 8.0.21 for osx10.15 on x86_64 (Homebrew) # M ...

  10. js IdleDetector 检测用户是否处于活动状态API

    btn.addEventListener("click", async () => { try { const state = await Notification.requ ...