3866 Cheering up the Cows kruskal 北师大oj

农夫约翰变得非常懒惰,不再想维护连接其N个牧场(5<=N<=10,000)的牛道。每个牧场都有一头牛。他计划删除尽可能多的路径,同时保持牧场连接。你需要确定保留哪些N-1条路径。输入包含N个牧场的访问时间和P条双向路径信息,输出最少的时间来每天至少访问每头牛一次。" 104944478,9333412,Sphinx搜索引擎操作指南,"['搜索技术', '数据库', 'Sphinx']
摘要由CSDN通过智能技术生成

Cheering up the Cows


Time Limit: 1000 ms     Case Time Limit: 1000 ms     Memory Limit: 65536 KB
Submit: 38     Accepted: 19

Description


Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths that currently provide a way to visit each of his N (5 <= N <= 10,000) pastures (conveniently numbered 1..N). Each and every pasture is home to one cow. FJ plans to remove as many of the P (N-1 <= P <= 100,000) paths as possible while keeping the pastures connected. You must determine which N-1 paths to keep.
Bidirectional path j connects pastures S_j and E_j (1 <= S_j <= N; 1 <= E_j <= N; S_j != E_j) and requires L_j (0 <= L_j <= 1,000) time to traverse. No pair of pastures is directly connected by more than one path.
The cows are sad that their transportation system is being reduced. You must visit each cow at least once every day to cheer her up. Every time you visit pasture i (even if you're just traveling
through), you must talk to the cow for time C_i (1 <= C_i <= 1,000).
You will spend each night in the same pasture (which you will choose) until the cows have recovered from their sadness. You will end up talking to the cow in the sleeping pasture at least in the morning when you wake up and in the evening after you have returned to sleep.
Assuming that Farmer John follows your suggestions of which paths to keep and you pick the optimal pasture to sleep in, determine the minimal amount of time it will take you to visit each cow at least once in a day.

Input


* Line 1: Two space-separated integers: N and P
* Lines 2..N+1: Line i+1 contains a single integer: C_i
* Lines N+2..N+P+1: Line N+j+1 contains three space-separated integers: S_j, E_j, and L_j

Output


* Line 1: A single integer, the total time it takes to visit all the cows (including the two visits to the cow in your sleeping-pasture)

Sample Input

5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6
4 5 12

Sample Output

176
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int u[100005],v[100005],w[100005],fath[100005],r[100005],t[100005];
int find(int x){return fath[x]==x?x:fath[x]=find(fath[x]);}
bool cmp(int x,int y) { return w[x]<w[y];}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int minc=(1<<31)-1;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&t[i]);
            if(t[i]<minc) minc=t[i];
            fath[i]=i;
        }
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            u[i]=x,v[i]=y;w[i]=z*2+t[x]+t[y];
            r[i]=i;
        }
        sort(r,r+m,cmp);
        int ans=0;
        for(int i=0;i<m;i++)
        {
            int e=r[i];
            int x=find(u[e]),y=find(v[e]);
            if(x!=y)
            {
                ans+=w[e];
                fath[x]=y;
            }
        }
        cout<<ans+minc<<endl;
    }
    return 0;
}               
                         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值