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

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[100100],fa[100100];
struct node
{
    int u,v,w;
}e[500000];

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=1;i<=n;i++)
    {
        fa[i]=i;
        cin>>c[i];
        ans=min(c[i],ans);
    }
    for(int i=1;i<=p;i++)
    {
        cin>>s>>t>>l;
        add(s,t,l*2+c[s]+c[t]);
    }
    sort(e+1,e+tot+1,cmp);
    for(int i=1;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-1)
        {
            cout<<ans;
            return 0;
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Shy-key/p/6533883.html

题目描述 农夫约翰一直在观察他的奶牛们。他注意到,如果在牛群中有太多的牛靠得太近,就会导致不健康的行为和情感问题。 约翰想知道他的牛群是否存在这个问题。他定义这个问题为:在一个固定长度的路段上,如果有两头高度大于等于 $y$ 的奶牛之间的距离小于 $x$,则牛群中就存在一个挤得太近的情况。 约翰有 $N$ 头牛 ($1 \leq N \leq 50,000$),每头牛的高度为 $h_i$ ($1 \leq h_i \leq 1,000,000$)。他想知道是否存在一对牛,使得它们之间的距离小于 $x$,且它们的高度都大于等于 $y$。 输入格式 第一行包含三个整数 $N, L, R$,分别表示牛的数量,路段长度,和问题的最大高度。 接下来 $N$ 行,每行一个整数 $h_i$,表示每头牛的高度。 输出格式 如果存在一对牛,它们之间的距离小于 $x$,且它们的高度都大于等于 $y$,则输出 $1$,否则输出 $0$。 输入样例1 4 6 4 4 4 5 7 输出样例1 1 输入样例2 5 3 3 1 5 5 5 5 输出样例2 0 提示 对于 $30\%$ 的数据,$N \leq 500$。 对于 $100\%$ 的数据,$1 \leq N \leq 50,000$,$1 \leq L \leq 1,000,000$,且 $L \leq R$。 数据范围 时间限制:1.0s,空间限制:256MB 算法1 (暴力枚举) $O(n^2)$ 首先对输入的牛的高度进行排序,之后枚举每头牛,再枚举它后面的每头牛,如果两头牛的高度均大于等于 $y$,且它们之间的距离小于 $x$,则输出 $1$。如果最后仍然没有满足条件的牛,则输出 $0$。 时间复杂度 暴力枚举,时间复杂度为 $O(n^2)$,无法通过此题。 算法2 (滑动窗口) $O(n \log n)$ 为了方便后续操作,我们将所有的牛按照它们的高度从小到大排序。之后,我们维护一个长度为 $L$ 的滑动窗口,它的右端点与左端点之间的距离小于 $x$。我们从左到右扫描每头牛,将它加入滑动窗口的左端点,同时将滑动窗口右移,直到滑动窗口的右端点与左端点之间的距离小于 $x$。 在处理完一头牛之后,我们需要判断滑动窗口中是否存在一对牛,它们的高度均大于等于 $y$,且它们之间的距离小于 $x$。我们可以用双指针来实现这个操作。我们从滑动窗口的左端点开始,向右移动一个指针 $i$,同时向右移动一个指针 $j$,直到 $h_j - h_i \leq x$。在这个过程中,我们需要判断 $h_i$ 和 $h_j$ 是否均大于等于 $y$。如果存在一对牛满足条件,则输出 $1$。如果最后仍然没有满足条件的牛,则输出 $0$。 时间复杂度 因为需要对所有的牛进行排序,所以时间复杂度为 $O(n \log n)$。 C++ 代码 算法3 (暴力优化) $O(n \log n)$ 首先对输入的牛的高度进行排序,之后枚举每头牛。如果当前牛的高度小于 $y$,则跳过这头牛。否则,我们从它的左边和右边各扩展出一个长度为 $x$ 的区间。如果这两个区间内的牛的数量均大于等于 $2$,且这两个区间中任意两头牛的高度均大于等于 $y$,则输出 $1$。 时间复杂度 因为需要对所有的牛进行排序,所以时间复杂度为 $O(n \log n)$。 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值