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. CSS3:变换和动画

    <html> <style> .container{ -webkit-perspective: 800; -webkit-perspective-origin: 50% 40% ...

  2. 【YOLO】实时对象检测使用体验

    官网:https://pjreddie.com/darknet/yolo/ 以下全部在服务器上完成,服务器上是有opencv等. 1.安装Darknet git clone https://githu ...

  3. Ajax无刷新显示

    前台页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1. ...

  4. Python 之pygame飞机游戏

    import pygame from pygame.locals import * import time import random # 我机 class HeroPlane(object): de ...

  5. nginx+tomcat实现负载均衡集群

    一台tomcat站点由于可能出现单点故障及无法应付过多客户复杂多样的请求等问题,不能单独应用于生产环境下 所以需要一套可靠的解决方案来完善web站点架构 而Nginx只能访问静态页面,如果需要动态需要 ...

  6. Django - 数据获取

    Django - 数据获取 1.radio值获取 2.checkbox获取 3.select 获取 select 获取值,需要根据前端multiple来获取,get or getlist; 4.上传文 ...

  7. php第十七节课

    分页查询 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  8. SDOI2018退役记

    在NOIp2017中,我意识到自己啥也不会.如今SDOI2018快来了,自己还是啥也不会.高一两次考试注定以打两次酱油告终.还是记录一下,到NOIp之后如果还没有退役的话,那这个博客可能还会继续更新吧 ...

  9. PAT 1060. Are They Equal

    If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...

  10. css的基本操作学习--css样式,选择器,hover,文字属性,文本属性,背景

    什么是css? 通配符选择器 <head> /* *通配符选择器 匹配任何元素 */ *{ margin: 0; padding: 0; } </head> css样式有三种 ...