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 ...
随机推荐
- Several ports (8005, 8080, 8009) required
Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The ...
- linux tar命令及解压总结
把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其 ...
- HTML5资料整理 [From luics]
来自http://www.cnblogs.com/luics/,新浪微博@徐凯-鬼道 HTML5资料整理 项目组要做html5这块,花了一周左右时间收集的,快有一年时间了,部分内容需要更新,仅供参 ...
- POJ - 1321 棋盘问题 dfs分层搜索(n皇后变式)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47960 Accepted: 23210 Descriptio ...
- C#基础之--线程、任务和同步:一、异步委托
创建线程的一种简单方式是定义一个委托,并异步调用它.委托是方法的类型安全的引用. Delegate还支持异步地调用方法.在后台Delegate类会创建一个执行任务的线程. 为了说明委托的异步特性,从一 ...
- HDU2896【AC自动机-模板】
思路: 因为不同病毒特征码不会相同. AC自动机,然后对于每一个输出即可. 注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE. //#include <bits/st ...
- lightoj 1034【强连通+缩点】
思路: 缩点,计算入度为0点的个数即可: #include<bits/stdc++.h> using namespace std; typedef long long LL; const ...
- HDU5113【DFS+剪枝】
题意: n*m的矩阵 k种颜色 每种颜色有c[i]个 上下左右相邻的格子不能一样的颜色 问你有没有一种染色方法,有的话输出方案. 思路: 暴搜啊,n,m都才5,做完以后大哥的剪枝是奇偶剪枝,其实画完图 ...
- perl 打开二进制文件,并拷贝内容
实现脚本 use strict ; my $fileName = "/Users/chenfool/Desktop/sequoiadb-大数据平台构建.pdf" ; my $out ...
- PostgreSQL-14-异常值处理
-- 查看异常值CREATE TABLE outerdata(id int PRIMARY KEY,value numeric); \COPY outerdata FROM 'C:\Users\iHJ ...