小朋友学TopCoder(4):SRM144 DIV2 1100-point

来源:互联网 发布:api原油库存数据下载 编辑:程序博客网 时间:2024/04/30 04:37

Problem Statement

You work for an electric company, and the power goes out in a rather large apartment complex with a lot of irate tenants. You isolate the problem to a network of sewers underneath the complex with a step-up transformer at every junction in the maze of ducts. Before the power can be restored, every transformer must be checked for proper operation and fixed if necessary. To make things worse, the sewer ducts are arranged as a tree with the root of the tree at the entrance to the network of sewers. This means that in order to get from one transformer to the next, there will be a lot of backtracking through the long and claustrophobic ducts because there are no shortcuts between junctions. Furthermore, it’s a Sunday; you only have one available technician on duty to search the sewer network for the bad transformers. Your supervisor wants to know how quickly you can get the power back on; he’s so impatient that he wants the power back on the moment the technician okays the last transformer, without even waiting for the technician to exit the sewers first.

You will be given three vector ’s: fromJunction, toJunction, and ductLength that represents each sewer duct. Duct i starts at junction (fromJunction[i]) and leads to junction (toJunction[i]). ductlength[i] represents the amount of minutes it takes for the technician to traverse the duct connecting fromJunction[i] and toJunction[i]. Consider the amount of time it takes for your technician to check/repair the transformer to be instantaneous. Your technician will start at junction 0 which is the root of the sewer system. Your goal is to calculate the minimum number of minutes it will take for your technician to check all of the transformers. You will return an int that represents this minimum number of minutes.

Definition

Class: PowerOutage
Method: estimateTimeOut
Parameters: vector , vector , vector
Returns: int
Method signature: int estimateTimeOut(vector fromJunction, vector toJunction, vector ductLength)
(be sure your method is public)

Limits

Time limit (s):2.000
Memory limit (MB):64

Constraints

  • fromJunction will contain between 1 and 50 elements, inclusive.
  • toJunction will contain between 1 and 50 elements, inclusive.
  • ductLength will contain between 1 and 50 elements, inclusive.
  • toJunction, fromJunction, and ductLength must all contain the same number of elements.
  • Every element of fromJunction will be between 0 and 49 inclusive.
  • Every element of toJunction will be between 1 and 49 inclusive.
  • fromJunction[i] will be less than toJunction[i] for all valid values of i.
  • Every (fromJunction[i],toJunction[i]) pair will be unique for all valid values of i.
  • Every element of ductlength will be between 1 and 2000000 inclusive.
  • The graph represented by the set of edges (fromJunction[i],toJunction[i]) will never contain a loop, and all junctions can be reached from junction 0.

Examples

0){0}{1}{10}Returns: 10The simplest sewer system possible. Your technician would first check transformer 0, travel to junction 1 and check transformer 1, completing his check. This will take 10 minutes.1){0,1,0}{1,2,3}{10,10,10}Returns: 40Starting at junction 0, if the technician travels to junction 3 first, then backtracks to 0 and travels to junction 1 and then junction 2, all four transformers can be checked in 40 minutes, which is the minimum.2){0,0,0,1,4}{1,3,4,2,5}{10,10,100,10,5}Returns: 165Traveling in the order 0-1-2-1-0-3-0-4-5 results in a time of 165 minutes which is the minimum.3){0,0,0,1,4,4,6,7,7,7,20}{1,3,4,2,5,6,7,20,9,10,31}{10,10,100,10,5,1,1,100,1,1,5}Returns: 281Visiting junctions in the order 0-3-0-1-2-1-0-4-5-4-6-7-9-7-10-7-8-11 is optimal, which takes (10+10+10+10+10+10+100+5+5+1+1+1+1+1+1+100+5) or 281 minutes.4){0,0,0,0,0}{1,2,3,4,5}{100,200,300,400,500}Returns: 2500

分析

题目是要求遍历一个图(或树)中的所有结点,最后不需要回到根结点
(1)以Example 2为例

example_2.png

遍历方法为分支的全排列数:A(2, 2) = 2! = 2
第一种:0 -> 1 -> 2 -> 1 -> 0 -> 3,用时10 + 10 + 10 + 10 + 10 = 50分钟
第二种:0 -> 3 -> 0 -> 1 ->2,用时10 + 10 + 10 + 10 = 40分钟

(2)以Example 3为例

example_3.png

