UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
来源:互联网 发布:中老年人学英语软件 编辑:程序博客网 时间:2024/05/01 07:27
拿的最小瓶颈路LCA模板做的,感觉好浪费。
#include<iostream> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int maxn=100+5; const int maxm=10000+5; const int logmax=20; const int inf=1000000000; int fa[maxn],cost[maxn],dep[maxn],anc[maxn][logmax],maxcost[maxn][logmax]; int n,m; struct edge{ int u,v,w; edge(int u=0,int v=0,int w=0): u(u),v(v),w(w) {} bool operator < (const edge &rhs) const { return w<rhs.w; } }e[maxm]; vector<int> G[maxn]; vector<vector<edge> > nG(maxn); int pa[maxn]; int vis[maxn]; int map[maxn][maxn];int findset(int x) {return pa[x]==x?x:findset(pa[x]);} int kruskal(int c) { int mst=0; sort(e,e+c); for(int i=0;i<c;i++) { int root1=findset(e[i].u); int root2=findset(e[i].v); if(root1==root2) continue; mst+=e[i].w; map[e[i].u][e[i].v]=map[e[i].v][e[i].u]=0; pa[root2]=root1; nG[e[i].u].push_back(edge(e[i].u,e[i].v,e[i].w)); nG[e[i].v].push_back(edge(e[i].v,e[i].u,e[i].w)); } return mst;} void dfs(int u,int f,int c,int d) { vis[u]=1; fa[u]=f;cost[u]=c;dep[u]=d; for(int i=0;i<nG[u].size();i++) { int v=nG[u][i].v; int c=nG[u][i].w; if(vis[v]) continue; dfs(v,u,c,d+1); } } void preprocess() { for(int i=1;i<=n;i++){ anc[i][0]=fa[i];maxcost[i][0]=cost[i]; for(int j=1;(1<<j)<=n;j++){ anc[i][j]=-1;maxcost[i][j]=0; } } for(int j=1;(1<<j)<=n;j++) { for(int i=1;i<=n;i++) { if(anc[i][j-1]!=-1) { int a=anc[i][j-1]; anc[i][j]=anc[a][j-1]; maxcost[i][j]=max(maxcost[i][j-1],maxcost[a][j-1]); } } } } int query(int p,int q) { int log; if(dep[p]<dep[q]) swap(p,q); for(log=1;(1<<log)<=dep[p];log++) ;log--; int ans=-inf; for(int i=log;i>=0;i--) if(dep[p]-(1<<i)>=dep[q]) {ans=max(ans,maxcost[p][i]);p=anc[p][i];} if(p==q) return ans; for(int i=log;i>=0;i--){ if(anc[p][i]!=-1&&anc[p][i]!=anc[q][i]){ ans=max(ans,maxcost[p][i]);p=anc[p][i]; ans=max(ans,maxcost[q][i]);q=anc[q][i]; } } ans=max(ans,cost[p]); ans=max(ans,cost[q]); return ans; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(map,0,sizeof(map)); int c=0; for(int i=1;i<=n;i++) {G[i].clear();nG[i].clear();pa[i]=i;} for(int i=1;i<=m;i++) { int x,y,w; scanf("%d%d%d",&x,&y,&w); G[x].push_back(y); G[y].push_back(x); e[c].u=x;e[c].v=y;e[c].w=w;c++; map[x][y]=map[y][x]=w; } int mst=kruskal(c),cmst=inf; memset(vis,0,sizeof(vis)); dfs(1,-1,0,0); preprocess(); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(map[i][j]){ cmst=min(cmst,mst+map[i][j]-query(i,j)); } } } printf("%d %d\n",mst,cmst); } return 0; }
0 0
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- UVA 10600 - ACM Contest and Blackout 次小生成树
- 【UVa】10600 ACM Contest and Blackout 次小生成树
- uva 10600 ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout (次小生成树)
- UVA - 10600 ACM Contest and Blackout(次小生成树)
- Uva 10600 ACM Contest and Blackout(次小生成树)
- UVA 10600 - ACM Contest and Blackout(最小生成树&次小生成树)
- UVa 10600 - ACM Contest and Blackout(最小生成树)
- UVA 10600 ACM Contest and Blackout(最小生成树)
- UVa 10600__ACM Contest and Blackout(次小生成树)
- UVa 10600 ACM contest and Blackout( 次小生成树)
- uva 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 - ACM Contest and Blackout (次小生成树)
- UVA 10600 ACM Contest and Blackout 次小生成树/裸
- UVA 10600 - ACM Contest and Blackout(次小生成树)
- UVA 10600 ACM Contest and Blackout (次小生成树 Kruskal 封装)
- UVA 10600 ACM Contest and Blackout(次小生成树)
- KCF 跟踪算法(3)
- 什么是FPS游戏
- 各个大神博客地址
- react native组件通信以及
- [Leetcode] 413. Arithmetic Slices
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- python学习记录--set
- css学习笔记
- androidstudio视频录制和截图功能
- Tomcat启动脚本
- linux tar 命令
- elicpse中布局编写时出现Element <RelativeLayout> is unknown.
- ZCMU-1382-数位dp
- 序列化与反序列化