题意: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. xtu数据结构 C. Ultra-QuickSort

    C. Ultra-QuickSort Time Limit: 7000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  2. Consecutive Subsequence (DP+map)

    You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...

  3. Windows Server AppFabric

    文章:Windows Server AppFabric简介 介绍了AppFabric强大的功能.

  4. BZOJ 1260: [CQOI2007]涂色paint【区间DP】

    Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...

  5. 刷题总结——支线剧情(bzoj3876费用流)

    题目: [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往 都有很多的支线剧情,现在JYY想花费最 ...

  6. weixin-api生成二维码

    二维码长链接转成短链接(减少扫描时间和提高成功率) 微信返回正确的二维码的结果,参数有个url,即二维码图片解析后的地址,也可以根据此URL生成需要的二维码图片,而不需要通过ticket去换取图片了 ...

  7. CSS选择器与XPath语言

    一 在爬取页面信息的过程中,需要到想要的信息进行定位,主要有两种方法.CSS选择器和XPath语言.查找某一个标签,两种方法都可以做到. 二 CSS选择器 http://www.w3school.co ...

  8. Contest Hunter #46 T1 磁力块 [分块]

    描述 在一片广袤无垠的原野上,散落着N块磁石.每个磁石的性质可以用一个五元组(x,y,m,p,r)描述,其中x,y表示其坐标,m是磁石的质量,p是磁力,r是吸引半径.若磁石A与磁石B的距离不大于磁石A ...

  9. Cucumber Vs RobotFramework

    I asked this same question a little over a year ago on this list when we were at your stage. Before ...

  10. PatentTips - Blending a Graphic over a Video

    Television broadcasts of events can be enhanced by providing graphics that are blended with other im ...