HDU 6041(仙人掌图+tarjan)
来源:互联网 发布:简单软件编程教程 编辑:程序博客网 时间:2024/05/22 14:31
题意:给你一个仙人掌图,求前K小的生成树。
题解:由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉。所以问题就变为有
对所有集合两个两个进行合并,设当前合并的集合是
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cstdlib>#include <queue>using namespace std;#define N 1010#define M 100010struct edge{ int x,y,w; edge(){} edge(int x,int y,int w):x(x),y(y),w(w){}};struct rec{ int id,w; rec(){} rec(int id,int w):id(id),w(w){} bool operator<(const rec &a)const { return w<a.w; }};vector<edge> g[N];vector<int> a[N*5];int ans[2][M];int dfn[N],sz[2];edge sta[N*5];int n,m,K,top,t,cnt;void dfs(int x,int fa){ dfn[x]=++cnt; edge p; int y,top1; for(int i=0;i<g[x].size();++i) { y=g[x][i].y; if(y==fa) continue; if(dfn[y]==0) { sta[++top]=g[x][i]; dfs(y,x); --top; } else if(dfn[x]>dfn[y]) { ++t; a[t].push_back(g[x][i].w); top1=top; do { p=sta[top1--]; a[t].push_back(p.w); }while(p.x!=y); } }}bool cmp(int x,int y){ return x>y;}void Merge(int *a,int r,vector<int> &b,int *c){ priority_queue<rec> que; for(int i=0;i<b.size();++i) que.push(rec(0,a[0]+b[i])); rec e; for(int i=0;i<K;++i) { sz[(r+1)&1]=i+1; e=que.top(); que.pop(); c[i]=e.w; if(e.id<sz[r&1]-1) que.push(rec(e.id+1,e.w-a[e.id]+a[e.id+1])); if(que.empty()) break; }}int main(){ int ca=0; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;++i) g[i].clear(); int x,y,w,tot=0; for(int i=1;i<=m;++i) { scanf("%d%d%d",&x,&y,&w); g[x].push_back(edge(x,y,w)); g[y].push_back(edge(y,x,w)); tot+=w; } scanf("%d",&K); memset(dfn,0,sizeof(dfn)); top=t=cnt=0; dfs(1,0); memset(ans,0,sizeof(ans)); sz[0]=1; for(int i=1;i<=t;++i) { sort(a[i].begin(),a[i].end(),cmp); Merge(ans[(i-1)&1],i-1,a[i],ans[i&1]); } unsigned sum=0; for(int i=0;i<sz[t&1];++i) sum+=(unsigned)(i+1)*(unsigned)(tot-ans[t&1][i]); printf("Case #%d: %u\n",++ca,sum); for(int i=1;i<=t;++i) a[i].clear(); }}
阅读全文
0 0
- HDU 6041(仙人掌图+tarjan)
- HDU 3594 Cactus (仙人掌图、Tarjan)
- hdu 3594 强连通好题仙人掌图,对自己的tarjan模板改下用这个
- [SHOI2008]cactus仙人掌图 (tarjan + dp)
- Cactus 【 仙人掌图 tarjan 求scc 】
- HDU - 3594 Cactus(仙人掌图)
- HDU Cactus 3594 仙人掌图
- HDU 6041 I Curse Myself (仙人掌图)
- [BZOJ 2125]最短路---仙人掌图+Tarjan缩环+LCA
- hdu3594-仙人掌图的判断&tarjan找环-Cactus
- hdu 3594 Cactus(仙人掌图)
- hdu 3594 Cactus (仙人掌图判定/SCC)
- HDU 3594 Cactus(仙人掌图模板)
- HDU --- 3594 Cactus 【仙人掌图的运用】
- HDU 3594 Cactus 仙人掌
- hdoj 3594 Cactus 【仙人掌图的判定】【有向图tarjan求SCC】
- [BZOJ1023]静态仙人掌 Tarjan+dp+单调队列
- HDU 6041 I Curse Myself(仙人掌+tanjan)
- JDBC
- hdu 6038 Function
- C#(SuperWebSocket)与websocket通信
- let和const浅析
- 说Win清理不直观 有这个一目了然的走起不?
- HDU 6041(仙人掌图+tarjan)
- 博弈论(二):Sprague-Grundy函数
- 网页刷不开,那是你没改这个设置
- 敢动我电脑?小本本记下来
- 让windows10通知中心常驻
- HDU-2256(矩阵快速幂)
- Windows10开热点难?
- Mac中启动关闭Jenkins的方法
- 为什么SSD不能回复数据?