hdu 4896 Minimal Spanning Tree

Minimal Spanning Tree

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 144    Accepted Submission(s): 44

Problem Description

Given a connected, undirected, weight graph G, your task is to select a subset of edges so that after deleting all the other edges, the graph G is still connected. If there are multiple ways to do this, you should choose the way that minimize the sum of the weight of these selected edges.

Please note that the graph G might be very large. we‘ll give you two numbers: N, and seed, N is number of nodes in graph G, the following psuedo-code shows how to to generate graph G.

Here ⊕ represents bitwise xor (exclusive-or).

Input

Input contains several test cases, please process till EOF.
For each test case, the only line contains two numbers: N and seed.(1 ≤ N ≤ 10000000, 1 ≤ seed ≤ 2333332)

Output

For each test case output one number, the minimal sum of weight of the edges you selected.

Sample Input

6 2877

2 17886

3 22452

Sample Output

2157810

259637

1352144

Author

Fudan University

我们打表发现x的周期是54,然后又发现边的权值周期也是54,我们就去暴力生成树权值的增量,会发现ans[i]-ans[i-54]

在i 》 某个值是是成立的,这样我们暴力前108个,然后按周期增量处理

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define maxn 10010
#define mod 2333333
#define LL long long
using namespace std;

struct node
{
    int u,v,val ;
    bool operator<(const node&s) const
    {
        return val < s.val ;
    }
}qe[maxn];
int fa[maxn] ;
LL ans[maxn] ;
int find(int x )
{
    if(x != fa[x])
        fa[x] = find(fa[x]) ;
    return fa[x] ;
}
LL get(int n )
{
    LL ans = 0 ;
    sort(qe,qe+n) ;
    for(int i = 0 ; i < n ;i++ )
    {
        int u = find(qe[i].u) ;
        int v = find(qe[i].v) ;
        if(u==v) continue ;
        ans += qe[i].val ;
        fa[u] = v ;
    }
    return ans ;
}
int main()
{
    int i ,k,j ;
    int x , n , m ;
    while( scanf("%d%d",&n,&x ) != EOF )
    {
        m  = 0 ;
        for( i = 2 ; i <= 108 ;i++)
        {
            x = x*907%mod ;
            int T = x ;
            for(int j = 1 ; j <= i ;j++)
                fa[j]=j;
            for( j = max(1,i-5) ; j <= i-1 ;j++ )
            {
                x = 907*x%mod ;
                int w = T^x ;
                qe[m].u=i;
                qe[m].v=j;
                qe[m++].val=w ;
            }
            ans[i] = get(m) ;
           // if(i > 108) cout << ans[i]-ans[i-54] << endl;
        }
        if(n <= 108) cout << ans[n] << endl;
        else
        {
            cout << ans[54+n%54]+(n/54-1)*(ans[108]-ans[54])<< endl ;
        }
    }
    return 0 ;
}

  

hdu 4896 Minimal Spanning Tree

时间: 2025-01-16 00:58:19

hdu 4896 Minimal Spanning Tree的相关文章

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

hdu 2489 Minimal Ratio Tree 枚举+最小生成树

点的总数很小,直接枚举就好. #include <stdio.h> #include <string.h> #define N 20 #define inf 1000000 int mk[N],n,k,ans[N]; double low[N],val[N]; double map[N][N],MIN; double prim() { int i,j; double sum=0; double tot=0; for(i=1;i<=n;i++) low[i]=inf; int

HDU 2489 Minimal Ratio Tree(数据结构-最小生成树)

Minimal Ratio Tree Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a

HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges

HDU 2489 Minimal Ratio Tree

Minimal Ratio Tree Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 248964-bit integer IO format: %I64d      Java class name: Maina For a tree, which nodes and edges are all weighted, the ratio of it is calcul

hdu 2489 Minimal Ratio Tree DFS枚举点+最小生成树 属于中等偏上题 ,Double比较大小的时候注意精度问题

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2835    Accepted Submission(s): 841 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a complete graph of n nodes with all nodes and edges weighted, your task is to find a tree, which is a su

HDU 2489 Minimal Ratio Tree(prim+DFS)

Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3345    Accepted Submission(s): 1019 Problem Description For a tree, which nodes and edges are all weighted, the ratio of it i