洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows
题目描述
Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths that currently provide a way to visit each of his N (5 <= N <= 10,000) pastures (conveniently numbered 1..N). Each and every pasture is home to one cow. FJ plans to remove as many of the P (N-1 <= P <= 100,000) paths as possible while keeping the pastures connected. You must determine which N-1 paths to keep.
Bidirectional path j connects pastures S_j and E_j (1 <= S_j <= N; 1 <= E_j <= N; S_j != E_j) and requires L_j (0 <= L_j <= 1,000) time to traverse. No pair of pastures is directly connected by more than one path.
The cows are sad that their transportation system is being reduced. You must visit each cow at least once every day to cheer her up. Every time you visit pasture i (even if you're just traveling
through), you must talk to the cow for time C_i (1 <= C_i <= 1,000).
You will spend each night in the same pasture (which you will choose) until the cows have recovered from their sadness. You will end up talking to the cow in the sleeping pasture at least in the morning when you wake up and in the evening after you have returned to sleep.
Assuming that Farmer John follows your suggestions of which paths to keep and you pick the optimal pasture to sleep in, determine the minimal amount of time it will take you to visit each cow at least once in a day.
For your first 10 submissions, you will be provided with the results of running your program on a part of the actual test data.
POINTS: 300
约翰有N个牧场,编号依次为1到N。每个牧场里住着一头奶牛。连接这些牧场的有P条道路,每条道路都是双向的。第j条道路连接的是牧场Sj和Ej,通行需要Lj的时间。两牧场之间最多只有一条道路。约翰打算在保持各牧场连通的情况下去掉尽量多的道路。
约翰知道,在道路被强拆后,奶牛会非常伤心,所以他计划拆除道路之后就去忽悠她们。约翰可以选择从任意一个牧场出发开始他维稳工作。当他走访完所有的奶牛之后,还要回到他的出发地。每次路过牧场i的时候,他必须花Ci的时间和奶牛交谈,即使之前已经做过工作了,也要留下来再谈一次。注意约翰在出发和回去的时候,都要和出发地的奶牛谈一次话。请你计算一下,约翰要拆除哪些道路,才能让忽悠奶牛的时间变得最少?
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and P
Lines 2..N+1: Line i+1 contains a single integer: C_i
- Lines N+2..N+P+1: Line N+j+1 contains three space-separated
integers: S_j, E_j, and L_j
输出格式:
- Line 1: A single integer, the total time it takes to visit all the cows (including the two visits to the cow in your
sleeping-pasture)
输入输出样例
5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6
4 5 12
176
说明
+-(15)-+
/ \
/ \
1-(5)-2-(5)-3-(6)--5
\ /(17) /
(12)\ / /(12)
4------+
Keep these paths:
1-(5)-2-(5)-3 5
\ /
(12)\ /(12)
*4------+
Wake up in pasture 4 and visit pastures in the order 4, 5, 4, 2, 3, 2, 1, 2, 4 yielding a total time of 176 before going back to sleep.
最小生成树
#include <algorithm>
#include <cstdio> using namespace std;
#define Max 100000 struct node
{
int x,y,z;
}edge[Max*]; int i,fa[],N,p,tot,c[];
int find_father(int x)
{
if(x==fa[x]) return x;
else return fa[x]=find_father(fa[x]);
}
bool cmp(node a,node b)
{
return a.z<b.z;
}
int main()
{
int ans=0x7fffffff;
int d;
scanf("%d%d",&N,&p);
for(i=;i<=N;++i)
{
scanf("%d",&c[i]);
ans=min(ans,c[i]);
}
for(i=;i<=p;++i)
{
scanf("%d%d%d",&edge[i].x,&edge[i].y,&d);
edge[i].z=d*+c[edge[i].x]+c[edge[i].y];
}
sort(edge+,edge++p,cmp);
for(i=;i<=N;++i) fa[i]=i;
int k=;
for(i=;i<=p;++i)
{
int fx=find_father(edge[i].x),fy=find_father(edge[i].y);
if(fx!=fy)
{
fa[fx]=fy;
ans+=edge[i].z;
k++;
if(k==N-) break;
}
}
printf("%d",ans);
return ;
}
洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows的更多相关文章
- 洛谷——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 C…
题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...
- 洛谷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 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...
- 洛谷——P2846 [USACO08NOV]光开关Light Switching
P2846 [USACO08NOV]光开关Light Switching 题目大意: 灯是由高科技——外星人鼠标操控的.你只要左击两个灯所连的鼠标, 这两个灯,以及之间的灯都会由暗变亮,或由亮变暗.右 ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷 P2918 [USACO08NOV]买干草Buying Hay 题解
P2918 [USACO08NOV]买干草Buying Hay 题目描述 Farmer John is running out of supplies and needs to purchase H ...
随机推荐
- Summit Online Judge
题意: 询问将取值在 $[L,R]$ 的若干个整数相加,可以得到 $[x,y]$ 区间内多少个数字. 解法: 只需要考虑求 $[L,R]$ 的数字能凑出 $[1,n]$ 的多少个数字,即可得出答案. ...
- JSON 下 -- jansson 示例
JSON 下 —— jansson 示例 参考网址: jansson 库的下载: http://www.digip.org/jansson/ 安装jansson 步骤: http://blog.csd ...
- 执行多个Sql脚本,Sqlplus
1.先制作需要执行的Sql文件list CMD 中输入[dir E:\FolderName >E:\ExcuteSqlList.txt ] 2.根据ExcuteSqlList.txt 中的文件名 ...
- MySQL基础知识(一)-超详细MySQL安装教程
简介 原计划,今天这篇想要给小伙伴们讲解一下python操作mysql数据库,但是由于近期换了一台新的电脑,所以一看mysql数据库都没安装,所有才有了这篇文章.尽管网上不乏此类型的文章,但是刚好自己 ...
- 单片机C基本编程规范
为了提高源程序的质量和可维护性,从而最终提高软件产品生产力,特编写此规范.本标准规定了程序设计人员进行程序设计时必须遵循的规范.本规范主要针对单片机编程语言和08编译器而言,包括排版.注释.命名.变量 ...
- 51Nod 1873 初中的算术
大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...
- iOS开发 - 多线程实现方案之Pthread篇
pthread基础 pthread是POSIX thread的简写,一套通用的多线程API,适用于Unix.Linux.Windows等系统,跨平台.可移植,使用难度大,C语言框架,线程生命周期由程序 ...
- 测试 | 代码覆盖测试工具 | Eclemma
安装: 打开eclipse,点击Help菜单下的Install New Software 在弹出的对话框中,点击Add 输入Name,如EclEmma 输入Location: http://updat ...
- UILabel和UIbutton(富文本)封装方法
/** 方法说明:设置label的富文本属性 参数说明:contentStr富文本内容 textColor字体颜色 rangeSet设置字体颜色及大小的位置 */ - (UILabel *)backf ...
- ES5(基本包装类型)字符串的方法
看一下字符串有哪些常用的方法: 1.concat();将多个文本组合起来,返回新的字符串,就是拼接字符串. 查找位置 2.indexOf();返回要匹配的字符在字符串第一次出现的索引,参数就是匹配的字 ...