I Curse Myself

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
There is a connected undirected graph with weights on its edges. It is guaranteed that each edge appears in at most one simple cycle.

Assuming that the weight of a weighted spanning tree is the sum of weights on its edges, define V(k) as the weight of the k-th smallest weighted spanning tree of this graph, however, V(k) would be defined as zero if there did not exist k different weighted spanning trees.

Please calculate (∑k=1Kk⋅V(k))mod232.

 
Input
The input contains multiple test cases.

For each test case, the first line contains two positive integers n,m (2≤n≤1000,n−1≤m≤2n−3), the number of nodes and the number of edges of this graph.

Each of the next m lines contains three positive integers x,y,z (1≤x,y≤n,1≤z≤106), meaning an edge weighted z between node x and node y. There does not exist multi-edge or self-loop in this graph.

The last line contains a positive integer K (1≤K≤105).

 
Output
For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 
Sample Input
4 3
1 2 1
1 3 2
1 4 3
1
3 3
1 2 1
2 3 2
3 1 3
4
6 7
1 2 4
1 3 2
3 5 7
1 5 3
2 4 1
2 6 2
6 4 5
7
 
Sample Output
Case #1: 6
Case #2: 26
Case #3: 493
 
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define PI acosI(-1.0)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e3+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e13+; struct is
{
int x,r;
bool operator <(const is &c)const
{
return x<c.x;
}
};
int d[maxn],n,m,k;
inline void update(vector<int>&a,vector<int>&b)
{
priority_queue<is>q;
for(int i=;i<b.size();i++)
d[i] = ,q.push((is){a[]+b[i],i});
vector<int>ans;
for(int i=;i<=k;i++)
{
if(q.empty())break;
is x=q.top();
q.pop();
ans.push_back(x.x);
if(d[x.r]+<a.size())
q.push((is){a[++d[x.r]]+b[x.r],x.r});
}
a=ans;
}
int cmp(int x,int y)
{
return x>y;
}
struct edge
{
int from,to,d,nex;
}G[maxn<<];
int head[maxn],edg;
inline void addedge(int u,int v,int d)
{
G[++edg]=(edge){u,v,d,head[u]},head[u]=edg;
G[++edg]=(edge){v,u,d,head[v]},head[v]=edg;
}
int pre[maxn],bccno[maxn];
int dfs_clock,bcc_cnt;
stack<int>s;
vector<int>ans,fuck;
inline int dfs(int u,int fa)
{
int lowu=++dfs_clock;
pre[u]=dfs_clock;
int child=;
for(int i=head[u]; i!=-; i=G[i].nex)
{
int v=G[i].to;
edge e=G[i];
if(!pre[v])
{
s.push(i);
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u])
{
bcc_cnt++;
fuck.clear();
while(true)
{
int e=s.top();
s.pop();
fuck.push_back(G[e].d);
if(bccno[G[e].from]!=bcc_cnt)
bccno[G[e].from]=bcc_cnt;
if(bccno[G[e].to]!=bcc_cnt)
bccno[G[e].to]=bcc_cnt;
if(G[e].from==u&&G[e].to==v) break;
}
if(fuck.size()>)update(ans,fuck);
}
}
else if(pre[v]<pre[u]&&v!=fa)
{
s.push(i);
lowu=min(lowu,pre[v]);
}
}
return lowu;
}
void init()
{
dfs_clock=bcc_cnt=edg=;
for(int i=;i<=n;i++)
head[i]=-,bccno[i]=,pre[i]=;
ans.clear();
ans.push_back();
} int main()
{
int cas=;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
ll sumsum=;
for(int i=; i<=m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
sumsum+=z;
addedge(x,y,z);
}
scanf("%d",&k);
dfs(,-);
ll out=,MOD=(1LL<<);
printf("Case #%d: ",cas++); for(int i=;i<=k;i++)
{
if(i->=ans.size())break;
out+=1LL*(sumsum-ans[i-])*i;
out%=MOD;
}
printf("%lld\n",out);
}
return ;
}

