UVALive 5905 Pool Construction 最小割,s-t割性质 难度:3
这道题要求一种填充+挖坑+建屏障的方法,使得这块土地上的所有坑和草地之间都有屏障,挖坑花费d每块,填充花费f每块,建屏障花费b每两块之间,注意并不要求一定有坑,但是外围一圈要一定没有坑,所以需要预先填充好
令st代表平地,ed代表坑,边权为花费,那么本题是要求一个st-ed最小割,因为s到平地花费为0,所以不建边,同理坑到ed也不需要花费不建边,每个点到相邻点建边,容量为b,s到坑建边,容量为d,平地到e建边,容量为f,为了防止外围的平地被挖,容量设为inf.
以st为起点,ed为终点,求一遍最大流,即最小割即可
这道题一开始会以为是dp,其实是一道最小割,虽然有2500个点,50000条边左右,但是仍然可以用网络流跑过,一开始想不出怎么处理屏障,但是其实只需要把每个点和相邻点之间都连起来即可,横向的流量没有意义,也就是说只有平地和坑之间的边会限制流量.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=5000;
const int maxm=5000000;
const int inf=0x3fffffff;
int first[maxn];
struct edge{
int nxt,from,t,f,c;
}e[maxm];
int w,h,d,fl,b,len;
char maz[60][60];
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
int st,ed,num; int dis[maxn],gap[maxn];
bool in(int x,int y){return x>=0&&x<h&&y>=0&&y<w;}
void addedge(int from,int t,int c){
e[len].nxt=first[from];
e[len].from=from;
e[len].t=t;
e[len].f=c;
e[len].c=c;
first[from]=len++;
swap(from,t);c=0; e[len].nxt=first[from];
e[len].from=from;
e[len].t=t;
e[len].f=c;
e[len].c=c;
first[from]=len++;
}
int dfs(int s,int flow){
if(s==ed)return flow;
int mindis=num-1;
int tflow=flow,sub;
for(int p=first[s];p!=-1;p=e[p].nxt){
int t=e[p].t;
if(e[p].f>0){
if(dis[t]+1==dis[s]){
sub=dfs(t,min(tflow,e[p].f));
e[p].f-=sub;e[p^1].f+=sub;
tflow-=sub;
if(dis[st]>=ed)return flow-tflow;
if(tflow<=0)break;
}
mindis=min(mindis,dis[t]);
}
}
if(flow-tflow<=0){
--gap[dis[s]];
if(gap[dis[s]]==0)dis[st]=ed;
else {
dis[s]=mindis+1;
++gap[dis[s]];
}
}
return flow-tflow;
} int maxflow(){
int flow=0;
gap[0]=num;
while(dis[st]<ed){
flow+=dfs(st,inf);
}
return flow;
}
void init(){
len=0;
st=w*h;
ed=st+1;
num=st+2;
fill(first,first+num,-1);
fill(gap,gap+num,0);
fill(dis,dis+num,0);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d%d",&w,&h,&d,&fl,&b);
init();
int ans=0;
for(int i=0;i<h;i++)scanf("%s",maz[i]); for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
for(int k=0;k<4;k++){
int tx=i+dx[k],ty=j+dy[k];
if(in(tx,ty)){
addedge(i*w+j,tx*w+ty,b);
}
}
if(maz[i][j]=='#'){
if(i==0||i==h-1||j==0||j==w-1){
addedge(i*w+j,ed,inf);
}
else {
addedge(i*w+j,ed,d);
}
}
else {
if(i==0||i==h-1||j==0||j==w-1){
ans+=fl;
maz[i][j]='#';
addedge(i*w+j,ed,inf);
}
else {
addedge(st,i*w+j,fl);
}
}
}
}
ans+=maxflow();
printf("%d\n",ans);
}
return 0;
}
UVALive 5905 Pool Construction 最小割,s-t割性质 难度:3的更多相关文章
- uvalive 5905 Pool construction
题目链接 题意: 有一个花园,有些地方是草地,有些地方是洞,现在要在这个花园中修建一个泳池,泳池全部由洞构成. 把洞变成草地需要花费一定的费用,把草地变成洞也需要花费一定的费用,并且泳池的边缘的草地上 ...
- Uva -1515 Pool construction(最小割)
输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 首先把最外一圈的洞变成草,并累加花费. 增加一个源点和一个汇点,源 ...
- UVa1515 Pool construction(最小割)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVA 1515 Pool construction 最大流跑最小割
Pool construction You are working for the International Company for Pool Construction, a constructio ...
- 【uva 1515】Pool construction(图论--网络流最小割 模型题)
题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- UVa 1515 (最小割) Pool construction
题意: 输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 但要保证最外一圈是草,求最小费用. 分析: 还不是特别理解 ...
- UVA-1515 Pool construction (最小割)
题目大意:有一块地,分成nxm块.有的块上长着草,有的块上是荒地.将任何一块长着草的块上的草拔掉都需要花费d个力气,往任何一块荒地上种上草都需要花费f个力气,在草和荒地之间架一个篱笆需要花费b个力气, ...
- UVa 1515 - Pool construction(最小割)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- OCR识别-python版(一)
需求:识别图片中的文字信息环境:windows系统 开发语言:python 使用工具类:1.pyocr 2.PIL 3.tesseract-ocr 步骤: 1.pyocr 网络通直接使用命令:pip ...
- Python学习笔记11—函数
建立第一个函数 /usr/bin/env Python #coding:utf-8 def add_function(a,b): c = a+b print c if __name__==" ...
- tracert 命令详解
tracert 命令详解 How to Use the TRACERT Utility The TRACERT diagnostic utility determines the route to a ...
- DHCP 工作原理
DHCP 详解 DHCP 工作原理 一.什么是DHCP? DHCP,动态主机配置协议,前身是BOOTP协议,是一个局域网的网络协议,使用UDP协议工作,常用的2个端口:67(DHCP server), ...
- Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- spring.hibernate设置参数的位置问题
1.spring的注解设置在set方法上 2.hibernate的注解设置在get方法上
- Web服务器异常问题记录
1.使用命令,出现"-bash: 命令: Input/output error" 重启服务器后正常,网上查了下是说硬盘写入读取异常,经过和服务器厂商沟通后,确认是硬件问题导致(硬盘 ...
- EI中国
这里可以看到EI收录的所有的中国期刊:http://lib.sytu.edu.cn/files/zwwxx/2009-EI-china.htm
- 使用ICMP协议Ping网络主机
#coding:utf-8 #!/usr/bin/env python import os import argparse import socket import struct import sel ...
- python进阶
决定在python上有所突破 先看看知乎: 如何面试Python后端工程师? Python之美 - 知乎专栏 Python书籍推荐