[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 ...
随机推荐
- 设置navigationbar透明度时的坑
1.需要设置导航条透明度时 UIImage *image = [UIImage imageNamed:@"bg_clear.png"]; //设置背景颜色 [nav2.na ...
- Android全屏的两种方法
在开发中我们经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方法是在配置文件里改! 一.在代码中设置: [java] view plain copy package ...
- static dictionary methods of text compression
Now I will introduce a way to compress a text. When we are confronted with numerous data, and the da ...
- 有序的数据结构中:内存操作红黑树快,磁盘操作b+树快
红黑树常用于存储内存中的有序数据,增删很快,b+树常用于文件系统和数据库索引,因为b树的子节点大于红黑树,红黑树只能有2个子节点,b树子节点大于2,子节点树多这一特点保证了存储相同大小的数据,树的高度 ...
- Java的工厂模式(二)
除了上文提到的方法之外,还可以使用Java的反射机制,这样就能使用类名称来加载所需要的类.我们只需改变工厂类和驱动类就可以了. FruitFactory.java package com.muggle ...
- span与a元素的键盘聚焦性以及键盘点击性研究——张鑫旭
一.众所周知的与不为所知的 我们平时涉及点击交互事件的时候,都是使用的a元素或者button元素,原因是可以相应键盘focus效果以及回车触发点击事件,这是众所周知的. 但是,可能存在一些特殊情况,我 ...
- 【代码笔记】iOS-自动成表格的效果
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import "LabelOnBackImag ...
- jQuery筛选结果等于true的筛选
一般来说,使用jQuery筛选器的筛选结果都是数组,可以用length>0来判断是否筛选到:但是有一些筛选结果是布尔值,需要注意: 1.hasClass 2.is
- intent调用代码总结
进入联系人界面 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(People.CO ...
- Pwn with File结构体之利用 vtable 进行 ROP
前言 本文以 0x00 CTF 2017 的 babyheap 为例介绍下通过修改 vtable 进行 rop 的操作 (:-_- 漏洞分析 首先查看一下程序开启的安全措施 18:07 haclh@u ...