嘟嘟嘟

这道题删完边后是一棵树,那么一定和最小生成树有关啦。

考虑最后的生成树,无论从哪一个点出发,每一条边都会访问两次,而且在看一看样例,会发现走第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的更多相关文章

  1. P2916 [USACO08NOV]安慰奶牛Cheering up the Cow

    往奶牛里打气 题目评级不难. 感觉思路有值得借鉴的地方.(虽然少,毕竟积沙成塔吗qwq) 很容易看出来,是要求最小生成树的. 然后生成树的计算方式不一样. 我们考虑拼接(感觉大部分oi都可以使用类似的 ...

  2. [USACO08NOV]安慰奶牛Cheering up the Cow BZOJ 1232 Kruskal

    Farmer John变得非常懒, 他不想再继续维护供奶牛之间供通行的道路. 道路被用来连接N (5 <= N <= 10,000)个牧场, 牧场被连续地编号为1..N. 每一个牧场都是一 ...

  3. 安慰奶牛Cheering up the Cow

    传送门 一次a就很开心 可以当作kruskal模板题(orz --------------------------------------------------------------------- ...

  4. 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…

    题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...

  5. 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...

  6. 洛谷——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 ...

  7. 【题解】P2916 [USACO08NOV]安慰奶牛Cheering up the Cow-C++

    原题传送门 这道题用最小生成树来完成,我选用的是kruskal(克鲁斯卡尔)来完成.这道题目在克鲁斯卡尔模板的基础上,有变动的地方只有2处:1.因为必须从一个点出发,而最小生成树最后会让所有点都连通, ...

  8. 洛谷P2916 [USACO08NOV]为母牛欢呼(最小生成树)

    P2916 [USACO08NOV]为母牛欢呼Cheering up the C… 题目描述 Farmer John has grown so lazy that he no longer wants ...

  9. 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

随机推荐

  1. java性能监控工具jstat

    jstat Monitors Java Virtual Machine (JVM) statistics. This command is experimental and unsupported. ...

  2. Java新建线程的两种方式

    Java新建线程有两种方式,一种是通过继承Thread类,一种是实现Runnable接口,下面是新建线程的两种方式. 我们假设有个竞赛,有一个选手A做俯卧撑,一个选手B做仰卧起坐.分别为两个线程: p ...

  3. 【 js 片段 】移动端适配简易步骤

    手机端全部适配简易步骤,不确保没有bug. 1.HTML 的 head 部分中加入如下代码: <meta name="viewport" content="widt ...

  4. css中的线及vertical-align

    行内元素格式化顺序: 相关概念:  leading(行间距):指填充在两行文字间的铅条,等于line-height 和 font-size之差,其中一半leading加到文字上方,另一半leading ...

  5. HTMLcanvas矩形阵雨 - 学习笔记

    HTMLcanvas矩形阵雨 在画布上执行 获取制图环境 全屏获取屏幕宽度和屏幕高度 确定每个文字的宽度 以确定列 循环输出 定时器调用 HTML 部分 <!DOCTYPE HTML> & ...

  6. css对ie的兼容性问题处理(一):

    1.在制作sidebar时对li里面的元素进行浮动,li在ie6/7下会出现4px的间间隙: 解决方法:在li下加上vertical-align属性,值可为top.bottom.middle: 扩展: ...

  7. 深入解析 ERP 计划的各个层次

    ERP 生产计划管理按照 ERP 计划的层次主要分为:经营规划.销售和运作规划.主生产计划.物料需求计划.能力需求计划.执行能力计划.执行物料计划等. 经营规划是企业的战略规划,用于确定企业经营目标和 ...

  8. Ubuntu VNC 打开spyder无法输入(检测不到键盘配置)解决方法

    在ubuntu中安装好spyder后, 打开spyder发现无法输入. 在打开spyder的终端窗口,有如下提示: QXcbConnection: Failed to initialize XRand ...

  9. flutter控件之RadioButton

    import 'package:flutter/material.dart'; class LearnRadioButton extends StatefulWidget{ @override Sta ...

  10. Memory map of an object array

    Student类: package com.itheima; /* * 自动生成构造方法: * 代码区域右键 -- Source -- Generate Constructors from Super ...