F是签到题

来源:互联网 发布:手机自动关机软件 编辑:程序博客网 时间:2024/05/02 02:54
F是签到题
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice OpenJ_Bailian 4118 uDebug

Description

Input

Output

Sample Input

Sample Output

Hint

Description

Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.

Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.

Output

* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.

Sample Input

5 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 5 17

Sample Output

42

Hint

OUTPUT DETAILS:

The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.

Description

北大信息学院的同学小明毕业之后打算创业开餐馆.现在共有n 个地点可供选择。小明打算从中选择合适的位置开设一些餐馆。这 n 个地点排列在同一条直线上。我们用一个整数序列m1, m2, ... mn 来表示他们的相对位置。由于地段关系,开餐馆的利润会有所不同。我们用pi 表示在mi 处开餐馆的利润。为了避免自己的餐馆的内部竞争,餐馆之间的距离必须大于k。请你帮助小明选择一个总利润最大的方案。


Input

标准的输入包含若干组测试数据。输入第一行是整数T (1 <= T <= 1000) ,表明有T组测试数据。紧接着有T组连续的测试。每组测试数据有3行,
第1行:地点总数 n (n < 100), 距离限制 k (k > 0 && k < 1000).
第2行:n 个地点的位置m1 , m2, ... mn ( 1000000 > mi > 0 且为整数,升序排列)
第3行:n 个地点的餐馆利润p1 , p2, ... pn ( 1000 > pi > 0 且为整数)

Output

对于每组测试数据可能的最大利润

Sample Input

2
3 11
1 2 15
10 2 30
3 16
1 2 15
10 2 30

Sample Output

40
30
#include<stdio.h>#include<iostream>using namespace std;long long int a[1000],b[2000];long long int d[1000];//用d来记录每一个位置的最大值,更新int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)            scanf("%lld",&a[i]);        for(int i=0;i<n;i++)        {            scanf("%lld",&b[i]);            d[i]=b[i];        }        for(int i=1;i<n;i++)        {long long int p=0;            for(int j=0;j<i;j++)            {                if(a[i]-a[j]>m)                {                    p=max(b[i]+d[j],p);                    d[i]=p;                }            }        }        long long int maxn=0;        for(int i=0;i<n;i++)        {            maxn=max(d[i],maxn);        }        printf("%lld\n",maxn);    }}

0 0
原创粉丝点击