【hdu 1533】Going Home
【链接】http://acm.hdu.edu.cn/showproblem.php?pid=1533
【题意】
【题解】
则这个时候跑一下最小费用最大流就好.
【错的次数】
【反思】
【代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 100;
const int NN = 3e4;
const int INF = 0x3f3f3f3f; struct abc{
int from,en,flow,nex,cost;
}; int n,m,totm,fir[2*N+50],dis[N*2+50],pre[2*N+50],mi[2*N+50],ans;
char s[N+10][N+10];
vector <pii> H,M;
abc bian[NN];
bool inq[2*N+50];
queue <int> dl; void add(int x,int y,int flow,int cost){
bian[totm].nex = fir[x];
fir[x] = totm;
bian[totm].from = x;
bian[totm].en = y;
bian[totm].cost = cost;
bian[totm].flow = flow;
totm++; bian[totm].nex = fir[y];
fir[y] = totm;
bian[totm].from = y;
bian[totm].en = x;
bian[totm].cost = -cost;
bian[totm].flow = 0;
totm++;
} bool spfa(int s,int t){
ms(dis,INF),ms(inq,0),ms(mi,INF);
dis[s] = 0,inq[s] = 1;
dl.push(s);
pre[t] = -1;
while (!dl.empty()){
int x = dl.front();
inq[x] = false;
dl.pop();
for (int i = fir[x];i!=-1;i = bian[i].nex){
int y = bian[i].en;
if (bian[i].flow && dis[y] > dis[x] + bian[i].cost){
dis[y] = dis[x] + bian[i].cost;
mi[y] = min(bian[i].flow,mi[x]);
pre[y] = i;
if (!inq[y]){
inq[y] = true;
dl.push(y);
}
}
}
}
if (pre[t]==-1) return false;
int now = t;
while (now != s){
int temp = pre[now];
bian[temp].flow -= mi[t];
bian[temp^1].flow += mi[t];
now = bian[temp].from;
ans += mi[t]*bian[temp].cost;
}
return true;
} int main(){
//Open();
//Close();
while (~ri(n)){
ans = 0;
ri(m);
if (n == 0 && m == 0) break;
rep1(i,0,n-1)
rs(s[i]);
H.clear(),M.clear();
rep1(i,0,n-1)
rep1(j,0,m-1)
if (s[i][j]=='H')
H.pb(mp(i,j));
else if (s[i][j]=='m')
M.pb(mp(i,j));
totm = 0;
ms(fir,255);
int len1 = H.size(),len2 = M.size();
rep1(i,0,len1-1) add(0,i+1,1,0);
rep1(i,0,len2-1) add(i+1+len1,len1+len2+1,1,0);
rep1(i,0,len1-1)
rep1(j,0,len2-1)
add(i+1,len1+j+1,1,abs(M[j].fi-H[i].fi)+abs(M[j].se-H[i].se));
while (spfa(0,len1+len2+1));
oi(ans);puts("");
}
return 0;
}
【hdu 1533】Going Home的更多相关文章
- 【HDU 1533】 Going Home (KM)
Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- Android 仿QQ首页的消息和电话的切换,首页的头部(完全用布局控制)
Android 仿QQ首页的消息和电话的切换,首页的头部(完全用布局控制) 首先贴上七个控制布局代码 1.title_text_sel.xml 字体颜色的切换 放到color文件夹下面 <?xm ...
- HTTP 与 HTTPS
https就是http和TCP之间有一层SSL层,这一层的实际作用是防止钓鱼和加密. 防止钓鱼通过网站的证书,网站必须有CA证书,证书类似于一个解密的签名. 另外是加密,加密需要一个密钥交换算法,双方 ...
- Bootstrap概览
摘选自runnoob.com,bootstrap4所有api概览: 一.安装: 弹性盒子(flexbox) Bootstrap 3 与 Bootstrap 4 最大的区别就是 Bootstrap 4 ...
- Vuejs2.0构建一个彩票查询WebAPP(3)
整个工程的目录及截图如下,源码下载 使用心得: 1.了解Vue的生命周期很有必要,详情参见博文Vue2.0 探索之路——生命周期和钩子函数的一些理解 2.Vuex全局状态管理真是美味不可言 st ...
- 搭建专属于自己的Leanote云笔记本
搭建专属于自己的Leanote云笔记本 Leanote 依赖 MongoDB 作为数据存储,下面开始安装 MongoDB: 下载 MongoDB 进入 /home 目录,并下载 MongoDB: cd ...
- Ubuntu 16.04安装mysql (连接)
1.安装mysql sudo apt-get install mysql-server 注:若出现依赖问题安装失败,先执行以下命令sudo apt-get install -f 2.安装过程中会输入密 ...
- C# INotifyPropertyChanged 实现
INotifyPropertyChanged: 该接口包含一个事件, 针对属性发生变更时, 执行该事件发生. // // 摘要: // 通知客户端属性值已更改. public interface IN ...
- iOS项目开发实战——学会使用TableView列表控件(二)
要在iOS开发中使用TableView列表控件,不仅能够直接使用TableViewController作为整个主界面,并且还能够使用TableView控件来实现.使用TableView能够进行很多其它 ...
- #学习笔记#——JavaScript 数组部分编程(二)
2.移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 function removeWithoutCopy(arr, item) { if(!A ...
- 微信小程序从零开始开发步骤(七)引入外部js 文件
上一章讲到小程序页面的四种常见的跳转的方法,这一章写如何引入一个外部的js文件,既utils文件夹的用处,其实步骤很简单: 1:准备好外部想要引入的外部文件,命名为util.js,并且填充固定的文件内 ...