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 ...
随机推荐
- Django 安装 创建项目 运行项目
Django基础 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的We ...
- PDF文档导出
代码如下: /// <summary> /// 获取html内容,转成PDF(注册) /// </summary> public void DownloadPDFByHTML( ...
- js获取浏览器类型和版本信息
bro () { let broName = 'Runing' let strStart = 0 let strStop = 0 let temp = '' let userAgent = windo ...
- 状态管理之cookie使用及其限制、session会话
# 1.什么是状态管理? 将浏览器与web服务器之间多次交互当作一个整体来处理,并且将多次交互所涉及的数据(即状态)保存下来.(cookie浏览器所涉及到的访问数据保存下来)# 2.如何进行状态管理? ...
- selenium自动化之鼠标操作
在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素.那么这种情况,我们怎么处理呢?,selenium给我们提供了一个类来处理这类事件——ActionChai ...
- url去重 --布隆过滤器 bloom filter原理及python实现
https://blog.csdn.net/a1368783069/article/details/52137417 # -*- encoding: utf-8 -*- ""&qu ...
- Docker学习笔记之使用 Docker Hub 中的镜像
0x00 概述 自己编写 Dockerfile 能够很好的实现我们想要的程序运行环境,不过如果装有我们想要环境的镜像已经由热心的开发者构建好并共享在 Docker Hub 上,直接使用它们就会远比自己 ...
- 使用dockerfile构建自己的镜像
CentOS Linux release 7.2.1511 Docker version 17.03.1-ce 首先应该了解docker镜像的分层机制,这个网上文章很多,简单说就是对镜像的每次修改都是 ...
- 写给大忙人的spring cloud 1.x学习指南
这几天抽空搞了下spring cloud 1.x(2.0目前应该来说还不成熟),因为之前项目中使用dubbo以及自研的rpc框架,所以总体下来还是比较顺利,加上spring boot,不算笔记整理,三 ...
- vue 起步
vue 官网 目前最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angular.js和react.js的优点,并剔除了它们的缺点”.授予了这么高的评价的vue ...