P2916 [USACO08NOV]为母牛欢呼Cheering up the C…

题目描述

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)

输入输出样例

输入样例#1:

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
输出样例#1:

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.

/*
以A~B为例子,AB这条边的边权就等于A点的值+B点的值+2*AB(因为这条路要来回走,共两遍)
然后再找点的值最小的一个点,作为出发点,就可以愉快地跑Kruskal了
注意答案开始为最小点的点权
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> #define N 10007
#define M 100007 using namespace std;
int head[M],fa[N],val[N];
int n,m,ans,cnt,x,y,z;
struct edge
{
int u,to,next,dis;
bool operator < (const edge &a) const{
return dis<a.dis;
} }e[M]; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
} void kruskal()
{
int num=;
sort(e+,e+m+);
for(int i=;i<=m;i++)
{
int sa=find(e[i].u),sb=find(e[i].to);
if(sa!=sb)
{
fa[sb]=sa;
num++;
ans+=e[i].dis;
}
if(num==n-) break;
}
printf("%d\n",ans);
} int main()
{
ans=0x7f7f7f7f;
n=read();m=read();
for(int i=;i<=n;i++)
{
val[i]=read();fa[i]=i;
ans=min(ans,val[i]);
}
for(int i=;i<=m;i++)
{
e[i].u=read();e[i].to=read();e[i].dis=read();
e[i].dis*=;e[i].dis+=val[e[i].u]+val[e[i].to];
}
kruskal();
return ;
}

洛谷P2916 [USACO08NOV]为母牛欢呼(最小生成树)的更多相关文章

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

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

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

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

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

  4. 洛谷P1546 最短网络 Agri-Net(最小生成树,Kruskal)

    洛谷P1546 最短网络 Agri-Net 最小生成树模板题. 直接使用 Kruskal 求解. 复杂度为 \(O(E\log E)\) . #include<stdio.h> #incl ...

  5. 洛谷 P1991 无线通讯网 Label:最小生成树 || 二分

    题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...

  6. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...

  7. 洛谷 P1546 最短网络 Agri-Net(最小生成树)

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P1546 首先不难看出这道题的思想是用了最小生成树,但是这道题有难点: 1.读题读不明白 2.不会读入 ...

  8. 洛谷 P1195 口袋的天空(最小生成树)

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P1195 思路: 首先可以判断这道题是用最小生成树来做的,然后在将其合并时用ans记录一下它的总消耗, ...

  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. 【sqli-labs】 less53 GET -Blind based -Order By Clause -String -Stacked injection(GET型基于盲注的字符型Order By从句堆叠注入)

    http://192.168.136.128/sqli-labs-master/Less-53/?sort=1';insert into users(id,username,password) val ...

  2. python mongodb压力测试脚本

    $ pip install pymongo #!/usr/bin/env python #coding=utf-8 #Author: Ca0Gu0 from pymongo import MongoC ...

  3. R语言数据重塑

    使用cbind()函数连接多个向量来创建数据帧.此外,使用rbind()函数合并两个数据帧   使用merge()函数合并两个数据帧.数据帧必须具有相同的列名称,在其上进行合并   melt()拆分数 ...

  4. 扩增子图表解读8网络图:节点OTU或类Venn比较

    网络图 Network 网络图虽然给人高大上的感觉,但是由于信息太多,无法给读者提供读有效的可读信息或是读者不知道该理解什么,总是让人望尔却步.那是因为大家太不了解网络,自己读不懂网络想表达的意思及其 ...

  5. 如何让字体大小<12px

    transform:scale( ) -webkit-transform:scale( )  

  6. word-spacing和letter-spacing区别

    word-spacing:单词与单词间的间距 letter-spacing:字母与字母间的间距

  7. Spring装配之——JAVA代码装配Bean

    首先创建几个普通的JAVA对象,用于测试JAVA代码装配bean的功能. package soundsystemJava; //作为接口 定义了CD播放器对一盘CD所能进行的操作 public int ...

  8. Django - 视图获取请求头

    1.urls.py(url和函数对应关系) 2.通过request.evniron,返回request的所有信息,用索引的方式,获取用户请求头信息. 3.也可以通过key,value方式,来展示请求头 ...

  9. Oracle query that count connections by minute with start and end times provided

    数据结构类似 SQL> select * from t; B                 E                 N ----------------- ------------ ...

  10. java折半插入排序

    代码如下: public class BinaryInsertSort { public static void binaryInsertSort(DataWrap [] data) { System ...