UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)
题意:n个城市(n <= 10000), 有m条边(m <= 40000),每一个城市有一个维护费用Cost(i),除此之外,每条边的维修费用为去掉该边后不能通信的城市对数与边权的积。这个费用要加到这条边的两端城市的某一个,问你全部城市的最大费用的最小值。、
思路:首先边的费用能够通过Tarjan求桥之后求得(利用桥的性质),然后就是二分答案了!对于每一个点,假设有个儿子不能维护。那么不可行,否则。试着让儿子去维护边权,假设不可行,仅仅能让父亲承担。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 10000+10;
const int maxm = 40000+10;
LL cost[maxn];
int head[maxn];
int n,m;
int dfn[maxn],lown[maxn];
bool CanE[maxn];
int cnt[maxm];
LL ans;
int nume,dfs_clock;
bool vis[maxn];
bool isBridge[maxm];
vector<int> sta;
struct edge{
int u,v,w,nxt;
}e[maxm]; void Tarjan(int u,int fa) {
lown[u] = dfn[u] = dfs_clock++;
sta.push_back(u);
for(int i = head[u]; i ; i = e[i].nxt) {
int v = e[i].v;
if(v==fa) continue;
if(!dfn[v]) {
Tarjan(v,u);
lown[u] = min(lown[u],lown[v]);
if(lown[v] > dfn[u]) {
isBridge[i] = isBridge[i^1] = true;
cnt[i] = cnt[i^1] = dfs_clock-dfn[v];
}
}else {
lown[u] = min(lown[u],dfn[v]);
}
}
}
bool dfs(int u,LL s,LL x) {
vis[u] = true;
LL ts = cost[u];
int sz = sta.size();
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v, w = e[i].w;
if(isBridge[i] && !vis[v]) {
LL tmp = (LL)w*cnt[i]*(sz-cnt[i]);
if(!dfs(v,tmp,x)) return false;
if(!CanE[v]) ts += tmp;
}
}
if(ts > x) return false;
if(ts+s <= x) CanE[u] = true;
return true;
}
bool can(LL x) {
int len = sta.size();
memset(CanE,false,sizeof CanE);
for(int i = 0; i < len; i++) vis[sta[i]] = false;
for(int i = 0; i < len; i++) {
int t = sta[i];
if(vis[t]) continue;
if(!dfs(t,0,x)) return false;
}
return true;
}
void init() {
nume = 1;
ans = 0;
dfs_clock = 1;
memset(head,0,sizeof head);
memset(isBridge,false,sizeof isBridge);
memset(vis,false,sizeof vis);
memset(dfn,0,sizeof dfn);
memset(lown,0,sizeof lown);
memset(CanE,false,sizeof CanE);
sta.clear();
}
void addedge(int u,int v,int w) {
e[++nume].nxt = head[u];
e[nume].u = u;
e[nume].v = v;
e[nume].w = w;
head[u] = nume;
}
void solve() {
for(int i = 1; i <= n; i++) {
if(!dfn[i]) {
sta.clear();
Tarjan(i,-1);
LL L = ans,R = 1e10;
while(L <= R) {
LL mid = (L+R)/2;
if(can(mid)) {
R = mid-1;
}else{
L = mid+1;
}
}
ans = max(ans,L);
}
}
printf("%lld\n",ans);
}
int main(){ int ncase,T=1;
cin >> ncase;
while(ncase--) {
init();
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) {
scanf("%lld",&cost[i]);
ans = max(ans,cost[i]);
}
for(int i = 0; i < m; i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
printf("Case %d: ",T++);
solve();
}
return 0;
}
UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)的更多相关文章
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...
- [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】
题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...
- 51nod1307(暴力树剖/二分&dfs/并查集)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...
- uva 12587 二分枚举
思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...
- 【UVA 11865】 Stream My Contest (二分+MDST最小树形图)
[题意] 你需要花费不超过cost元来搭建一个比赛网络.网络中有n台机器,编号0~n-1,其中机器0为服务器,其他机器为客户机.一共有m条可以使用的网线,其中第i条网线的发送端是机器ui,接收端是机器 ...
- Codeforces 901C Bipartite Segments(Tarjan + 二分)
题目链接 Bipartite Segments 题意 给出一个无偶环的图,现在有$q$个询问.求区间$[L, R]$中有多少个子区间$[l, r]$ 满足$L <= l <= r &l ...
- UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)
题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...
- uva 11324 The Largest Clique (Tarjan+记忆化)
/*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...
随机推荐
- BZOJ 1426 收集邮票 ——概率DP
$f(i)$表示现在有$i$张,买到$n$张的期望 所以$f(i)=f(i+1)+\frac {n}{n-i}$ 费用提前计算,每张邮票看做一元,然后使后面每一张加1元 $g(i)$表示当前为$i$张 ...
- haskell 乱搞(2)之 Y-conbinator [原创]
Y-conbinator"有没有用"?并没有,在大多数支持函数式编程的语言里,你可以自由的使用递归,而这货只是作为理论基石弥散在函数式编程的血肉之中 这是数学笔记,这是数学笔记,这 ...
- bzoj 1818 Cqoi2010 内部白点 扫描线
[Cqoi2010]内部白点 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1126 Solved: 530[Submit][Status][Disc ...
- linux虚拟机无法上网 Network is unreachable
系统centos 安装ftp时报错 Couldn't resolve host 'mirrorlist.centos.org [root@wulihua bin]# yum install vsft ...
- 中国余数定理 2(codevs 3990)
题目描述 Description Skytree神犇最近在研究中国博大精深的数学. 这时,Sci蒟蒻前来拜访,于是Skytree给Sci蒟蒻出了一道数学题: 给定n个质数,以及k模这些质数的余数.问: ...
- 洛谷P2677 超级书架 2
题目描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= N & ...
- 洛谷P2625 豪华游轮
题目描述 有一条豪华游轮(其实就是条小木船),这种船可以执行4种指令: right X : 其中X是一个1到719的整数,这个命令使得船顺时针转动X度. left X : 其中X是一个1到719的整数 ...
- *AtCoder Regular Contest 096F - Sweet Alchemy
$n \leq 50$的树,每个点有权值,现要选点(可多次选一个点)使点数尽量多,如下限制:选的总权值不超过$C \leq 1e9$:$c_i$表示$i$选的次数,$p_i$表示$i$的父亲,那么$c ...
- android 禁止ViewPager滑动
最近项目中,有个需求就是要禁止ViewPager滑动事件,我们看下360手机助手的界面,风格就类似这样的 大家如果使用过360手机助手就会发现中间内容是不可以滑动的,现在写一个demo,讲下怎么禁止V ...
- SQL Server 命令行操作
连接sqlcmd -S localhost -U SA -P '123456'; 执行脚本 sqlcmd -S localhost -U SA -P '123456' -i /root/dbo.sql ...