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. nodejs核心技术

    一.知识结构: http模块:配置简单 的web服务,npm/cnpm工具 express框架:express中间件进行服务配置:路由:请求处理: DB服务:学习使用mysql关系型数据库: web接 ...

  2. vue:自定义指令

    <div id="app"> <div v-lang="color">{{num}}</div> <button @c ...

  3. springmvc请求路径和请求参数的获取注解- @PathVariable和@RequestParam

    @PathVariable和@RequestParam @PathVariable是从路径里面去获取变量,也就是把路径当做变量. @RequestParam是从请求里面获取参数. 如:url:http ...

  4. 怎样从外网访问内网MongoDB数据库?

    本地安装了一个MongoDB数据库,只能在局域网内访问到,怎样从外网也能访问到本地的MongoDB数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动MongoDB数据库 默认安装 ...

  5. tomcat 启动时遇到org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs

    当发生这样的错误的时候 org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet ...

  6. Qt QTextEdit根据行号移动光标

    QTextEdit* p = new QTextEdit; QTextBlock block = p->document()->findBlockByNumber(nLineNum); p ...

  7. ltp-ddt的makefile结构

    顶层makefile COMMON_TARGETS        := pan utils COMMON_TARGETS    += tools testcases/ddt COMMON_TARGET ...

  8. MySQL字符类型datetime与timestamp

    这片博客来详细分区一下这哥俩! 首先来说明这两个字符类型: DATETIME 8 1000-01-01 00:00:00 ~9999~12-31 23:59:59 0000-00-00 00:00:0 ...

  9. LNMP 添加 memcached服务

    LNMP 添加 memcached服务   由于memcached具有更多的功能和服务,已经不推荐使用memcache了.(缺少个字母d) 1. 首先安装memcached服务端. 这里使用yum源安 ...

  10. Java操作文件Util

    package io.guangsoft.utils; import java.io.File; import java.io.FileInputStream; import java.io.File ...