Cheering up the Cows,2008nov,usaco
来源:互联网 发布:手机python编程软件 编辑:程序博客网 时间:2024/05/16 14:46
那道题搞一搞会发现在选好的n-1 条边上,每条边走两次,还需要花掉两个端点需要的时间,才能回到原点。那么我们不妨把每个边权设成路长*2+两个端点的时间,这样建最小生成树就可以了。还有,回到出发点还要加上出发点的值。
#include<stdio.h>#include<algorithm>using namespace std;struct node{ int from; int to; int len;}edge[2000000];int cmp(node a,node b){ return a.len<b.len?1:0;}int n,m;int val[2000000];int f[2000000];int ans;void in(){ for(int i=1;i<=n;i++) { f[i]=i; }}int find(int x){ if(f[x]==x) { return x; }else { f[x]=find(f[x]); return f[x]; }}void kru(){ int cnt=0; for(int i=1;i<=m;i++) { int fx=find(edge[i].from); int fy=find(edge[i].to); if(fx!=fy) { cnt++; f[fy]=fx; ans+=edge[i].len; if(cnt==n-1) { return ; } } }}int main(){ scanf("%d %d",&n,&m); int min=9999999; for(int i=1;i<=n;i++) { scanf("%d",&val[i]); if(val[i]<min) { min=val[i]; } } for(int i=1;i<=m;i++) { int v; scanf("%d %d %d",&edge[i].from,&edge[i].to,&v); edge[i].len=val[edge[i].from]+val[edge[i].to]+v*2; } sort(edge+1,edge+1+m,cmp); in(); kru(); printf("%d",ans+min);}
0 0
- Cheering up the Cows,2008nov,usaco
- 【USACO 2008 Nov Gold】 2.Cheering up the Cows 最小生成树、
- 题解:cheering up the cows[USACO2008 NOV]
- HNU ACM: Cheering up the Cows (kruscal)
- 【USACO 2008 Nov Gold】 1.Mixed Up Cows 状压DP、
- USACO 2008 Nov Gold 1.Mixed Up Cows
- bzoj1231[USACO 2008 Nov]Mixed Up Cows混乱的奶牛
- usaco fencing the cows
- BZOJ 1619 USACO 2008 Nov Guarding the Farm 保卫牧场
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
- 洛谷P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
- USACO Section 5.1 Fencing the Cows
- usaco 5.1.1 Fencing the Cows
- USACO Fencing the Cows 解题报告
- 【usaco 2012 Nov Bronze】找奶牛Find the Cow!
- 【usaco 2012 Nov Bronze】找奶牛Find the Cow!
- [Usaco2013 Nov]Crowded Cows
- [Usaco2013 Nov]Crowded Cows
- 老板,你知道APP是怎么死的吗?
- Eclipse技巧:[5]eclipse js提示
- 【哈希表除留余数法+BSGS算法求离散对数】POJ Discrete Logging 2417
- Struts2笔记——ONGL表达式语言
- javascript float转int
- Cheering up the Cows,2008nov,usaco
- IoT service,mqtt,publish topic different from subscribe topic
- NOIP 2014 day1第二题 联合权值
- 各种页面高度
- VPN原理-举例说明
- php会话控制
- TCP协议滑动窗口协议以及拥塞控制算法
- socket套接字的使用
- GDT、GDTR、LDT、LDTR的理解 [zz]