csu 1548(三分)

1548: Design road

Time Limit: 2 Sec  Memory Limit: 256 MB
Submit: 383  Solved: 200
[Submit][Status][Web Board]

Description

You
need to design road from (0, 0) to (x, y) in plane with the lowest
cost. Unfortunately, there are N Rivers between (0, 0) and (x, y).It
costs c1 Yuan RMB per meter to build road, and it costs c2 Yuan RMB per
meter to build a bridge. All rivers are parallel to the Y axis with
infinite length.

Input

There are several test cases.
Each test case contains 5 positive integers N,x,y,c1,c2 in the first line.(N ≤ 1000,1 ≤ x,y≤ 100,000,1 ≤ c1,c2 ≤ 1000).
The following N lines, each line contains 2 positive integer xi, wi ( 1 ≤
i ≤ N ,1 ≤ xi ≤x, xi-1+wi-1 < xi , xN+wN ≤ x),indicate the i-th
river(left bank) locate xi with wi width.
The input will finish with the end of file.

Output

For each the case, your program will output the least cost P on separate line, the P will be to two decimal places .

Sample Input

1 300 400 100 100
100 50
1 150 90 250 520
30 120

Sample Output

50000.00
80100.00

题意:有一个人要从(0,0)->(x,y),中间有n条河,每条河都有一个宽度和一个起始位置,并且上一条河与下一条河无交点,修一公里路花费 c1 ,一公里河花费 c2,问人从起点到终点的最小费用?题解:我们将所有的和平移到最右边,那么这里就只要枚举河岸就OK了,单峰极值,三丰求解。(0,0)->(X,t)为河,(X,t)->(x,y)为路.花费为 sqrt(X*X+t*t)*c2+sqrt((x-X)*(x-X)+(y-t)*(y-t))*c1
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n;
double x,y,c1,c2,sum,X;
const double eps = 1e-8;
double Calc(double t)
{
    return sqrt(X*X+t*t)*c2+sqrt((x-X)*(x-X)+(y-t)*(y-t))*c1;
}
double solve(double MIN,double MAX)
{
    double Left, Right;
    double mid, midmid;
    double mid_value, midmid_value;
    Left = MIN;
    Right = MAX;
    while (Left +eps < Right)
    {
        mid = (Left + Right) / 2;
        midmid = (mid + Right) / 2;
        mid_value = Calc(mid);
        midmid_value = Calc(midmid);
        if (mid_value <= midmid_value) Right = midmid;
        else Left = mid;
    }
    return Left;
}
int main()
{
    while(scanf("%d%lf%lf%lf%lf",&n,&x,&y,&c1,&c2)!=EOF)
    {
        X = 0;
        for(int i=1; i<=n; i++)
        {
            double a,b;
            scanf("%lf%lf",&a,&b);
            X+=b;
        }
        double k = solve(0,y);
        printf("%.2lf\n",Calc(k));
    }
}
时间: 2024-11-12 12:11:01

csu 1548(三分)的相关文章

三分 --- CSU 1548: Design road

Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到达(x,y).但是在0~x之间有n条平行于y轴的河,每条河位于xi处,无限长,wi宽,并分别给出了建立路和桥每公里的单价 求:到达目标的最小费用. analyse: 比赛的时候一直没想到思路,第二个样列怎么算都算不对,赛后才知道是三分. 首先把所有的桥移到最右端,然后三分枚举路和河的交点. Time

[ACM] CSU 1548 Design road (三分)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 第一次接触三分,题意和代码参考的网上的. 题意:修路:从(0,0)~(x,y),n个数表示有第二行开始有n行表示有n条河,tx是河的起始位置,ty是河的宽度,有水的地方要修桥,而x,y表示修路的端点,C1表示修路每米的花费,C2表示修桥每米的花费,问你最后花费的最少金额! 思路:先把有河的地方都和到一起,然后它的宽度就知道了,那么就把高度三分,找花费最小的点 这里三分求的是最小值. 三分法

csu 1548: Design road(三分)

1548: Design road Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 243  Solved: 115 [Submit][Status][Web Board] Description You need to design road from (0, 0) to (x, y) in plane with the lowest cost. Unfortunately, there are N Rivers between (0, 0) a

csu 1548: Design road (三分)

题意:需要从(0,0) 点 到(x,y) 修一段路 其中有n条和y轴平行的河 修路的单位成本c1 修桥的单位成本c2 问最小总成本为多少 思路:把所有河合并 再三分桥的长度 求出最小成本 #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<algorithm> #include<queu

CSU 1548 Design road(三分查找)

题目链接:https://cn.vjudge.net/problem/142542/origin Description You need to design road from (0, 0) to (x, y) in plane with the lowest cost. Unfortunately, there are N Rivers between (0, 0) and (x, y).It costs c1 Yuan RMB per meter to build road, and it

1548: Design road (思维题 做法:三分找极值)

1548: Design road Submit Page    Summary    Time Limit: 2 Sec     Memory Limit: 256 Mb     Submitted: 450     Solved: 237 Description You need to design road from (0, 0) to (x, y) in plane with the lowest cost. Unfortunately, there are N Rivers betwe

CSU 1728 线形逐步共聚合反应(三分)

题意:给你n个东西,然后每个东西里面a物质各加了ai,然后让你往每个东西里面加等量的b物质,要求这里面 |sum(a)-sum(b)|最大的一个区间的这个值最小,问你b物质应该加多少? 题解:假设b物质加x,让s(i,j)=sigma(a[k]-x)要求的是最小化 max|s(i,j)| (i,j都属于1到n) 光这么看的话基本是没有什么想法的,我就是太蠢,在这里愣了好久不知道怎么做,其实应该进行化简 max|s(i,j)| = max( max( s(i,j) , -s(i,j) ) ) =

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>

CSU 1111: 三家人【有趣的思维题】

1111: 三家人 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 2241  Solved: 874 [Submit][Status][Web Board] Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请问这笔钱如何分给A.B 二位太太较为恰当?A 应得多少元?90/(5+4)*5=$50