https://vjudge.net/problem/UVA-12264

题意:

有很多个阵地,分为敌方和己方,每个士兵可以移动到相邻的己方的阵地,但是只能移动一步。

现在要让与敌方相邻的阵地中士兵最少的最多。

思路:

最少的最多,那显然二分。

二分这个最少的值。拆点,敌方阵地不管,S向己方阵地\(i\)向连边,容量为本阵地士兵的数量,\(i'\)向T连边,如果是与敌方相邻的阵地,那么容量为二分的值;如果是处于己方阵地的包围,那么容量为1即可。最后跑最大流判断是否满流。

STD:

本STD在uva上AC,uvalive上WA,请谨慎食用。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 205;
int a[N];
char s[N][N];
bool is[N]; struct edge
{
int u,v,cap;
edge(int u,int v,int cap):u(u),v(v),cap(cap){}
edge(){}
}; vector<edge> es;
vector<int> G[N];
int n,S,T;
int dis[N],cur[N]; void adde(int u,int v,int cap)
{
es.push_back(edge(u,v,cap));
es.push_back(edge(v,u,0));
int sz = es.size();
G[u].push_back(sz-2);
G[v].push_back(sz-1);
} bool bfs()
{
memset(dis,inf,sizeof dis);
dis[S] = 0;
queue<int> q;
q.push(S);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = 0;i < G[u].size();i++)
{
edge &e = es[G[u][i]];
int v = e.v;
if (e.cap > 0 && dis[v] >= inf)
{
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
return dis[T] < inf;
} int dfs(int u,int flow)
{
if (u == T) return flow;
for (int i = cur[u];i < G[u].size();i++)
{
cur[u] = i;
edge &e = es[G[u][i]];
int v = e.v;
if (dis[v] == dis[u] + 1 && e.cap > 0)
{
int tmp = dfs(v,min(e.cap,flow));
if (tmp)
{
e.cap -= tmp;
es[G[u][i]^1].cap += tmp;
return tmp;
}
}
}
return 0;
} int dinic()
{
int ans = 0;
while (bfs())
{
int tmp;
memset(cur,0,sizeof(cur));
while (tmp = dfs(S,inf)) ans += tmp;
}
return ans;
} bool meet(int lim)
{
for (int i = 0;i < N;i++) G[i].clear();
es.clear();
for (int i = 1;i <= n;i++)
{
if (a[i] <= 0) continue;
adde(S,i,a[i]);
adde(i,i+n,inf);
}
int sum = 0;
for (int i = 1;i <= n;i++)
{
if (a[i] <= 0) continue;
if (is[i])
{
sum += lim;
adde(i+n,T,lim);
}
else
{
sum++;
adde(i+n,T,1);
}
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (i == j) continue;
if (a[i] <= 0 || a[j] <= 0) continue;
if (s[i][j] == 'Y')
{
adde(i,j+n,inf);
}
}
}
int ans = dinic();
return ans >= sum;
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
memset(is,0,sizeof is);
scanf("%d",&n);
S = 0;
T = 2 * n + 1;
for (int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 1;i <= n;i++)
{
scanf("%s",s[i]+1);
}
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (a[i] > 0 && a[j] <= 0)
{
if (s[i][j] == 'Y') is[i] = true;
}
}
}
int l = 1,r = 1e5;
while (r - l > 1)
{
int mid = (l + r) >> 1;
if (meet(mid)) l = mid;
else r = mid;
}
while (meet(l+1)) l++;
printf("%d\n",l);
}
return 0;
} /*
3
1 1 0
NYN
YNY
NYN
7
7 3 3 2 0 0 5
NYNNNNN
YNYYNNN
NYNYYNN
NYYNYNN
NNYYNNN
NNNNNNY
NNNNNYN
4
2 2 0 0
NNYN
NNNY
YNNN
NYNN
*/

