hdu 6041 I Curse Myself 无向图找环+优先队列
I Curse Myself
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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.
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).
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
Case #2: 26
Case #3: 493
#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 无向图找环+优先队列的更多相关文章
- HDU 6041 - I Curse Myself | 2017 Multi-University Training Contest 1
和题解大致相同的思路 /* HDU 6041 - I Curse Myself [ 图论,找环,最大k和 ] | 2017 Multi-University Training Contest 1 题意 ...
- zstu.4191: 无向图找环(dfs树 + 邻接表)
4191: 无向图找环 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 117 Solved: 34 Description 给你一副无向图,每条边有 ...
- HDU 6041.I Curse Myself 无向仙人掌图
I Curse Myself Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- HDU 6041 I Curse Myself(二分+搜索)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6041 [题目大意] 给出一个仙人掌图,求第k小生成树 [题解] 首先找到仙人掌图上的环,现在的问题 ...
- hdu 6041 I Curse Myself
题目: 点这里OvO http://acm.hdu.edu.cn/showproblem.php?pid=6041 2017 Multi-University Training Contest - T ...
- HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场
题意: 给出一个仙人掌图,然后求他的前K小生成树. 思路: 先给出官方题解 由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉.所以问题就变为有 M 个集合,每个集合里面都有一堆 ...
- HDU 6041 I Curse Myself ——(仙人掌图,tarjan,转化)
题解见这个博客:http://blog.csdn.net/ME495/article/details/76165039. 复杂度不太会算..这个经典问题的解法需要注意,维护队列里面只有k个元素即可.另 ...
- HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...
- [hdu5348]图上找环,删环
http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...
随机推荐
- excel vba 数据分析
(Visual Basic Application) VBA(Visual Basic for Application)是Microsoft Office系列软件的内置编程语言,其语法结构与Visua ...
- 【Shell循环进程并行处理】利用简单的语句实现for循环并行处理命令
在生信分析中,经常会遇到不同的重复和处理,这样的分析过程有时是非常费时且占用资源并不是很多的,可以同时在后台运行以节约时间,这是并行处理的意义.除了需要并行处理,循环迭代来遍历整个文件夹的需要分析的数 ...
- Mongo第三个参数的用法
Mongo update的用法 Update( array $criteria , array $new_object [, array $options = array() ] ) 第一个参数是条件 ...
- ElasticSearch vs Solr多维度分析对比
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- P5241 序列(滚动数组+前缀和优化dp)
P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$ ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- Python之字符编码(一)
一.了解字符编码的知识储备? 1.计算机基础知识? 计算机中所有的软件文件(包括:操作系统)都存储在硬盘,启动计算机,计算机需要把系统文件都去到内存中. 2.文本编辑器存取文件的原理(nodepad+ ...
- Java 中的多线程你只要看这一篇就够了
引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个 ...
- windows安装 php-redis redis 扩展
1.查看phpinfo(),确定要下载的扩展版本,扩展的下载地址在:https://pecl.php.net/package/redis 上图对应是是以下版本 2.把下载包里的文件放到php的ext文 ...
- ODAC(V9.5.15) 学习笔记(四)TOraDataSet
名称 类型 说明 SequenceMode TSequenceMode ODAC可以直接利用Oracle中的序列对象为表的主键赋值,从而实现主键自动增长的功能.该属性决定了在什么场合下使用序列: sm ...