hdu6041

题意

给出一个仙人掌

如果一个无向连通图的任意一条边最多属于一个简单环,我们就称之为仙人掌。所谓简单环即不经过重复的结点的环。

求前 \(K\) 小生成树 。

分析

仙人掌中每个环中我们最多可以删掉一条边,题目就变成了有 \(M\) 个数组,每次从每个数组中分别取一个数字并求和,前 \(K\) 大的和。

首先用 \(Tarjan\) 算法找环,然后使用优先队列不断合并两个数组( 多路归并问题,见白书 P189 )。

code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1e5 + 10;
const int M = 2e3;
int n, m, K;
struct Edge {
int to, w, next;
}e[M << 1];
int cnt, head[M << 1];
void addedge(int u, int v, int w) {
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}
int a[MAXN], b[MAXN], c[MAXN];
int dfn[M], sz;
stack<int> sta;
struct Item {
int s;
int x;
Item(int s, int x):s(s), x(x) {}
bool operator<(const Item& other) const {
return s < other.s;
}
};
void cal() {
priority_queue<Item> q;
for(int i = 1; i <= b[0]; i++) {
q.push(Item(a[1] + b[i], 2));
}
c[0] = 0;
while(c[0] < K && !q.empty()) {
Item it = q.top(); q.pop();
c[++c[0]] = it.s;
if(it.x <= a[0]) q.push(Item(it.s - a[it.x - 1] + a[it.x], it.x + 1));
}
a[0] = c[0];
for(int i = 1; i <= c[0]; i++) {
a[i] = c[i];
}
}
int tarjan(int fa, int u) {
dfn[u] = ++sz;
int lowu = sz;
for(int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if(!dfn[v]) {
sta.push(i);
int lowv = tarjan(u, v);
if(lowu <= lowv) {
b[0] = 0;
while(1) {
int j = sta.top(); sta.pop();
b[++b[0]] = e[j].w;
if(j == i) break;
}
if(b[0] > 1) cal();
} else lowu = lowv;
} else if(v != fa && lowu > dfn[v]) {
sta.push(i);
lowu = dfn[v];
}
}
return lowu;
}
int main() {
int kase = 1;
while(~scanf("%d%d", &n, &m)) {
while(!sta.empty()) sta.pop();
cnt = 0;
sz = 0;
memset(dfn, 0, sizeof dfn);
memset(head, -1, sizeof head);
a[0] = 1; a[1] = 0;
int s = 0;
for(int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
s += w;
addedge(u, v, w);
addedge(v, u, w);
}
scanf("%d", &K);
tarjan(-1, 1);
unsigned ans = 0;
for(int i = 1; i <= a[0]; i++) {
ans = ans + (unsigned)i * (s - a[i]);
}
if(a[0] == 0) ans = s;
printf("Case #%d: %u\n", kase++, ans);
}
return 0;
}

hdu6041的更多相关文章

随机推荐

  1. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) B

    B. Little Artem and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes in ...

  2. [模拟赛] GotoAndPlay

    GotoAndPlay 10月3日,在杭州市西湖景区,一只小松鼠不停地接受一道道食物,花生. 玉米.饼干,可谓来者不拒,憨态可掬的模样吸引了众多围观者... Description 小松鼠终于吃撑了, ...

  3. 湖南大学第十四届ACM程序设计新生杯 E.Easy Problem

    E.Easy Problem Description: Zghh likes number, but he doesn't like writing problem description. So h ...

  4. bzoj 4880 [Lydsy1705月赛]排名的战争 贪心

    [Lydsy1705月赛]排名的战争 Time Limit: 8 Sec  Memory Limit: 256 MBSubmit: 338  Solved: 69[Submit][Status][Di ...

  5. django自己搭建的博客

    1.博客地址: http://jiangtao4.pythonanywhere.com/ 2.后台可以发布笔记,可以翻页,数据存在MySQL里面 3.GitHub地址: https://github. ...

  6. jrebel插件激活

    不管用的哪个工具都可以通过下面的教程获取注册码,激活你的开发工具 原文出自:http://www.gezila.com/tutorials/11476.html 首先打开Myeclipse,点击“he ...

  7. transition(动画属性)

    CSS 过渡(transition)是通过定义元素从起点的状态和结束点的状态,在一定的时间区间内实现元素平滑地过渡或变化的一种补间动画机制.你可以让属性的改变过程持续一段时间,而不是立即生效. 通过t ...

  8. Hibernate 三种状态变化 与 sql 语句的关系

    前言:在Hibernate中有三种状态,对它的深入理解,才能更好的理解hibernate的运行机理,刚开始不太注意这些概念,后来发现它是重要的.对于理解hibernate,JVM和sql的关系有更好的 ...

  9. Eclipse Tomcat部署项目没有加载新加的静态资源文件

    额,一直用MyEclipse,后来用Eclipse时,启动项目后去Tomcat webapps找对应文件夹,发现没有,才知道Eclipse 默认不往本地Tomcat部署. 1.eclipse不像MyE ...

  10. 关于Solaris的一些小技巧

    关于Solaris的一些小技巧 http://blog.chinaunix.net/uid-9787800-id-2394301.html SunOS 操作命令及linux区别 http://blog ...