uva 12264 Risk的更多相关文章

  1. UVA - 12264 Risk (二分,网络流)

    题意比较坑,移动完以后的士兵不能再次移动,不然样例都过不了... 最小值最大满足决策单调性所以二分答案,跑网络流验证是否可行. 这种题重点在建图,为了保证只移动一次,拆点,一个入点一个出点,到了出点的 ...

  2. Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。

    /** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...

  3. 紫书 习题 11-10 UVa 12264 (二分答案+最大流)

    书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...

  4. UVa 567: Risk

    这是一道很简单的图论题,只要使用宽度优先搜索(BFS)标记节点间距离即可. 我的解题代码如下: #include <iostream> #include <cstdio> #i ...

  5. UVA 567 Risk【floyd】

    题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=508">https://uva ...

  6. LA 3353 最优巴士线路设计

    给出一个 n 个点的有向图,找若干个圈,是的每个结点恰好属于一个圈.要求总长度尽量小. 三倍经验题 Uva 12264,HDU 1853 这题有两种解法,一是匹配: 每个点只在一个圈中,则他有唯一的前 ...

  7. uva oj 567 - Risk(Floyd算法)

    /* 一张有20个顶点的图上. 依次输入每个点与哪些点直接相连. 并且多次询问两点间,最短需要经过几条路才能从一点到达另一点. bfs 水过 */ #include<iostream> # ...

  8. UVA - 1025 A Spy in the Metro[DP DAG]

    UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...

  9. UVa12264 Risk(最大流)

    题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

随机推荐

  1. UUID相同导致的网络连接问题

    目录 场景 思路 解决过程 提升虚拟机配置 直连交换机 最终解决方案 总结 场景 有同事从公司寄了一台服务器到现场,用来安装数据库.缓存等组件供开发使用.到了之后,连接电源.网线,设置IP,用vSph ...

  2. Spring Cloud health节点通过注册中心扫描状态的简单实现

    package com.zjs.web; import com.netflix.appinfo.InstanceInfo; import com.zjs.FallbackApiApplication; ...

  3. 【miscellaneous】单播、广播和多播IP地址

    转自:http://www.cnblogs.com/gaoxing/archive/2012/02/19/2358484.html 除地址类别外,还可根据传输的消息特征将IP地址分为单播.广播或多播. ...

  4. springboot整合es客户端操作elasticsearch(二)

    在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采 ...

  5. Cookie中的httponly的属性和作用

    1.什么是HttpOnly? 如果cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,窃取cookie内容,这样就增加了cookie的安 ...

  6. 查询统计SQL分组求和使用小技巧

    我们在做查询统计时,肯定会遇到将查询结果再次分组求和这种需求,但是往往查询的sql本身就比较复杂,再使用分组函数不太可能,那么这时候我们就想到了用临时表的办法,通过联合临时表我们就可以获得想要的分组求 ...

  7. The import javax.websocket cannot be resolved的解决问题

    在eclipse中导入项目的时候出现了这个问题,废了我半天劲,才搞明白,把问题记录下来,方便大家以后遇到这问题好处理.提供参考. 出现的问题截图: 因为我用的是tomcat8, 大体步骤:项目上点右键 ...

  8. PAT B1006 换个格式输出整数 (15)

    AC代码 #include <cstdio> const int max_n = 3; char radix[max_n] = {' ', 'S', 'B'}; int ans[max_n ...

  9. 切换PHP版本导致Phalcon拓展失效

    公司使用PHP的Phalcon框架做后端.Phalcon最大的特点就是底层由C语言实现,使用Phalcon框架必须要安装php_phalcon.dll扩展.之前的是使用PHP7.2的版本,后面考虑性能 ...

  10. 【求教 探讨】python tkinter的messagebox

    最近有一个要求,用python的tkinter制作一个messagebox,传入3个参数: title  text timeout.用户可以点击“确定” 关闭窗口:  或者 等待几秒(timeout) ...