uva12264 Risk
最小值最大,就二分判断。
map[i] = '0'+map[i];这样更方便
每个点拆成i,i’, S连i,cap为a[i],i’连T,cap为1(保证至少剩一个)或mid。
i,i’ ,a[i]
i->j’, inf //把i连到j就WA了...所以题目的意思大概是只能移动到相邻点?
判断一下border
#include<iostream>
#include<queue>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = , INF = 0x3f3f3f3f;
int a[maxn], border[maxn];
string map[maxn];
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
};
struct EdmondsKarp
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn];
int p[maxn]; void init(int n)
{
this->n = n;
for (int i = ; i < n; i++)
{
G[i].clear();
}
edges.clear();
memset(p, , sizeof(p));//²»ÐèÒª£¿
}
void AddEdge(int u, int v, int c)
{
edges.push_back(Edge(u, v, c, ));
edges.push_back(Edge(v, u, , ));
m = edges.size();
G[u].push_back(m - );
G[v].push_back(m - );
}
int MaxFlow(int s, int t)
{
int flow = ;
for (;;)
{
memset(a, , sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if (!a[e.to] && e.cap > e.flow)
{
a[e.to] = min(a[u], e.cap - e.flow);
Q.push(e.to);
p[e.to] = G[u][i];
}
}
if (a[t])
break;
}
if (!a[t])
break;
for (int i = t; i != s; i = edges[p[i]].from)
{
edges[p[i]].flow += a[t];
edges[p[i] ^ ].flow -= a[t];
}
flow += a[t];
}
return flow;
}
}E; int Build(int val,int N)
{
for (int i = ; i <= N; i++)
{
if (!a[i])
continue;
E.AddEdge(, i, a[i]);
E.AddEdge(i, i + N, a[i]);
for (int j = ; j <= N; j++)
if (map[i][j] == 'Y')
{
if (!a[j])
border[i] = true; //border
else
E.AddEdge(i, j + N, INF);
}
}
int ans = ;
for (int i = ; i <= N; i++)
if (border[i])
{
E.AddEdge(i + N, * N + , val);
ans += val;
}
else if (a[i])
{
E.AddEdge(i + N, * N + , );
ans++;
};
return ans;
}
void solve(int n)
{
int a, b, ans;
int l = , r = ;
while (l<r)
{
E.init( * n + );
int mid = l+(r-l)/;
a = Build(mid, n);
b = E.MaxFlow(, * n + );
if (a == b)
{
l = mid + ;
ans = mid;
}
else r = mid;
}
cout << ans << endl;
} void input(int n){
memset(a, , sizeof(a));
memset(border, , sizeof(border));
for (int i = ; i < maxn; i++)
map[i].clear(); for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++)
{
cin >> map[i];
map[i] = ''+map[i];
}
} int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
input(n);
solve(n);
}
return ;
}
uva12264 Risk的更多相关文章
- UVa12264 Risk(最大流)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。
/** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...
- PHP Datatype Conversion Safety Risk、Floating Point Precision、Operator Security Risk、Safety Coding Principle
catalog . 引言 . PHP operator introduction . 算术运算符 . 赋值运算符 . 位运算符 . 执行运算符 . 递增/递减运算符 . 数组运算符 . 类型运算符 . ...
- Linux fork()、exec() Hook Risk、Design-Principle In Multi-Threadeed Program
目录 . Linux exec指令执行监控Hook方案 . 在"Multi-Threadeed Program"环境中调用fork存在的风险 . Fork When Multi-T ...
- Threat Risk Modeling Learning
相关学习资料 http://msdn.microsoft.com/en-us/library/aa302419(d=printer).aspx http://msdn.microsoft.com/li ...
- Stakeholder Risk Management
In this article we'll address the people swirling around your project: stakeholders. You'll find som ...
- 10 Golden Rules of Project Risk Management
The benefits of risk management in projects are huge. You can gain a lot of money if you deal with u ...
- zoj 1221 Risk Flory
博客开了快半年了- -学习编程也快1年半了,觉得空空的不太好看,刚好最近开始练习ACM了,就来做一个简单的ACM学习笔记吧,纪念的第一题zol 1221 Risk 风险游戏(个人觉得是这样翻- -翻译 ...
- UVa 567: Risk
这是一道很简单的图论题,只要使用宽度优先搜索(BFS)标记节点间距离即可. 我的解题代码如下: #include <iostream> #include <cstdio> #i ...
随机推荐
- 割点(Tarjan算法)
本文可转载,转载请注明出处:www.cnblogs.com/collectionne/p/6847240.html .本文未完,如果不在博客园(cnblogs)发现此文章,请访问以上链接查看最新文章. ...
- Android的文件读取与存储
Java新建文件,然后就可以写入数据了,但是Android却不一样,因为Android是 基于Linux的,我们在读写文件的时候,还需加上文件的操作模式 Environment类是一个提供访问环境变量 ...
- VC 中TEXT、_T、L的区别
http://i.cnblogs.com/EditPosts.aspx?opt=1 对于从VC++6.0转到VS2005编译环境中的程序员.往往会碰到字符集之间的转换. VC6.0采用的是ANSI字符 ...
- EM算法(徐亦达)笔记
- Codeforces Round #396 (Div. 2) E
Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirec ...
- JSDOM获取子节点的一些方法
一般情况获取子节点,通过找到查找父节点的ID或者class类名,来获取父节点,再通过children属性,得到子节点的数组: 之前在另外一篇随笔中说过,如果使用另一个属性childNode,会把注释. ...
- base64 正则表达式 ,判断图片是base64还是图片链接
base64正则表达式 在这里看到https://segmentfault.com/q/1010000009628242/a-1020000009629647 var reg = /^\s*data: ...
- codeforces727C(交互)
题意 $n$个数,初始时不知道他们的值. 每次可以询问两个数的和,在$n$次询问内确定他们的值 $n \leqslant 5000$ Sol 首先询问出$1, 2$,$1, 3$,$2, 3$ 解个方 ...
- jQuery源码分析系列(转载来源Aaron.)
声明:非本文原创文章,转载来源原文链接Aaron. 版本截止到2013.8.24 jQuery官方发布最新的的2.0.3为准 附上每一章的源码注释分析 :https://github.com/JsAa ...
- 一张图告诉你,只会这些HTML还远远不够!!!!!
不知道自己HTML水平如何,不知道HTML5如何进化?看这张图 如果一半以上的你都不会,必须看这本书,阿里一线工程师用代码和功能页面来告诉你每一个技术点. 都会一点,但不知道如何检验自己,看看本书提供 ...