lightoj 1243 - Guardian Knights 最小费用流

时间:2022-08-28 00:25:41
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std; const int maxe = ;
const int maxn = ;
const int INF = 0x3f3f3f3f; struct Edge{
int u,v,flow,cap,cost;
int next;
Edge(int u=,int v=,int flow=,int cap=,int cost=,int next=):
u(u), v(v), flow(flow), cap(cap), cost(cost), next(next){}
}; struct MCMF{
Edge edges[maxe];
int head[maxn],cnt;
int d[maxn];
bool inq[maxn];
int pa[maxn];
int res[maxn]; void init(){
memset(head,-,sizeof(head));
cnt = ;
} void addedge(int u,int v,int cap,int cost){
edges[cnt] = Edge(u,v,,cap,cost,head[u]);
head[u] = cnt++;
edges[cnt] = Edge(v,u,,,-cost,head[v]);
head[v] = cnt++;
} bool SPFA(int s,int t,int& flow,int& cost){
memset(d,0x3f,sizeof(d));
memset(inq,,sizeof(inq)); queue<int> Q;
Q.push(s);
inq[s] = true; d[s] = ;
res[s] = INF; res[t] = ; while(!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = false; for(int i=head[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(e.cap>e.flow && d[e.v] > d[u] + e.cost){
d[e.v] = d[u] + e.cost;
pa[e.v] = i;
res[e.v] = min(res[u],e.cap-e.flow);
if(!inq[e.v]){
Q.push(e.v);
inq[e.v] = true;
}
}
}
}
if(res[t] == ) return false;
flow += res[t];
cost += res[t]*d[t];
for(int i=t;i!=s;i=edges[pa[i]].u){
edges[pa[i]].flow += res[t];
edges[pa[i]^].flow -= res[t];
}
return true;
} int MinCost(int s,int t){
int flow = ,cost = ;
while(SPFA(s,t,flow,cost)){} return cost;
}
}solver; struct Node{
int x,y;
int dist;
Node(int x=,int y=,int dist = ): x(x), y(y) ,dist(dist) {}
};
Node Knight[];
int n,k,m;
int dist[][]; int Mymap[][]; // == 0代表rock,-1代表space ,1-m代表mill.
int next_x[] = {-,,,};
int next_y[] = {,-,,}; void bfs(){
bool vis[][];
memset(dist,0x3f,sizeof(dist));
queue<Node> Q;
for(int i=;i<=k;i++){
while(!Q.empty()) Q.pop();
Q.push(Node(Knight[i].x,Knight[i].y,)); memset(vis,,sizeof(vis));
vis[Knight[i].x][Knight[i].y] = true; while(!Q.empty()){
Node out = Q.front(); Q.pop();
for(int j=;j<;j++){
int x = out.x + next_x[j];
int y = out.y + next_y[j];
if(vis[x][y]) continue;
vis[x][y] = true;
if(Mymap[x][y] == -){
Q.push(Node(x,y,out.dist+));
}
else if(Mymap[x][y]>){
dist[i][Mymap[x][y]] = out.dist+;
Q.push(Node(x,y,out.dist+));
}
}
}
}
} int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
for(int cas=;cas<=T;cas++){
cin>>n>>k>>m;
char ch[];
memset(Mymap,,sizeof(Mymap));
int cntm = ;
for(int i=;i<=n;i++){
scanf("%s",ch+);
for(int j=;j<=n;j++){
if(ch[j]>='A' && ch[j]<='Z'){
int num = ch[j] - 'A' + ;
Knight[num] = Node(i,j,);
Mymap[i][j] = -;
}
else if(ch[j] == 'm'){
Mymap[i][j] = cntm++;
}
else if(ch[j] == '.'){
Mymap[i][j] = -;
}
}
}
bfs();
solver.init();
int s = , t = k+m+;
for(int i=;i<=k;i++){
int a;
scanf("%d",&a);
solver.addedge(s,i,a,);
}
for(int i=;i<=k;i++)
for(int j=;j<=m;j++){
solver.addedge(i,k+j,,dist[i][j]);
} for(int i=;i<=m;i++){
solver.addedge(k+i,t,,);
} printf("Case %d: %d\n",cas,solver.MinCost(s,t));
}
}

lightoj 1243 - Guardian Knights 最小费用流的更多相关文章

  1. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...

  2. Lightoj 1010 - Knights in Chessboard

    1010 - Knights in Chessboard    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...

  3. Lightoj 1010 - Knights in Chessboard &lpar;胡搞&rpar;

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1010 题目描述: 有一个n*m的棋盘,根据象棋中马走日字的规则,问此棋盘最多 ...

  4. LightOJ - 1010 Knights in Chessboard&lpar;规律&rpar;

    题目链接:https://vjudge.net/contest/28079#problem/B 题目大意:给你一个nxm的棋盘,问你最多可以放几个骑士让他们互相攻击不到.骑士攻击方式如下图: 解题思路 ...

  5. LightOJ1171 Knights in Chessboard &lpar;II&rpar;(二分图最大点独立集)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...

  6. lightoj 1010 &lpar;水题,找规律&rpar;

    lightoj 1010 Knights in Chessboard 链接:http://lightoj.com/volume_showproblem.php?problem=1010 题意:国际象棋 ...

  7. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  8. POJ2942 Knights of the Round Table&lbrack;点双连通分量&vert;二分图染色&vert;补图&rsqb;

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Acce ...

  9. POJ 2942 Knights of the Round Table

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10911   Acce ...

随机推荐

  1. windows系统和ubuntu虚拟机之间文件共享——samba

    参考:http://www.cnblogs.com/phinecos/archive/2009/06/06/1497717.html 一. samba的安装: sudo apt-get insall  ...

  2. 云计算P2V的迁移过程

    总结一下客户在传统物理机向虚拟化环境迁移中的典型场景和常用工具及步骤.

  3. 难得的中文ASP&period;NET 5&sol;MVC 6入门教程

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于ASP.NET 5还未正式发布,即使是官方文档都还不完善,更不要说系统的中文文档了 ...

  4. 5&period;Firedac错误信息

    主要错误信息属性: 1.EFDDBEngineException Errors -- TFDDBError对象集合. ErrorCount --错误的记录数 Kind -- DBMS的错误集合. Me ...

  5. 转 Citrix XenCenter安装VM之挂载ISO详解

    转自:http://www.2cto.com/os/201302/190713.html 环境信息:XenServer Version:6.0.2XenCenter Version:6.0.2NFS ...

  6. 百度图片爬虫-python版-如何爬取百度图片&quest;

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  7. Oracle数据库中的Function调用参数问题

    在工作中用到了Oracle数据库,需要调用Oracle的Function,Function返回的游标和结果都是通过参数来获取的 比如Function定义如下: , intype, ininttype) ...

  8. celery expires 让celery任务具有时效性

    起因:有的时候.我们希望任务具有时效性.比方定时每5分钟去抓取某个状态,由于celery队列中的任务可能非常多,等到这个任务被运行时.已经超过了5分钟,那么这个任务的运行已经没有意义.由于下一次抓取已 ...

  9. IOS--UIAlertView的使用方法详细

    IOS--UIAlertView的使用方法详细   // UIAlertView的常用方法 // 标准样式 UIAlertView *oneAlertView = [[UIAlertView allo ...

  10. pytest 14 使用自定义标记mark

    标记失败用到的情况是,本身就知道这是失败的例子,所以,不用让他运行,直接跳过.或者是依赖于某个方法,某个方式失败的话,用例直接标记成失败. 标记失败有两种方法,一种是方法内部,一种是方法外部.内部用p ...