hdu 6041 I Curse Myself 无向图找环+优先队列的更多相关文章

  1. HDU 6041 - I Curse Myself | 2017 Multi-University Training Contest 1

    和题解大致相同的思路 /* HDU 6041 - I Curse Myself [ 图论,找环,最大k和 ] | 2017 Multi-University Training Contest 1 题意 ...

  2. zstu.4191: 无向图找环(dfs树 + 邻接表)

    4191: 无向图找环 Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 117  Solved: 34 Description 给你一副无向图,每条边有 ...

  3. HDU 6041.I Curse Myself 无向仙人掌图

    I Curse Myself Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. HDU 6041 I Curse Myself(二分+搜索)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6041 [题目大意] 给出一个仙人掌图,求第k小生成树 [题解] 首先找到仙人掌图上的环,现在的问题 ...

  5. hdu 6041 I Curse Myself

    题目: 点这里OvO http://acm.hdu.edu.cn/showproblem.php?pid=6041 2017 Multi-University Training Contest - T ...

  6. HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场

    题意: 给出一个仙人掌图,然后求他的前K小生成树. 思路: 先给出官方题解 由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉.所以问题就变为有 M 个集合,每个集合里面都有一堆 ...

  7. HDU 6041 I Curse Myself ——(仙人掌图,tarjan,转化)

    题解见这个博客:http://blog.csdn.net/ME495/article/details/76165039. 复杂度不太会算..这个经典问题的解法需要注意,维护队列里面只有k个元素即可.另 ...

  8. HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)

    求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...

  9. [hdu5348]图上找环,删环

    http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...

随机推荐

  1. [转载]CSS教程--字体与文本属性

    b>font-family功能:用于改变HTML标志或元素的字体,你可设置一个可用字体清单.浏览器由前向后选用字体.语法:{font-family:字体1,字体2, ... ,字体n} font ...

  2. 前端框架VUE----对象的单体模式

    对象的单体模式 为了解决箭头函数this指向的问题 推出来一种写法 对象的单体模式 1 var person = { 2 name:'小马哥', 3 age:12, 4 fav(){ 5 consol ...

  3. FAT16/32不等于ESP:windows安装程序无法将windows配置为在此计算机的硬件上运行

    今天给公司电脑装系统,由于公司特殊需要,要给新电脑装win7系统.三台完全一样的华硕adol笔记本,前两台都和win10并存装成了双系统,第三台被不懂系统的人尝试装win7搞坏了,只能全盘格式化后再装 ...

  4. 【题解】 Luogu P4145 上帝造题的七分钟2 / 花神游历各国

    原题传送门 这道题实际和GSS4是一样的,只是输入方式有点区别 GSS4传送门 这道题暴力就能过qaq(这里暴力指线段树) 数据比较水 开方修改在线段树中枚举叶节点sqrt 查询区间和线段树基本操作 ...

  5. Installing Moses on Ubuntu 16.04

    Installing Moses on Ubuntu 16.04 The process of installation To install requirements sudo apt-get in ...

  6. shell实现自动部署两台tomcat项目+备份

    就做个记录吧, 其实也没啥好说的. 主机 #!/bin/bash TODAY=$(date -d 'today' +%Y-%m-%d-%S) MIP="192.168.180.24" ...

  7. js的匿名函数 和普通函数

    匿名函数在声明时不用带上函数名, 可以把匿名函数当作一个function类型的值来对待 声明一个普通的函数 function func() { ... } 可以认为和var func = functi ...

  8. kali linux fuzz工具集简述

    模糊测试是一种自动化软件测试技术,涉及提供无效,意外或随机数据作为计算机程序的输入. 然后监视程序是否存在异常,例如崩溃,内置代码断言失败或潜在的内存泄漏. 通常,模糊器用于测试采用结构化输入的程序. ...

  9. topcoder srm 435 div1

    problem1 link 遍历未被删除的叶子结点即可. problem2 link 首先,将所有的蛋白质原子编号,设为$[0,m-1]$,每个原子可能对应多个长度为3的$ACGT$.设$n$为DNA ...

  10. Oracle SQL——inner jion;left join;right join的区别和使用场景

    背景 在一次面试的时候,面试官让我说一下这三者的使用场景和区别,当时瞬间懵逼,哈哈.回来赶快看一看,记下来. 详解 inner join 等值查询:返回两张表中,联结字段值相等的组合记录 举例:所有学 ...