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) ...
随机推荐
- Combox选中项注意事项
一般我们选中某个combox的下拉框会用如下方式: 1.combox.SelectedIndex=下拉框下标,如0是选中第一个.-1是不选中任何项等等: 2.combox.SelectedItem=某 ...
- cocos2dx 手势识别
转自:http://blog.csdn.net/qq634416025/article/details/8685187 g_rGemertricRecognizer = new GeometricRe ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- Codeforces Gym 100463B Music Mess Hash 逻辑题
Music Mess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments ...
- C# 仿制QQ弹出新闻消息框
打开QQ的时候,QQ新闻弹出窗体在屏幕的右下角就会慢慢升起一个小窗口,占用的地方不大,可以起到提示的作用.下面就让我们来看看,怎样用系统API来轻松实现这个功能. API原型函数: bool Anim ...
- 2013 Fench Open quart-semifnl press conference
http://v.youku.com/v_show/id_XNTY3NTAwOTA4.html?firsttime=179 Novak, very congradutlations, to rea ...
- android之多媒体篇(一)
Android 4.0.3(Api Level 15)支持的多媒体格式. 注意:有些设备可能支持其他的文件格式. 1.Audio AAC LC/LTP.HE-AACv1(AAC+).AMR-NB.AM ...
- MySQL auto_increment实现
http://www.cnblogs.com/xpchild/p/3825309.html 运维的时候,经常遇到auto_increment的疑惑: 机器异常crash,重启后id回退的问题 性能考虑 ...
- 谈C#中的Delegate
引言 Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delegate这个特性,所以一直没有对它作整理.这两天,我再度翻阅了一些关于Delegate的资料 ...
- Ubuntu下VSFTPD(五)(匿名FTP设置方法)
匿名FTP设置方法: 通常在登录FTP服务器的用户不确定的情况下,应将FTP服务器设置为允许匿名账号登录的FTP服务器 1.启用匿名帐号 anonymous_enable=YES local_ ...