https://www.luogu.org/problem/show?pid=2916

题目描述

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.

#include <algorithm>
#include <iostream>
#define maxn 1000000 using namespace std; int n,p,s,t,l,tot,ans=maxn,num;
int c[],fa[];
struct node
{
int u,v,w;
}e[]; void add(int a,int b,int c)
{
tot++;
e[tot].u=a;
e[tot].v=b;
e[tot].w=c;
} int find(int x)
{
if(x!=fa[x])
return fa[x]=find(fa[x]);
return x;
} bool cmp(node a,node b)
{
return a.w<b.w;
} int main()
{
cin>>n>>p;
for(int i=;i<=n;i++)
{
fa[i]=i;
cin>>c[i];
ans=min(c[i],ans);
}
for(int i=;i<=p;i++)
{
cin>>s>>t>>l;
add(s,t,l*+c[s]+c[t]);
}
sort(e+,e+tot+,cmp);
for(int i=;i<=p;i++)
{
int xx=find(e[i].u),yy=find(e[i].v);
if(xx!=yy)
{
fa[xx]=yy;
num++;
ans+=e[i].w;
}
if(num==n-)
{
cout<<ans;
return ;
}
}
return ;
}

最新文章

  1. github 使用网址
  2. [PHP] 读取大文件并显示
  3. Datagrid扩展方法onClickCell{easyui-datagrid-扩充-支持单元格编辑}
  4. LVS测试小结
  5. linux 安装sysstat使用iostat、mpstat、sar、sa(转载)
  6. Middleware
  7. apply方法别有他用!
  8. 小米2s 用线刷,刷回MIUI V5了
  9. Java知识总结--三大框架
  10. 2014年2月5日 Oracle ORACLE的工作机制[转]
  11. sqlplus命令手册
  12. C++符号优先级
  13. Android下利用zbar类库实现扫一扫
  14. 51单片机开发板(W25Q16学习)
  15. yolo3的改变
  16. 一个正整数表示为n个连续正整数之和(第1届第2题)
  17. SQL复制数据表 (select * into 与 insert into)
  18. 【转】你真的懂select Socket模型吗?
  19. Gradle入门(5):创建二进制发布版本
  20. MySQL建表字段类型

热门文章

  1. Cognos添加关联字段
  2. codevs 1316 文化之旅 2012年NOIP全国联赛普及组
  3. Vue踩坑第一步,安装Vue最新版本
  4. SAP CRM点了附件的超链接后报错的处理方式
  5. Java异常归纳
  6. 爬虫学习之csv读取和存储
  7. 【转】C#中的==、Equal、ReferenceEqual
  8. [jzoj5073 GDOI2017第二轮模拟] 影魔
  9. springMVC中处理静态资源的几种方案
  10. UI进阶 即时通讯之XMPP环境搭建