题意:

输入一个字符矩阵,'.'代表洞,'#'代表草地。可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b。

但要保证最外一圈是草,求最小费用。

分析:

还不是特别理解紫书上的讲解。。

首先把最外一圈的洞变成草,并累加花费。

增加一个源点和一个汇点,源点连接每个草地,汇点连接每个洞。

源点与最外一圈的草地连一条容量无穷大的边,与其他草地连一条容量为d的边。表示把这条弧切断,割的容量增加d,草就会变成洞。

每个洞与汇点连一条容量为f的边。

相邻两个格子之间连一条双向边。

用最大流算法求最小割在加上之前把边界上的洞变成草的费用,就是最小花费。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  *  + ;
const int INF = ; struct Edge
{
int from, to, cap, flow;
}; bool operator < (const Edge& a, const Edge& b)
{ return a.from < b.from || ( a.from == b.from && a.to < b.to ); } struct Dinic
{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn]; //BFS
int d[maxn]; //起点到i的距离
int cur[maxn]; //当前弧指针 int Init(int n)
{
for(int i = ; i < n; ++i) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge{from, to, cap, });
edges.push_back(Edge{to, from, , });
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
memset(vis, false, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = true;
d[s] = ;
while(!Q.empty())
{
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); ++i)
{
Edge& e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = true;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x, int a)
{
if(x == t || a == ) return a;
int flow = , f;
for(int& i = cur[x]; i < G[x].size(); ++i)
{
Edge& e = edges[G[x][i]];
if(d[x] + ==d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > )
{
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if(a == ) break;
}
}
return flow;
} int MaxFlow(int s, int t)
{
this->s = s; this->t = t;
int flow = ;
while(BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
}g; int w, h;
char pool[][]; inline int ID(int i, int j) { return i*w+j; } int main()
{
//freopen("in.txt", "r", stdin); int T, d, f, b;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d%d%d", &w, &h, &d, &f, &b);
for(int i = ; i < h; ++i) scanf("%s", pool[i]);
int cost = ;
for(int i = ; i < h; ++i)//把边上的洞填成草
{
if(pool[i][] == '.') { pool[i][] = '#'; cost += f; }
if(pool[i][w-] == '.') { pool[i][w-] = '#'; cost += f; }
}
for(int i = ; i < w; ++i)
{
if(pool[][i] == '.') { pool[][i] = '#'; cost += f; }
if(pool[h-][i] == '.') { pool[h-][i] = '#'; cost += f; }
} g.Init(h*w+);
for(int i = ; i < h; i++)
for(int j = ; j < w; j++)
{
if(pool[i][j] == '#')
{
int cap = d;
if(i == || i == h- || j == || j == w-) cap = INF;
g.AddEdge(w*h, ID(i, j), cap);
}
else
{
g.AddEdge(ID(i, j), w*h+, f);
}
if(i > ) g.AddEdge(ID(i, j), ID(i-, j), b);
if(i < h-) g.AddEdge(ID(i, j), ID(i+, j), b);
if(j > ) g.AddEdge(ID(i, j), ID(i, j-), b);
if(j < w-) g.AddEdge(ID(i, j), ID(i, j+), b);
} printf("%d\n", cost + g.MaxFlow(w*h, w*h+));
} return ;
}

代码君

UVa 1515 (最小割) Pool construction的更多相关文章

  1. uva 11248 最小割

    Dinic 1 #include<iostream> #include<string> #include<algorithm> #include<cstdli ...

  2. UVA 1515 Pool construction 最大流跑最小割

    Pool construction You are working for the International Company for Pool Construction, a constructio ...

  3. UVa 1515 - Pool construction(最小割)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. Uva -1515 Pool construction(最小割)

    输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 首先把最外一圈的洞变成草,并累加花费. 增加一个源点和一个汇点,源 ...

  5. 【uva 1515】Pool construction(图论--网络流最小割 模型题)

    题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...

  6. UVa1515 Pool construction(最小割)

    题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. UVALive 5905 Pool Construction 最小割,s-t割性质 难度:3

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. UVA-1515 Pool construction (最小割)

    题目大意:有一块地,分成nxm块.有的块上长着草,有的块上是荒地.将任何一块长着草的块上的草拔掉都需要花费d个力气,往任何一块荒地上种上草都需要花费f个力气,在草和荒地之间架一个篱笆需要花费b个力气, ...

  9. UVA1515 Pool construction (最小割模型)

    如果不允许转化'#'和'.'的话,那么可以直接在'#'和'.'之间连容量为b的边,把所有'#'和一个源点连接, 所有'.'和一个汇点连接,流量不限,那么割就是建围栏(分割'#'和'.')的花费. 问题 ...

随机推荐

  1. sharepoint 列表的column验证----------SharePoint 2010 List Validation Formula

    首先,依次打开-站点->列表名称->列表设置->验证设置: 我们设置一个时间的列不能小于当前时间,并且在编辑的时候不需要验证. =OR([,Created<TODAY())

  2. 【环境】Linux下连接无线网常用命令

    启用/重启/关闭 网络服务 /etc/init.d/networking start /etc/init.d/networking restart /etc/init.d/networking sto ...

  3. [转载]DirectoryEntry配置IIS7出现ADSI Error:未知错误(0x80005000)

    一.错误情况 环境:win7+iis7.0 DirectoryEntry配置IIS7出现如下错误 或者是 下面一段代码在IIS6.0下运转正常,但IIS7.0下运转会出错: System.Direct ...

  4. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  5. git init 与 git init --bare 的区别

    git init  和 git init –bare 的区别 使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repos ...

  6. POJ 3270 Cow Sorting(置换群)

    题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. ...

  7. POJ2485Highways

    http://poj.org/problem?id=2485 题意 : 这道题和1258很像,但是这道题求的是最小生成树中最大的那条边,所以在函数里处理一下就行了. 思路 : 赤裸裸的最小生成树啊,但 ...

  8. jndi配置数据源

    jndi(java Naming and DirectoryInterface,java命名和目录接口)是一组在Java应用中访问命名和目录服务的API. 命名服务将名称和对象联系起来,使得我们可以用 ...

  9. 【零基础学习iOS开发】【02-C语言】05-进制

    上一讲简单介绍了常量和变量,这讲补充一点计算机的基础知识---进制. 我们先来看看平时是如何表示一个整数的,最常见的肯定是用阿拉伯数字表示,比如“十二”,我们可以用12来表示,其实这种表示方式是基于一 ...

  10. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...