遍历方法为分支的全排列数:A(3, 3) = 3! = 6
第一种:0 -> 1 -> 2 -> 1 -> 0 -> 3 -> 0 -> 4 -> 5
用时10 + 10 + 10 + 10 + 10 + 10 + 100 + 5 = 165分钟
第二种:0 -> 1 -> 2 -> 1 -> 0 -> 4 -> 5 -> 4 -> 0 -> 3
用时10 + 10 + 10 + 10 + 100 + 5 + 5 + 100 + 10 = 260分钟
第三种:0 -> 3 -> 0 -> 1 -> 2 -> 1 -> 0 -> 4 -> 5
用时10 + 10 + 10 + 10 + 10 + 10 + 100 + 5 = 165分钟
第四种:0 -> 3 -> 0 -> 4 -> 5 -> 4 -> 0 -> 1 -> 2
用时10 + 10 + 10 + 100 + 5 + 5 + 100 + 10 + 10 = 260分钟
第五种:0 -> 4 -> 5 -> 4 -> 0 -> 1 -> 2 -> 1 -> 0 -> 3
用时100 + 5 + 5 + 100 + 10 + 10 + 10 + 10 + 10 = 260分钟
第六种:0 -> 4 -> 5 -> 4 -> 0 -> 3 -> 0 -> 1 -> 2
用时100 + 5 + 5 + 100 + 10 + 10 + 10 + 10 = 250分钟

(3)总结
从题意和上面的两个例子可以看出,本题实际上是要求:用时最长的分支遍历一遍,其他所有的分支都要遍历两遍。
因此,问题可以转化为:所有分支遍历两遍所需的时间 - 最终分支遍历一遍所需的时间

代码

#include <iostream>#include <vector>using namespace std;class PowerOutage {private:    int getmax(vector<int> &from, vector<int> &to, vector<int> &length, int start)    {        int len = from.size();        vector<int> tmp;        int max = 0;        // 计算有几个分支         for(int i = 0; i < len; i++)        {            if(from[i] == start)            {                tmp.push_back(i);            }        }        // 递归结束的条件         if(tmp.empty())        {            return 0;        }        // 计算用时最长的分支所用的时间         for(int i = 0; i < tmp.size(); i++)        {            int node = tmp[i];            int t = length[node] + getmax(from, to, length, to[node]);            if(max < t)              {                max = t;            }               }        return max;    }public:    int estimateTimeOut(vector<int> fromJunction, vector<int> toJunction, vector<int> ductLength)    {        int sum = 0;        int max = 0;        int len = ductLength.size();        for(int i = 0; i < len; i++)        {            sum += ductLength[i];        }        sum *= 2;        max = getmax(fromJunction, toJunction, ductLength, 0);        return sum - max;    }};int main(){    vector<int> from;    from.push_back(0);    from.push_back(1);    from.push_back(0);    vector<int> to;    to.push_back(1);    to.push_back(2);    to.push_back(3);    vector<int> len;    len.push_back(10);    len.push_back(10);    len.push_back(10);    PowerOutage p;    cout << p.estimateTimeOut(from, to, len);    return 0;}



更多内容请关注微信公众号
wechat.jpg

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 鼻窦炎鼻子痒怎么办 鼻窦炎术后头痛怎么办 鼻窦炎术后头疼怎么办 六岁儿童鼻炎怎么办 宝宝鼻窦炎怎么办 鼻炎吐黄痰怎么办 鼻炎引起发烧怎么办 4岁鼻窦炎怎么办 儿童鼻炎咳嗽怎么办 怀孕腿肿怎么办 美术培训班资质怎么办 无锡准生证怎么办 ppt软件打不开怎么办 ppt修复不了怎么办 ppt文件损坏怎么办 word内容不见了怎么办 ppt打不开了怎么办 手机pptx打不开怎么办 演示文稿打不开怎么办 画脏了怎么办? 小孩头上长疮怎么办 小孩头上长油疮怎么办 小孩头上长脓疮怎么办 宝宝头上长疮怎么办 宝宝头上生疮怎么办 婴儿头上长疮怎么办 宝宝头上长脓包怎么办 婴儿头上小脓包怎么办 孩子不爱画画怎么办 孩子发烧怎么办晚上 小孩子出水痘怎么办 宝宝幼儿园皮怎么办 骨头被针扎透了怎么办 手被针扎穿了怎么办 手被机针扎穿了怎么办 宝宝38.1度怎么办 没有调色盘怎么办 直线画不直怎么办 钢笔笔盖丢了怎么办 钢笔笔盖打不开怎么办 钢笔笔盖拧不开怎么办