hdu6041
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的更多相关文章
随机推荐
- 左侧导航条+中间显示内容+右侧菜单配置,Bootstrap+AdminLTE+Jquery
1.最近做个导航页面,找了一大堆UI,最终选了AdminLTE,这个UI也是以bootstrap为基础,简单实用,中间内容用jquery的load加载,简单暴力,非常适合快速开发. 2.效果图如下: ...
- [NOIP2017 TG D2T2]宝藏
题目大意:给定一个有重边,边有权值的无向图.从某一个点出发,求到达所有的点需要的最少费用,并且限制两点之间只有一条路径.费用的计算公式为:所有边的费用之和.而边$x->y$的费用就为:$y$到初 ...
- 你知道HTML标签设计的本意吗?
“DIV+CSS”这个词汇不知道害了多少人,也许其提出者本意并没有错,但是跟风者从表现曲解了其意思,认为整个页面就应当是DIV+CSS文件的组合.这样做,对于视觉上并没有什么影响,因为还原了之前设计的 ...
- github导入springboot maven项目
1.在GitHub里force喜欢的项目,获取GitHub项目地址,eclipse---import---project from git---clone uri---next---finish,项目 ...
- Unescape JavaScript's escape() using C#
js里面的 unescape escape 对应C#里面 var unescapedString = Microsoft.JScript.GlobalObject.unescape(yourEscap ...
- HDU1272:小希的迷宫(并查集)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- Codeforces Round #520 (Div. 2) C. Banh-mi
C. Banh-mi time limit per test:1 second memory limit per test:256 megabytes 题目链接:https://codeforc.es ...
- git学习,哇瑟说实话我想要的
1.Git 简介及安装Git是目前世界上最先进的分布式版本控制系统(没有之一).它的诞生也颇具传奇,Linux创始人Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!有兴趣的话, ...
- javaScript获取文档中所有元素节点的个数
HTML+JS 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- HTML页面为什么设置了UTF-8仍然中文乱码
如题,其实问题很简单,在用EditPlus写html页面的时候,发现设置为UTF-8的时候仍然出现了乱码,这是一个很奇怪的问题,而且我完全考虑了浏览器的解析问题,将title放在了了meta标签之后, ...