• ACdream 1135 MST


    MST

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

    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
    

    Source

    dut200901102

    Manager

     
    解题:是的,没错,就是MST,只是是双关键字排序
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 500010;
     5 struct arc{
     6     int u,v,length,cost;
     7     bool operator<(const arc &rhs)const{
     8         if(length == rhs.length) return cost < rhs.cost;
     9         return length < rhs.length;
    10     }
    11 }e[maxn];
    12 int uf[maxn];
    13 int Find(int x){
    14     if(x != uf[x]) uf[x] = Find(uf[x]);
    15     return uf[x];
    16 }
    17 int main(){
    18     int n,m;
    19     while(~scanf("%d%d",&n,&m)){
    20         for(int i = 0; i < m; ++i)
    21         scanf("%d%d%d%d",&e[i].u,&e[i].v,&e[i].length,&e[i].cost);
    22         for(int i = 0; i <= n; ++i) uf[i] = i;
    23         sort(e,e + m);
    24         LL length = 0,cost = 0,cnt = 0;
    25         for(int i = 0; i < m && cnt + 1 < n; ++i){
    26             int u = Find(e[i].u);
    27             int v = Find(e[i].v);
    28             if(u == v) continue;
    29             uf[u] = v;
    30             length += e[i].length;
    31             cost += e[i].cost;
    32             ++cnt;
    33         }
    34         if(cnt + 1 == n) printf("%lld %lld
    ",length,cost);
    35         else puts("-1 -1");
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    【基础】Oracle 表空间和数据文件
    oracle学习笔记1:windows下oracle数据库安装及.net调用oracle数据库
    redis中文网站
    .NET中的消息队列
    .Net下的进程间的通讯 -- Windows消息队列
    1060 最复杂的数(反素数玄学dfs)
    1282 时钟(最小表示法+hash)
    1191 消灭兔子(贪心+优先队列)
    1366 贫富差距(floyed)
    1503 猪和回文(DP)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4970306.html
Copyright © 2020-2023  润新知