lightoj 1243 - Guardian Knights 最小费用流
- #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 最小费用流的更多相关文章
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
G - Game of Hyper Knights Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & ...
- Lightoj 1010 - Knights in Chessboard
1010 - Knights in Chessboard PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...
- Lightoj 1010 - Knights in Chessboard (胡搞)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1010 题目描述: 有一个n*m的棋盘,根据象棋中马走日字的规则,问此棋盘最多 ...
- LightOJ - 1010 Knights in Chessboard(规律)
题目链接:https://vjudge.net/contest/28079#problem/B 题目大意:给你一个nxm的棋盘,问你最多可以放几个骑士让他们互相攻击不到.骑士攻击方式如下图: 解题思路 ...
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...
- lightoj 1010 (水题,找规律)
lightoj 1010 Knights in Chessboard 链接:http://lightoj.com/volume_showproblem.php?problem=1010 题意:国际象棋 ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
随机推荐
- Burp Suite Walkthrough
Burp Suite is one of the best tools available for web application testing. Its wide variety of featu ...
- WPF学习(二)布局与菜单、工具栏
布局 //表格①Grid//3列 4行的表格 <Grid> <Grid.ColumDefinitions> <ColumnDefinti ...
- Php会员权限
<?phpecho $uu=@array_sum(@$_POST['gr']);?><form action="" method="POST" ...
- 2016030102 - Ubuntu软件安装与删除相关命令
apt-get, dkpg 常用命令: 安装软件命令: apt-get install softname1 softname2 softname3…… 卸载软件命令: apt-get remove s ...
- 通过jquery获取后台传过来的值进行全选
注:funs是从action中传过来的list<Function> 其中属性中有其子对象list<role> 下面通过s标签遍历 ,也可以通过c标签遍历 jsp页面中: < ...
- ping命令找不到
重装系统后安装JDK了,网络一直不好,我ping了下,结果显示ping不是内部或者外部命令,在谷歌里百度了下,在环境变量的path后加上“;C:\Windows\System32”即可,果然有效哦. ...
- c printf
printf的格式控制的完整格式:% - 0 m.n l或h 格式字符下面对组成格式说明的各项加以说明:①%:表示格式说明的起始符号,不可缺少.②-:有-表示左对齐输出,如省略表示右对齐输出.③0:有 ...
- 深入了解一下PYTHON中关于SOCKETSERVER的模块-A
有了这块知识,应该对各类WEB框架有更好的理解吧..FLASK,DJANGO,WEBPY.... #!/usr/bin/env python from BaseHTTPServer import HT ...
- 【UVA11019】Matrix Matcher
Description Given an N × M matrix, your task is to find the number of occurences of an X × Y pattern ...
- 【POJ2417】baby step giant step
最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...