【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 ...
随机推荐
- javascript对象如何使用
javascript对象如何使用 一.总结 一句话总结:JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... 因为函数是对象,所以自定义对象的创建中有种方法就是函数 1.js中的 ...
- Linux 内核源码(kernel source)
查看内核的发行版:uname -r(--kernel-release) $ uname -r 4.4.0-78-generic 内核源码所在的位置:/usr/src $ cd /usr/src $ l ...
- 面向对象 —— 对类(class)的理解
类是成员变量和成员函数的封装,封装的一个重要功能就是可见性(继承除外,当然继承是面向对象的另外一个重要特性),所谓可见性,类内可见,类外不可见.可见性保证了类型安全(type-safe) 对类进行实例 ...
- export和source的区别
1.执行脚本是在一个子shell环境运行的,脚本执行完后该子shell自动退出. 2.执行脚本中的系统环境变量(用export定义的变量)才会被复制到子shell中. 3.一个shell中的系统环境变 ...
- Linux安全应用之防垃圾邮件服务器的构建
Linux安全应用之防垃圾邮件服务器的构建 一.垃圾邮件产生的原因 垃圾邮件(SPAM) 也称作UCE(Unsoticited Commercial Email.未经许可的商业电子邮件)或UBE(Un ...
- 重装python 和 yum
https://blog.csdn.net/ghostyusheng/article/details/https://segmentfault.com/q/1010000009194060/a-102 ...
- 编程里的API是什么意思?
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码 ...
- Tomcat 的三种高级运行模式
Tomcat 的连接器有两种:HTTP和AJP AJP(Apache JServ Protocol):AJP是面向数据包的基于TCP/IP的协议,它在Apache和Tomcat的实例之间提供了一个专用 ...
- Python socket doesn't close connection properly
Python socket doesn't close connection properly The error information: [Errno 98] Address already in ...
- effective stl读书笔记 & stl里面提供的算法 & emplace & ostream_iterator
加锁和解锁,也可以在构造函数和析构函数里面,自动调用. 相等和等价的关系:等价是用在排序的时候,跟less函数有关. vector,deque,string 要用erase-remove组合:而关联容 ...