I Curse Myself

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2266    Accepted Submission(s): 544

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
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212 
 
题意:有一个n个结点,m条无向边的仙人掌图。求删除一些边形成生成树,求前k小生成树。
思路:因为是一个仙人掌图,所以每条边最多在一个简单环内。所以只需要删除每个简单环内的一条边就能形成生成树。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=1e3+,M=4e3+;
struct edge
{
int from,to;
int w;
int next;
};
int n,m,k;
edge es[M];
int cnt,head[N];
int dfs_clock=;
int pre[N],low[N];
stack<int>s;
void init(int n)
{
cnt=;
dfs_clock=;
for(int i=; i<=n+; i++) head[i]=-,pre[i]=;
}
void addedge(int u,int v,int w)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].w=w;
es[cnt].next=head[u];
head[u]=cnt;
}
int tmp[],ans[];
struct node
{
int num;
int id;
bool operator <(const node x) const
{
return x.num>num;
}
};
void unit(priority_queue<node> &q)
{
tmp[]=;
while(tmp[]<k&&!q.empty())
{
node x=q.top();
q.pop();
tmp[++tmp[]]=x.num;
if(++x.id<=ans[]) q.push((node)
{
x.num-ans[x.id-]+ans[x.id],x.id
});
}
ans[]=;
for(int i=; i<=tmp[]; i++) ans[++ans[]]=tmp[i];
}
bool dfs(int u,int fa)
{
pre[u]=low[u]=++dfs_clock;
for(int i=head[u]; i!=-; i=es[i].next)
{
int v=es[i].to;
if(v==fa) continue;
if(!pre[v])
{
s.push(i);
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=pre[u])
{
priority_queue<node>q;
while(!s.empty())
{
int poi=s.top();
s.pop();
q.push((node){ans[]+es[poi].w,});
if(poi==i) break;
}
if(q.size()>) unit(q);
}
}
else if(pre[v]<pre[u]&&v!=fa)
{
s.push(i);
low[u]=min(low[u],pre[v]);
}
}
}
int main()
{
int Case=;
while(scanf("%d%d",&n,&m)!=EOF)
{
init(n);
int all=;
for(int i=; i<=m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
all+=w;
addedge(u,v,w),addedge(v,u,w);
}
scanf("%d",&k);
ans[]=,ans[++ans[]]=;
dfs(,);
ll sum=,mod=(1LL<<);
for(int i=; i<=ans[]; i++)
sum=(sum+(1LL*(all-ans[i])*i)%mod)%mod;
printf("Case #%d: %lld\n",++Case,sum);
}
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. HDU 6041 I Curse Myself ——(仙人掌图,tarjan,转化)

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

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

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

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

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

  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 无向图找环+优先队列

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

  7. HDU 3594.Cactus 仙人掌图

    Cactus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. hdu 3594 Cactus /uva 10510 仙人掌图判定

    仙人掌图(有向):同时满足:1强连通:2任何边不在俩个环中. 个人理解:其实就是环之间相连,两两只有一个公共点,(其实可以缩块),那个公共点是割点.HDU数据弱,网上很多错误代码和解法也可以过. 个人 ...

  9. HDU 3594 Cactus (强连通+仙人掌图)

    <题目链接> <转载于 >>> > 题目大意: 给你一个图,让你判断他是不是仙人掌图. 仙人掌图的条件是: 1.是强连通图. 2.每条边在仙人掌图中只属于一个 ...

随机推荐

  1. Android 开发 Fresco框架点击小图显示全屏大图实现 ZoomableDraweeView

    目标需求 实现一张小图片,被点击后变成一个在整个屏幕上显示的大图片.类似于微信朋友圈的图片. 实现流程 1.Fresco基本初始化 2.下载并且导入ZoomableDraweeView 它是实现大图的 ...

  2. oo 第一次博客作业

    oo 第一次博客作业 早在大一就听说了oo的各种传奇故事,大二下学期终于也开始了我的oo之旅. 基于度量来分析自己的程序结构 第一次作业 类图分析 耦合度分析 可以看出在第一次作业中,我的耦合度非常高 ...

  3. Linux 文件目录管理命令

    1.touch 用于设置空白文件或设置文件时间 touch命令参数及作用 参 数                                                             ...

  4. pyqt------对话框

    一 .输入文字 from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication) im ...

  5. __proto__、prototype和原型对象

    一.__proto__ 对象内部存在一个指针,用来指向上一层函数的原型对象.ECMA-262第五版中关这个指针叫[[prototype]],但Firefox.Safari和Chrome在每个对象上都支 ...

  6. 记一次Monolog的BufferHandler使用

    laravel中可以设置自定义的日记channel(config/logging中设置),按照laravel-china的一篇文章,把log按一定格式并且以批量的方式写入日志文件: https://l ...

  7. Spring Cloud基础教程

    Spring Cloud基础教程  2017-04-04 被围观 90375 次 该教程内容不定时更新,如您对这些内容感兴趣,可以关注我的博客或微信公众号! 本教程示例代码: GitHub:https ...

  8. c#+.net常用功能点

    1.比较差集合,排除不在集合中的,即data是data1排除data2剩下的数据 var data1 = new List<string>(); var data2 = new List& ...

  9. Oracle:查询各组最新的一条记录

    oracle中怎么查询各组中最新的一条记录呢?比如说现在有一个表中几条数据如下: 有两种写法:写法一:over partition by 分析函数 SELECT * FROM (select ID_, ...

  10. css第三天

    三 1.标准模式与怪异模式(针对盒子模型)与边框,填充,边界,有关 标准模式(默认):元素的总宽度 = 盒子的宽度 + 左右填充宽度 + 左右边框宽度元素的总高度 = 盒子的高度 + 上下填充高度 + ...