51nod 1640 天氣晴朗的魔法

來源:互聯網
上載者:User

標籤:

51nod魔法學校近日開展了主題為“天氣晴朗”的魔法交流活動。
N名魔法師按陣法站好,之後選取N - 1條魔法鏈將所有魔法師的魔力串連起來,形成一個魔法陣。
魔法鏈是做法成功與否的關鍵。每一條魔法鏈都有一個魔力值V,魔法最終的效果取決於陣中所有魔法鏈的魔力值的和。
由於逆天改命的魔法過於暴力,所以我們要求陣中的魔法鏈的魔力值最大值儘可能的小,與此同時,魔力值之和要儘可能的大。
現在給定魔法師人數N,魔法鏈數目M。求此魔法陣的最大效果。Input
兩個正整數N,M。(1 <= N <= 10^5, N <= M <= 2 * 10^5)接下來M行,每一行有三個整數A, B, V。(1 <= A, B <= N, INT_MIN <= V <= INT_MAX)保證輸入資料合法。
Output
輸出一個正整數R,表示合格魔法陣的魔力值之和。
Input樣本
4 61 2 31 3 11 4 72 3 42 4 53 4 6
Output樣本
12
Wizmann (題目提供者)思路:直接先一次克魯斯卡爾找到產生樹裡面最小的邊,然後用這個最小的值找到所有小於等於這個邊權的邊,用這些邊做一次最大產生樹就是答案。
#include<iostream>#include<algorithm>#include<cstdio>#include<queue>#include<map>#include<vector>#include<cstring>#include<cmath>using namespace std;typedef long long ll;const int inf =0x3f3f3f3f;const double  pi = acos(-1.0);const int N = 3e5 + 10;int root[N];struct node{    int x, y;    ll w;} p[N];int Find(int x){    return x == root[x]?x:root[x] = Find(root[x]);}int cmp1(node a, node b){    return a.w<b.w;}int cmp2(node a, node b){    return a.w>b.w;}int main(){    int n, m;    scanf("%d%d", &n, &m);    for(int i = 0; i<m; i++)    {        scanf("%d%d%I64d", &p[i].x, &p[i].y, &p[i].w);    }    for(int i = 0; i<=n; i++)        root[i] = i;    sort(p, p+m, cmp1);    int k, num = 0;    ll last = -inf*100LL;    for(int i = 0; i<m; i++)    {        int a = Find(p[i].x), b = Find(p[i].y);        if(a!=b)        {            num++;            last = max(last, p[i].w);            root[a] = b;        }        if(num == n-1)        {            break;        }    }    for(int i = 0; i<=n; i++)        root[i] = i;    for(int i = 0; i<m; i++)    {        if(p[i].w>last)        {            k = i;            break;        }    }    sort(p, p+k, cmp2);    ll ans = 0;    num = 0;    for(int i = 0; i<k; i++)    {        int a = Find(p[i].x), b = Find(p[i].y);        if(a!=b)        {            num++;            ans += p[i].w;            root[a] = b;        }        if(num == n-1)            break;    }    cout<<ans<<endl;    return 0;}


51nod 1640 天氣晴朗的魔法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.