题面链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1232
题意:实在懒得转述了...
显然每条边需要走两次,将边权置为边权*2+端点点权即可,最小生成树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <cmath> #include <queue> #include <cstdio> #include <iomanip> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define N 10010 #define M 100010 #define inf 1000000000 #define ll long long using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int fa[N]; int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);} struct zgz { int from,to,val; }edge[M]; int cnt; bool cmp(zgz a,zgz b) {return a.val<b.val;} int n,m,ans,c[N]; int main() { n=read(),m=read();ans=inf; for(int i=1;i<=n;i++) { fa[i]=i; c[i]=read();ans=min(ans,c[i]); } for(int i=1;i<=m;i++) { int u=read(),v=read(),w=read(); w=(w<<1)+c[u]+c[v]; edge[i].from=u,edge[i].to=v,edge[i].val=w; } sort(edge+1,edge+1+m,cmp); for(int i=1;i<=m;i++) { int u=edge[i].from,v=edge[i].to; u=find(u),v=find(v); if(u!=v) { cnt++; ans+=edge[i].val; fa[u]=v; } if(cnt==n-1)break; } printf("%d\n",ans); } |
Comments | NOTHING