当前位置 : 主页 > 编程语言 > java >

luogu2916 [USACO08NOV]

来源:互联网 收集:自由互联 发布时间:2022-08-10
​​http://www.elijahqi.win/2017/07/23/luogu2916-usaco08nov/​​​ 题目描述 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


​​http://www.elijahqi.win/2017/07/23/luogu2916-usaco08nov/​​​
题目描述

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

输入输出格式

输入格式:

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.

本想五分钟搞定,发现题目没读清楚又花了25min我。。、

大概意思是:要花费最小时间去安慰牛,每个地方都要安慰牛,只要经过就要安慰牛,路是往返的所以权值存双向边,最后应该多加一次需要安慰的牛的值的最小的那个,因为从那里起点,然后跑最小生成树

#include<cstdio>
#include<algorithm>
#define N 10000
#define P 100000
inline int read(){
int x=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct node{
int x,y,z;
}data[P];
int c[N],h[N],num,fa[N],n,p;
inline void insert1(int x,int y,int z){
data[++num].x=x;data[num].y=y;data[num].z=z;
}
inline bool cmp(node a,node b){
return a.z<b.z;
}
inline int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline int min(int x,int y){
return x<y?x:y;
}
int main(){
// freopen("2916.in","r",stdin);
// freopen("2916.out","w",stdout);
n=read();p=read();int ans=1<<30;
for (int i=1;i<=n;++i) c[i]=read(),ans=min(ans,c[i]);
int x,y,z;
for (int i=1;i<=p;++i) x=read(),y=read(),z=read(),z<<=1,z+=c[x]+c[y],insert1(x,y,z);
std::sort(data+1,data+p+1,cmp);
for (int i=1;i<=n;++i) fa[i]=i;
int cnt=0;
//for (int i=1;i<=p;++i) printf("%d,data[i].z);printf("\n");
for (int i=1;i<=p;++i){
x=find(data[i].x);y=find(data[i].y);z=data[i].z;
if (x!=y){
cnt++;ans+=z;fa[x]=y;
}
if (cnt==n-1) break;
}
printf("%d",ans);
return 0;
}


上一篇:hdu1811 Rank of Tetris(拓扑排序+并查集)
下一篇:没有了
网友评论