题意: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)的更多相关文章

  1. UVA.10474 Where is the Marble ( 排序 二分查找 )

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

  2. 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 ...

  3. [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】

    题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...

  4. 51nod1307(暴力树剖/二分&dfs/并查集)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...

  5. uva 12587 二分枚举

    思路:维护一个森林,二分枚举最小的最大值. #include<set> #include<map> #include<cmath> #include<queu ...

  6. 【UVA 11865】 Stream My Contest (二分+MDST最小树形图)

    [题意] 你需要花费不超过cost元来搭建一个比赛网络.网络中有n台机器,编号0~n-1,其中机器0为服务器,其他机器为客户机.一共有m条可以使用的网线,其中第i条网线的发送端是机器ui,接收端是机器 ...

  7. Codeforces 901C Bipartite Segments(Tarjan + 二分)

    题目链接  Bipartite Segments 题意  给出一个无偶环的图,现在有$q$个询问.求区间$[L, R]$中有多少个子区间$[l, r]$ 满足$L <= l <= r &l ...

  8. UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)

    题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...

  9. uva 11324 The Largest Clique (Tarjan+记忆化)

    /*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...

随机推荐

  1. [android开发篇]自定义权限

    有时候,我们可能遇到如下需求场景:当用户在一个应用程序中进行某项操作时,会启动另外一个应用程序,最常见的时直接打开了另外一个应用程序,并进入其中某个Activity(如:有的应用中有推荐应用列表,当用 ...

  2. Relay Race (DP)

    Furik and Rubik take part in a relay race. The race will be set up on a large square with the side o ...

  3. TOJ 假题之 Cow Brainiacs

    1570: Cow Brainiacs  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: ...

  4. 常州模拟赛d4t2 陶陶摘苹果

    题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 n 个苹果.苹果成熟的时候,陶陶就会 跑去摘苹果. 陶陶的手不能弯 (他仅能把手伸直),当且仅当陶陶达到的高度与苹果的高度相等的时候,陶陶 ...

  5. [ZJOI2007]时态同步 (树形DP)

    题目描述 小 Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字 1,2,3-.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个 ...

  6. Linux(11):期中架构(3)--- SSH远程管理服务 & ansible 批量管理服务

    SSH远程管理服务 1. 远程管理服务知识介绍 # 1.1 SSH远程登录服务介绍说明 SSH是Secure Shell Protocol的简写,由 IETF 网络工作小组(Network Worki ...

  7. Linux(6):定时任务

    定时任务 定时任务的说明和分类 # 定时任务分类: 1. crond(crontab) 定时任务软件(软件包 cronie) 2. atd 运行一次 3. anacron 非7*24小时运行的服务器 ...

  8. Gauss 高斯消元

    高斯消元…… (裸的暴力) 如果你有一个n元的方程组你会怎么办? Ans:直接用初中的解方程组的方法呀! 没错,直接暴力加减消元.那什么是“高斯消元”?说白了,就是普通的加减消元罢了. 本人再考场上打 ...

  9. echarts 图表用例

    参考博客:http://blog.csdn.net/verne_feng/article/details/51731653 http://echarts.baidu.com/echarts2/doc/ ...

  10. Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) E. Vasya and Good Sequences

    题目链接 官网题解写的好清楚,和昨晚Aguin说的一模一样…… 这题只和每个数1的个数有关,设每个数1的个数的数组为$b$,就是首先一段如果是好的,要满足两个条件: 1.这一段$b$数组和为偶数,因为 ...