HDU 6041 I Curse Myself (仙人掌图)
来源:互联网 发布:电脑屏幕亮度调节软件 编辑:程序博客网 时间:2024/05/21 20:23
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
(∑Kk=1k⋅V(k))mod 232 .
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 31 2 11 3 21 4 313 31 2 12 3 23 1 346 71 2 41 3 23 5 71 5 32 4 12 6 26 4 57
Sample Output
Case #1: 6Case #2: 26Case #3: 493
题意
求一棵无向仙人掌图中前
思路
首先我们需要清楚什么是无向仙人掌图,它是一张连通图,且任意一条边都至多属于一个环。
也就是说,仙人掌图的一棵生成树就是其每个环去掉一条边所形成的图。
那么我们可以一次通过
PS:在最后求解前
AC 代码
#include<cstdio>#include<cstdlib>#include<cstring>#include<stdlib.h>#include<iostream>#include<queue>#include<vector>#include<map>#include<cmath>#include<algorithm>using namespace std;const int maxn = 2100;typedef pair<int,int> P;typedef __int64 LL;struct node{ int to; int next; int cost;} edge[maxn<<1];int head[maxn<<1],tot;int Stack[maxn],stop;bool instack[maxn];int n,m,k;bool isStart;bool vis[maxn];int g[maxn][maxn];int Bcnt;vector<int>G[maxn];vector<int> f,tmpf;LL mod = 1LL<<32;void init(){ memset(head,-1,sizeof(head)); memset(vis,false,sizeof(vis)); memset(instack,false,sizeof(instack)); tot=stop=Bcnt=0; isStart=false; f.clear(); tmpf.clear();}void addedge(int u,int v,int cost){ edge[tot].to=v; edge[tot].next=head[u]; edge[tot].cost=cost; head[u]=tot++;}void dfs(int x,int fa){ Stack[++stop]=x; instack[x]=true; vis[x]=true; for(int i=head[x]; i!=-1; i=edge[i].next) { int to=edge[i].to; if(to==fa)continue; if(!vis[to]) dfs(to,x); else if(instack[to]) // 找到仙人掌的一个环 { vector<int> G; int top=stop; int p=to; while(Stack[top]!=to) // 统计环中所有边长 { G.push_back(g[p][Stack[top]]); p=Stack[top--]; } G.push_back(g[p][Stack[top]]); sort(G.begin(),G.end(),[](const int &a,const int &b) // 边长从大到小排序 { return a>b; }); int len=G.size(); if(!isStart) // 第一次计算 { for(int j=0; j<len; j++) f.push_back(G[j]); isStart=true; } else { int cnt=f.size()*len; cnt=min(cnt,k); priority_queue<P> que; for(int j=0; j<len; j++) que.push(P(f[0]+G[j],0)); tmpf.clear(); while(cnt--) { P p=que.top(); que.pop(); tmpf.push_back(p.first); if(p.second!=(int)f.size()-1) { p.first-=f[p.second++]; p.first+=f[p.second]; que.push(p); } } swap(f,tmpf); } } } instack[x]=false; stop--;}int main(){ for(int ti=1; ~scanf("%d%d",&n,&m); ti++) { init(); LL cnt=0,ans=0; for(int i=0; i<m; i++) { int u,v,cost; cin>>u>>v>>cost; addedge(u,v,cost); addedge(v,u,cost); g[u][v]=g[v][u]=cost; cnt+=cost; } scanf("%d",&k); dfs(1,-1); int len=f.size(); if(!len)ans=cnt; for(int i=0; i<len; i++) ans=(ans+(1LL*(i+1)*(cnt-f[i]))%mod)%mod; printf("Case #%d: %I64d\n",ti,ans); } return 0;}
- HDU 6041 I Curse Myself (仙人掌图)
- HDU 6041 I Curse Myself(仙人掌+tanjan)
- 2017多校训练赛第一场 HDU 6041 I Curse Myself(仙人掌图生成树)
- HDU 6041 I Curse Myself
- HDU 6041 I Curse Myself
- hdu 6041 I Curse Myself [2017 Multi-University Training Contest
- HDU-6041 I Curse Myself(双连通分量+k路归并)
- HDU 6041 I Curse Myself(边双连通分量+优先队列)
- 2017多校第一场 HDU 6041 I Curse Myself Tarjan找环+K路归并
- 2017多校一 1009题 hdu 6041 I Curse Myself 找环(tarjan) + K 路归并
- 多校第一场I Curse Myself(HDU6041)
- HDU6041-I Curse Myself(k路归并)
- HDU 6041(仙人掌图+tarjan)
- hdu 3594 Cactus(仙人掌图)
- HDU 3594 Cactus (仙人掌图、Tarjan)
- HDU - 3594 Cactus(仙人掌图)
- HDU Cactus 3594 仙人掌图
- hdu 3594 Cactus (仙人掌图判定/SCC)
- python学习-面向对象进阶之python是动态语言(四)
- 笔记4 控制语句
- CountDownLatch和CyclicBarrier的使用和区别
- [WUA APIs]UpdateHistoryEntryCollection
- poj 1830 开关问题(高斯消元)
- HDU 6041 I Curse Myself (仙人掌图)
- java线程池简介
- Vue实战之后台管理系统(一)
- [WUA APIs]UpdateInstallationResult
- 高性能mysql笔记
- 零收获的一天
- 三点定位算法--用于平面2D定位
- 枚举类型(常量)
- [WUA APIs]UpdateInstaller