N cities (n <= 10000) with m edges (M <= 40000), each city has a maintenance cost (I). In addition, the maintenance cost of each edge is the product of the city logarithm and edge weight that cannot be communicated after the edge is removed. This fee must be added to one of the two cities on the edge, ask you the minimum maximum fee for all cities. ,
Train of Thought: First, you can get the cost of the edge by finding the bridge through Tarjan (using the nature of the Bridge), and then the second answer! For each vertex, if a son cannot maintain the edge, it is not feasible. Otherwise, try to let the son maintain the edge right. If not, he can only let the father bear it.
#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;}
Va 12587 reduce the maintenance cost (Tarjan + binary + DFS)