1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <vector>
  8. using namespace std;
  9.  
  10. const int maxe = ;
  11. const int maxn = ;
  12. const int INF = 0x3f3f3f3f;
  13.  
  14. struct Edge{
  15. int u,v,flow,cap,cost;
  16. int next;
  17. Edge(int u=,int v=,int flow=,int cap=,int cost=,int next=):
  18. u(u), v(v), flow(flow), cap(cap), cost(cost), next(next){}
  19. };
  20.  
  21. struct MCMF{
  22. Edge edges[maxe];
  23. int head[maxn],cnt;
  24. int d[maxn];
  25. bool inq[maxn];
  26. int pa[maxn];
  27. int res[maxn];
  28.  
  29. void init(){
  30. memset(head,-,sizeof(head));
  31. cnt = ;
  32. }
  33.  
  34. void addedge(int u,int v,int cap,int cost){
  35. edges[cnt] = Edge(u,v,,cap,cost,head[u]);
  36. head[u] = cnt++;
  37. edges[cnt] = Edge(v,u,,,-cost,head[v]);
  38. head[v] = cnt++;
  39. }
  40.  
  41. bool SPFA(int s,int t,int& flow,int& cost){
  42. memset(d,0x3f,sizeof(d));
  43. memset(inq,,sizeof(inq));
  44.  
  45. queue<int> Q;
  46. Q.push(s);
  47. inq[s] = true; d[s] = ;
  48. res[s] = INF; res[t] = ;
  49.  
  50. while(!Q.empty()){
  51. int u = Q.front(); Q.pop();
  52. inq[u] = false;
  53.  
  54. for(int i=head[u];i!=-;i=edges[i].next){
  55. Edge& e = edges[i];
  56. if(e.cap>e.flow && d[e.v] > d[u] + e.cost){
  57. d[e.v] = d[u] + e.cost;
  58. pa[e.v] = i;
  59. res[e.v] = min(res[u],e.cap-e.flow);
  60. if(!inq[e.v]){
  61. Q.push(e.v);
  62. inq[e.v] = true;
  63. }
  64. }
  65. }
  66. }
  67. if(res[t] == ) return false;
  68. flow += res[t];
  69. cost += res[t]*d[t];
  70. for(int i=t;i!=s;i=edges[pa[i]].u){
  71. edges[pa[i]].flow += res[t];
  72. edges[pa[i]^].flow -= res[t];
  73. }
  74. return true;
  75. }
  76.  
  77. int MinCost(int s,int t){
  78. int flow = ,cost = ;
  79. while(SPFA(s,t,flow,cost)){}
  80.  
  81. return cost;
  82. }
  83. }solver;
  84.  
  85. struct Node{
  86. int x,y;
  87. int dist;
  88. Node(int x=,int y=,int dist = ): x(x), y(y) ,dist(dist) {}
  89. };
  90. Node Knight[];
  91. int n,k,m;
  92. int dist[][];
  93.  
  94. int Mymap[][]; // == 0代表rock,-1代表space ,1-m代表mill.
  95. int next_x[] = {-,,,};
  96. int next_y[] = {,-,,};
  97.  
  98. void bfs(){
  99. bool vis[][];
  100. memset(dist,0x3f,sizeof(dist));
  101. queue<Node> Q;
  102. for(int i=;i<=k;i++){
  103. while(!Q.empty()) Q.pop();
  104. Q.push(Node(Knight[i].x,Knight[i].y,));
  105.  
  106. memset(vis,,sizeof(vis));
  107. vis[Knight[i].x][Knight[i].y] = true;
  108.  
  109. while(!Q.empty()){
  110. Node out = Q.front(); Q.pop();
  111. for(int j=;j<;j++){
  112. int x = out.x + next_x[j];
  113. int y = out.y + next_y[j];
  114. if(vis[x][y]) continue;
  115. vis[x][y] = true;
  116. if(Mymap[x][y] == -){
  117. Q.push(Node(x,y,out.dist+));
  118. }
  119. else if(Mymap[x][y]>){
  120. dist[i][Mymap[x][y]] = out.dist+;
  121. Q.push(Node(x,y,out.dist+));
  122. }
  123. }
  124. }
  125. }
  126. }
  127.  
  128. int main()
  129. {
  130. //freopen("E:\\acm\\input.txt","r",stdin);
  131. int T;
  132. cin>>T;
  133. for(int cas=;cas<=T;cas++){
  134. cin>>n>>k>>m;
  135. char ch[];
  136. memset(Mymap,,sizeof(Mymap));
  137. int cntm = ;
  138. for(int i=;i<=n;i++){
  139. scanf("%s",ch+);
  140. for(int j=;j<=n;j++){
  141. if(ch[j]>='A' && ch[j]<='Z'){
  142. int num = ch[j] - 'A' + ;
  143. Knight[num] = Node(i,j,);
  144. Mymap[i][j] = -;
  145. }
  146. else if(ch[j] == 'm'){
  147. Mymap[i][j] = cntm++;
  148. }
  149. else if(ch[j] == '.'){
  150. Mymap[i][j] = -;
  151. }
  152. }
  153. }
  154. bfs();
  155. solver.init();
  156. int s = , t = k+m+;
  157. for(int i=;i<=k;i++){
  158. int a;
  159. scanf("%d",&a);
  160. solver.addedge(s,i,a,);
  161. }
  162. for(int i=;i<=k;i++)
  163. for(int j=;j<=m;j++){
  164. solver.addedge(i,k+j,,dist[i][j]);
  165. }
  166.  
  167. for(int i=;i<=m;i++){
  168. solver.addedge(k+i,t,,);
  169. }
  170.  
  171. printf("Case %d: %d\n",cas,solver.MinCost(s,t));
  172. }
  173. }

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 (胡搞)

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

  4. LightOJ - 1010 Knights in Chessboard(规律)

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

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

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

  6. lightoj 1010 (水题,找规律)

    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[点双连通分量|二分图染色|补图]

    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. Burp Suite Walkthrough

    Burp Suite is one of the best tools available for web application testing. Its wide variety of featu ...

  2. WPF学习(二)布局与菜单、工具栏

    布局 //表格①Grid//3列 4行的表格   <Grid>    <Grid.ColumDefinitions>             <ColumnDefinti ...

  3. Php会员权限

    <?phpecho $uu=@array_sum(@$_POST['gr']);?><form action="" method="POST" ...

  4. 2016030102 - Ubuntu软件安装与删除相关命令

    apt-get, dkpg 常用命令: 安装软件命令: apt-get install softname1 softname2 softname3…… 卸载软件命令: apt-get remove s ...

  5. 通过jquery获取后台传过来的值进行全选

    注:funs是从action中传过来的list<Function> 其中属性中有其子对象list<role> 下面通过s标签遍历 ,也可以通过c标签遍历 jsp页面中: < ...

  6. ping命令找不到

    重装系统后安装JDK了,网络一直不好,我ping了下,结果显示ping不是内部或者外部命令,在谷歌里百度了下,在环境变量的path后加上“;C:\Windows\System32”即可,果然有效哦. ...

  7. c printf

    printf的格式控制的完整格式:% - 0 m.n l或h 格式字符下面对组成格式说明的各项加以说明:①%:表示格式说明的起始符号,不可缺少.②-:有-表示左对齐输出,如省略表示右对齐输出.③0:有 ...

  8. 深入了解一下PYTHON中关于SOCKETSERVER的模块-A

    有了这块知识,应该对各类WEB框架有更好的理解吧..FLASK,DJANGO,WEBPY.... #!/usr/bin/env python from BaseHTTPServer import HT ...

  9. 【UVA11019】Matrix Matcher

    Description Given an N × M matrix, your task is to find the number of occurences of an X × Y pattern ...

  10. 【POJ2417】baby step giant step

    最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...