ACdream原创群赛(16) F

MST

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

SubmitStatus

Problem Description

Given a connected, undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together.  A single graph can have many different spanning trees. We can also assign a weight to each edge, which is a number representing
how unfavorable it is, and use this to assign a weight to a spanning tree by computing the sum of the weights of the edges in that spanning tree. A minimum spanning tree (MST) is then a spanning tree with weight less than or equal to the weight of every other
spanning tree.

------ From wikipedia

Now we make the problem more complex. We assign each edge two kinds of weight: length and cost. We call a spanning tree with sum of length less than or equal to others MST. And we want to find a MST who has minimal sum of cost.

Input

There are multiple test cases.

The first line contains two integers N and M indicating the number of vertices and edges in the gragh.

The next M lines, each line contains three integers a, b, l and c indicating there are an edge with l length and c cost between a and b.

1 <= N <= 10,000

1 <= M <= 100,000

1 <= a, b <= N

1 <= l, c <= 10,000

Output

For each test case output two integers indicating the sum of length and cost of corresponding MST.

If you can find the corresponding MST, please output "-1 -1".

Sample Input

4 5

1 2 1 1

2 3 1 1

3 4 1 1

1 3 1 2

2 4 1 3

Sample Output

3 3

题意比较坑人,可以存在不连通的图。

这是一个稀疏图,故用kruskal算法!!!

AC代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define inf 100000000
using namespace std;

int f[10005];

struct H{
    int l,r,d,c;
}w[100005];

int find (int a)
{
    return f[a]==a? a : f[a]=find(f[a]);
}

bool cmp(H a, H b)
{
    return a.d<b.d||(a.d==b.d&&a.c<b.c);//主要注意这个条件的判断就可以了
}
int main()
{
    int n,m;
    int i,j;
    int a,b,c,d;
    int ans;
    while(cin>>n>>m)
    {
        memset(w,0,sizeof w);
        for(i=1;i<=n;i++)
            f[i]=i;
        for(i=1;i<=m;i++)
        {
            cin>>a>>b>>c>>d;
            w[i].l=a;
            w[i].r=b;
            w[i].d=c;
            w[i].c=d;
        }
        int ans=0,cost=0,bj=0;
        sort(w+1,w+m+1,cmp);
        for(i=1;i<=m;i++)
        {
            int xx,yy;
            xx=find(w[i].l);
            yy=find(w[i].r);
            if(xx!=yy)
            {
                bj++;
                ans+=w[i].d;
                cost+=w[i].c;
                f[xx]=yy;
            }
        }
        if(bj==n-1)
        printf("%d %d\n",ans,cost);
        else printf("-1 -1\n");
    }
    return 0;
}

ACdream原创群赛(16) F

时间: 2024-10-10 11:22:05

ACdream原创群赛(16) F的相关文章

acdream原创群赛(16) --- B - Apple

<传送门> B - Apple Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description Alice and Bob are coming.This time, they are playing with apples. Initially, there are N baskets and M apples. Both bas

ACdream原创群赛(16) J

Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description You are given an N*N digit matrix and you can get several horizontal or vertical digit strings from any position. For example: 123 4

ACdream原创群赛(13)のwuyiqi退役专场 F The Arrow

F 首先是要理解概率求期望这种题目是用递归来解的! 大概规律就是: 完成事件A的期望是Ea,一次完成的概率是pa,那么 Ea = pa * Ea + (1 + Ea) * (1 - pa) 如果有好多种可能,比方说完成一个事件A的同时,也有诸如事件B的概率期望pb,Eb,事件C的概率期望pc,Ec等等等等,那么就分别写出来: Ea = pa * Ea + (1 + Ea) * (~pa) + pb * (1 + Eb) + pc * (1 + Ec) + ... 其中~pa是除了这些已知的事件发

ACdream原创群赛__15

这场感觉题目确实还算可以,不过,说好的每题10s效果上却不理想.这个时限还算比较紧.因为时间不是按绝对的多出几秒来计算,而是几倍来计算的. 比赛做的不好,后面又去做了一下. A:典型的数位DP,一直坑在这里. E:求f(f(f(n)))%p.f()表示斐波那契数.关于求斐波那契数模的循环节是有特定的数学定理和方法的.我也不知道,但是看了结论之后自己会实现了.首先,把p因数分解,ai^pi,显然最终的循环节就等于这些单独因子计算循环节的lcm.同时ai^pi的循环节又是G(ai)*ai^(pi-1

ACdream原创群赛(18)のAK&#39;s dream题解

只做了4题水题ADGI A题需要注意的就是“[...]”的输出了,何时输出,何时不输出. 1 #include <stdio.h> 2 int main() 3 { 4 int n, cur, d; 5 int cnt = 1; 6 while(scanf("%d%d%d",&n,&cur,&d)!=EOF) 7 { 8 printf("Case #%d: ",cnt++); 9 if(cur==1) 10 printf(&quo

ACdream原创群赛(18)のAK&#39;s dream

ACdream原创群赛(18)のAK's dream 题目链接 A:水题,直接模拟题意去搞即可 D:贪心+组合数学,把剑和英雄都从小到大排序,每次计算该英雄能用前几把剑cnt,cnt减去之前有多少人就是该英雄能选的,种数乘上cnt - 之前有多少人 F:DP,dp[i][j][2]表示第i天,用了j次修改,当前状态是A还是B,然后去转移即可 G:水题,直接用字符串处理即可,把数字都加上前导零方便判断 H:几何,利用正弦定理,余弦定理乱搞就可以了 I:水题,直接每个位置推出来输出,化简利用gcd即

ACdream原创群赛(13)のwuyiqi退役专场 H Salmon And Cat

H 首先是要姿势正确! 注意完美数的生成机: 2+2a+2b+ab ab都是完美数 假设生成完美数c c = 2 + 2a + 2b + ab c + 2 = ab+2a+2b+4 c + 2 = (a + 2)(b + 2) 然后一开始只有两个完美数1和3. 所以所有的完美数只有质因数分解之后都是类似于 N = (3 ^ x) * (5 ^ y) 但是5不是完美数. 然后就没事了... /**** *COPYRIGHT NOTICE *Copyright (c) 2014 *All right

ACdream原创群赛(13)のwuyiqi退役专场 C True love

True love Time Limit: 4000/2000 MS (Java/Others)     Memory Limit:128000/64000 KB (Java/Others) Problem Description Is there true love in the world?maybe or not, god knows! We know there are some complex relationships between ds and cxlove, they fell

ACdream原创群赛(11)の风神日华神专场 C.神奇的%系列一

3.算法综合实践——搜索引擎 上网搜索有关“搜索引擎”的相关资料,包括但不限于以下方面(至少要有2个方面):搜索引擎岗位要求.搜索引擎工作原理.搜索引 擎涉及到教材中哪些算法.搜索引擎的盈利模式.搜索引擎源码链接.国内外搜索引擎公司现状等. <1>搜索引擎指自动从因特网搜集信息,经过一定整理以后,提供给用户进行查询的系统.因特网上的信息浩瀚万千,而且毫无秩序,所有的信息像汪洋上的一个个小岛,网页链接是这些小岛之间纵横交错的桥梁,而搜索引擎,则为用户绘制一幅一目了然的信息地图,供用户随时查阅.