zoukankan      html  css  js  c++  java
  • codeforces_302D

    D. Yaroslav and Time
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.

    A player spends d·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.

    Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).

    Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.

    Input

    The first line contains integers n and d (3 ≤ n ≤ 100, 103 ≤ d ≤ 105) — the number of stations and the constant from the statement.

    The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 ≤ ai ≤ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xiyi (-100 ≤ xi, yi ≤ 100).

    It is guaranteed that no two stations are located at the same point.

    Output

    In a single line print an integer — the answer to the problem.

    Examples
    input
    3 1000
    1000
    0 0
    0 1
    0 3
    output
    2000
    input
    3 1000
    1000
    1 0
    1 1
    1 2
    output
    1000


    一开始当作搜索来做,发现有问题,读题要全面分析,最后看题解,发现是一个最短路问题。
    思路:知道是最短路后,就是一个简单题,用floyd过掉。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<stdlib.h>
    #include<cmath>
    using namespace std;
    #define INF 99999999
    int n,d;
    int map[105][105];
    
    void init()
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                if(i==j)
                    map[i][j]=0;
                else
                    map[i][j]=INF;
            }
    }
    
    int add[105];
    int loca[105][2];
    int main()
    {
        scanf("%d%d",&n,&d);
        init();
        for(int i=2; i<n; i++)
            scanf("%d",&add[i]);
        for(int i=1; i<=n; i++)
            scanf("%d%d",&loca[i][0],&loca[i][1]);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(i!=j)
                    map[i][j]=(abs(loca[i][0]-loca[j][0])+abs(loca[i][1]-loca[j][1]))*d-add[j];
        for(int k=1; k<=n; k++)
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
        printf("%d
    ",map[1][n]);
        return 0;
    }


  • 相关阅读:
    Vmware15的安装以及Ubunt的在虚拟机上的安装
    MYSQL数据库查询索引
    SpringBoot和mybatis整合报错:Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 152; columnNumber: 10; 元素类型为 "mapper"
    idea中导包出现import org.apach.*,提交代码老出现冲突,不想使用.*的设置
    idea打断点后发现被标记的断点处那一行整行被标记了其他颜色,前面没有断点标识的红点
    Intellj IDEA 光标显示insert状态解决办法
    AngularJS 单元测试 Karma jasmine
    npm 及安装
    AngularJS 承诺 Promise
    AngularJS 路由及SPA理解
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5405169.html
Copyright © 2011-2022 走看看