[USACO08NOV]Cheering up the Cow
这道题删完边后是一棵树,那么一定和最小生成树有关啦。
考虑最后的生成树,无论从哪一个点出发,每一条边都会访问两次,而且在看一看样例,会发现走第w条边(u, v)的代价是L[w] * 2 + c[u] + c[v],所以说把每一条边的边权改为这个,然后跑裸的最小生成树就行了。然后答案还要加上出发的点的权值,那自然要加权值最小的点。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
#define space putchar(' ')
#define enter puts("")
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e4 + ;
const int max_size = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << ) + (ans << ) + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, m, a[maxn];
struct Node
{
int x, y, cost;
bool operator < (const Node& other)const
{
return cost < other.cost;
}
}t[max_size]; int p[maxn];
void init()
{
for(int i = ; i <= n; ++i) p[i] = i;
}
int Find(int x)
{
return x == p[x] ? x : p[x] = Find(p[x]);
} int ans = , cnt = , Min = INF; int main()
{
n = read(); m = read();
for(int i = ; i <= n; ++i) a[i] = read(), Min = min(Min, a[i]);
for(int i = ; i <= m; ++i)
{
t[i].x = read(); t[i].y = read(); t[i].cost = read();
t[i].cost = (t[i].cost << ) + a[t[i].x] + a[t[i].y];
}
sort(t + , t + m + );
init();
for(int i = ; i <= m; ++i)
{
int px = Find(t[i].x), py = Find(t[i].y);
if(px != py)
{
p[px] = py;
ans += t[i].cost; cnt++;
if(cnt == n - ) break;
}
}
write(ans + Min); enter;
return ;
}
[USACO08NOV]Cheering up the Cow的更多相关文章
- P2916 [USACO08NOV]安慰奶牛Cheering up the Cow
往奶牛里打气 题目评级不难. 感觉思路有值得借鉴的地方.(虽然少,毕竟积沙成塔吗qwq) 很容易看出来,是要求最小生成树的. 然后生成树的计算方式不一样. 我们考虑拼接(感觉大部分oi都可以使用类似的 ...
- [USACO08NOV]安慰奶牛Cheering up the Cow BZOJ 1232 Kruskal
Farmer John变得非常懒, 他不想再继续维护供奶牛之间供通行的道路. 道路被用来连接N (5 <= N <= 10,000)个牧场, 牧场被连续地编号为1..N. 每一个牧场都是一 ...
- 安慰奶牛Cheering up the Cow
传送门 一次a就很开心 可以当作kruskal模板题(orz --------------------------------------------------------------------- ...
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows
题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...
- 洛谷——P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows
https://www.luogu.org/problem/show?pid=2916 题目描述 Farmer John has grown so lazy that he no longer wan ...
- 【题解】P2916 [USACO08NOV]安慰奶牛Cheering up the Cow-C++
原题传送门 这道题用最小生成树来完成,我选用的是kruskal(克鲁斯卡尔)来完成.这道题目在克鲁斯卡尔模板的基础上,有变动的地方只有2处:1.因为必须从一个点出发,而最小生成树最后会让所有点都连通, ...
- 洛谷P2916 [USACO08NOV]为母牛欢呼(最小生成树)
P2916 [USACO08NOV]为母牛欢呼Cheering up the C… 题目描述 Farmer John has grown so lazy that he no longer wants ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
随机推荐
- 【转】分布式环境下5种session处理策略(大型网站技术架构:核心原理与案例分析 里面的方案)
前言 在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理.如果不做任何处理的话,用户将出现频繁登录的现象,比如集群中存在A.B两台服务器,用户在第一次访问网站时,Ngin ...
- elasticsearch 6.3 安装手记
系统环境 centos 7 elasticsearch 6.3 需要 JDK 8 版本,先安装 JDK 8. ES6.3 安装地址: https://www.elastic.co/guide/en/e ...
- Tomcat启动慢原因之一 At least one JAR was scanned for TLDs yet contained no TLDs
Tomcat启动时提示: 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging f ...
- github for window 中 git shell 设置代理方法和解决ssl证书错误的问题
体验了一下传说中的 github for windows(操作git有很多的方法,我还没有学会,所以找了个简单的方法),听说用起来还不错,毕竟也开始接触了github.下载地址是 http://win ...
- opencv3.2.0 分离颜色通道&多通道图像混合
##名称:分离颜色通道&多通道图像混合 ##平台:QT5.7.1+OpenCV3.2.0 ##时间:2017年12月11日 /***************创建QT控制台程序********* ...
- Spring Boot--02MVC设置
package com.smartmap.sample.ch1.conf; import java.util.List; import javax.servlet.http.HttpServletRe ...
- BASE_DIR 拼接文件路径
# C:/Disk_D/pycharm_stu/macboy/blog/views/tt.py 当前文件绝对路径 BASE_DIR = os.path.dirname(os.path.dirname( ...
- 二、ionic如何使用外链
1.ionic如何使用外链并返回原有页面? html如下: 对应的controller如下: (function() { angular.module('app').controller('extra ...
- 2.CSS使用基础(1)
目录 一.css颜色的四种表示法 二.使用css 4种方式 三.css注释: 四.CSS Id 和 Class选择器 五.多重样式优先级顺序 六.CSS Backgrounds(背景) 七.CSS T ...
- [C#] Microsoft .Net框架SerialPort类的用法与示例
从Microsoft .Net 2.0版本以后,就默认提供了System.IO.Ports.SerialPort类,用户可以非常简单地编写少量代码就完成串口的信息收发程序.本文将介绍如何在PC端用C# ...