uva 12587 二分枚举
思路:维护一个森林,二分枚举最小的最大值。
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 20010
#define Maxm 40010
#define LL long long
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 1e15
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 1000000007
using namespace std;
int head[Maxn],vi[Maxn],dfn[Maxn],low[Maxn],num[Maxn],is[Maxn],Stack[Maxn],top,e,lab;
int n,m;
LL val[Maxn],need[Maxn],cnt;
struct Edge{
int u,v,next,f;
LL val;
}edge[Maxm];
void init()
{
clr(head,-);
clr(vi,);
clr(is,);
clr(need,);
clr(dfn,);
clr(low,);
clr(num,);
e=lab=cnt=;
}
void add(int u,int v,LL val)
{
//cout<<u<<" "<<v<<endl;
edge[e].u=u,edge[e].v=v,edge[e].val=val,edge[e].f=,edge[e].next=head[u],head[u]=e++;
}
void Tarjan(int u)
{
int i,v,cc;
++cnt;
vi[u]=;
dfn[u]=low[u]=++lab;
Stack[++top]=u;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(edge[i].f) continue;
edge[i].f=edge[i^].f=;
if(!dfn[v]){
cc=cnt;
Tarjan(v);
if(low[v]>dfn[u]){
is[u]=;
num[v]=cnt-cc;
}
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],dfn[v]);
}
}
bool dfs(int u,LL nal)
{
int i,v;
need[u]=val[u];
vi[u]=;
for(i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
if(vi[v]) continue;
if(!dfs(v,nal))
return false;
if(is[u]){
if(num[v]*(cnt-num[v])*edge[i].val+need[v]>nal)
need[u]+=num[v]*(cnt-num[v])*edge[i].val;
}
if(need[u]>nal)
return false;
}
return true;
}
bool OK(LL x,int p)
{
int i,j;
for(i=;i<=top;i++) vi[Stack[i]]=;
for(i=;i<=top;i++) if(val[Stack[i]]>x){
return false;
}
if(!dfs(p,x))
return false;
return true;
}
void solve()
{
int i,j;
LL ans=;
for(i=;i<=n;i++) if(!dfn[i]){
Tarjan(i);
LL l,r,mid;
l=,r=inf;
while(l<r){
mid=(l+r)>>;
if(OK(mid,i))
r=mid;
else
l=mid+;
}
ans=max(l,ans);
cnt=;
top=;
}
printf("%lld\n",ans);
return ;
}
int main()
{
int i,j,u,v,t,Ca=;
LL cc;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
scanf("%lld",val+i);
for(i=;i<=m;i++){
scanf("%d%d%lld",&u,&v,&cc);
add(u,v,cc);
add(v,u,cc);
}
printf("Case %d: ",++Ca);
solve();
}
return ;
}
uva 12587 二分枚举的更多相关文章
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- SDIBT 3237 Boring Counting( 划分树+二分枚举 )
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
- Codeforces 807C - Success Rate(二分枚举)
题目链接:http://codeforces.com/problemset/problem/807/C 题目大意:给你T组数据,每组有x,y,p,q四个数,x/y是你当前提交正确率,让你求出最少需要再 ...
- Codeforces 801C Voltage Keepsake(二分枚举+浮点(模板))
题目链接:http://codeforces.com/contest/801/problem/C 题目大意:给你一些电器以及他们的功率,还有一个功率一定的充电器可以给这些电器中的任意一个充电,并且不计 ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
随机推荐
- NameThreadForDebugging -- Naming threads for debugging
http://forums.devart.com/viewtopic.php?t=16907 type tagTHREADNAME_INFO = record dwType : LongWord; / ...
- TMS3705A PCF7991AT 线路图
- gzip压缩JavaScript
为了提高客户端的体验效果,RIA开发逐渐兴起.这样会项目中会充斥的大量的JavaScript代码,与此同时会消耗客户端浏览器性能.对于 Ext 实现的 one page one application ...
- [Express] Level 3: Massaging User Data
Flexible Routes Our current route only works when the city name argument matches exactly the propert ...
- 深入理解 CSS 中的行高与基线
1.基本概念 1. 基线.底线.顶线.中线 注意:基线(base line)并不是汉字文字的下端沿,而是英文字母“x”的下端沿. 2. 内容区 内容区是指底线和顶线包裹的区域(行内元素display ...
- 疑难杂症:NoSuchMethodError: com.opensymphony.xwork2.util.finder.UrlSet.includeClassesUrl(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;)
严重: Exception starting filter struts2java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finde ...
- SimpleDateFormat使用详解及与毫秒的相互转换
1. SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...
- oracle视图总结
视图简介: 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改.视图基于的表称为基表.视图是存储在数据字典里的一条select语句. 通过创建视图可以提取数 ...
- .NET开发必备工具之-Reflector
第一步,下载 第二步,解压缩下载的包,双击Reflector应用程序打开 第三步,设置默认程序集,可以不设定 第四步,找到你要反编译dll文件 第五步,把dll文件拷贝到方便的位置(我在桌面上新建了T ...
- EMS电子面单接口对接使用-免费版
快递鸟电子面单接口,可一次对接15家快递公司, 无需和每一家快递公司做对接.支持快递有四通一达.顺丰.EMS.宅急送.德邦.优速等15家快递公司,对顺丰有电子面单服务需求的可以选择顺丰自有的电子面单或 ...