引水入城

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. AtCoder Beginner Contest 177 D - Friends (并查集)

    题意:有\(n\)个人,给你\(m\)对朋友关系,朋友的朋友也是朋友,现在你想要将他们拆散放到不同的集合中,且每个集合中的人没有任何一对朋友关系,问最少需要多少集合. 题解:首先用并查集将朋友关系维护 ...

  2. C#之字符编码

    在 Windows Vista 及之后的版本中,每个Unicode字符都使用UTF-16编码,UTF的全称是 Unicode Transformation Format(Unicode 转换格式).U ...

  3. leetcode 4 寻找两个有序数组的中位数 二分法&INT_MAX

    小知识 INT_MIN在标准头文件limits.h中定义. #define INT_MAX 2147483647#define INT_MIN (-INT_MAX - 1) 题解思路 其实是类似的二分 ...

  4. liunx命令二

    声明:以下资料全部摘自实验楼 常用快捷键 按键 作用 Table 补全命令 Ctrl+c 强制结束 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂停当前程序,暂停后按下任意键恢复运行 Ctrl ...

  5. P1337 [JSOI2004]平衡点(模拟退火)题解

    题意: 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不会垂到地 ...

  6. BZOJ3211 花神游历各国(分块 区间开根号)

    题意:给n个数,可以进行两种操作:给区间[l,r]每个数开方向下取整:算区间[l,r]的和. 思路:我们可以知道,一个数一直开方下去,就会变成0或者1,然后就不会变了.那么当一个区间只剩0或1时,就不 ...

  7. cheerio & jQuery for server

    cheerio & jQuery for server Fast, flexible & lean implementation of core jQuery designed spe ...

  8. ES5 function & ES6 class & method type

    ES5 function & ES6 class & method type ES5 function "use strict"; /** * * @author ...

  9. Node.js & process.env & OS Platform checker

    Node.js & process.env & OS Platform checker Window 10 Windows 7 ia32 CentOS $ node # process ...

  10. js 实现各种算法 APP

    js 实现各种算法 APP 常见算法: 排序,搜索/查找,枚举,遍历,最短路径,二叉树 open source web app desktop app react native app flutter ...