Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划

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 <int>'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 <int>, vector <int>, vector <int>
Returns:int
Method signature:int estimateTimeOut(vector <int> fromJunction, vector <int> toJunction, vector <int> 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: 10
The 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: 40
Starting 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: 165
Traveling 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: 281
Visiting 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


代码如下:

#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

class PowerOutage{
public:
    int estimateTimeOut(vector<int> fromJunc,vector<int> toJunc,vector<int> juncLen)
    {
        int len = fromJunc.size();
        int min;
        int G[55][55];
        int res[55];
        int pre[55];
        int cost[55];
        int row=0;
        int maxr=0,flag;
        memset(G,0,sizeof(G));
        memset(res,0,sizeof(res));
        memset(pre,0,sizeof(pre));
        memset(cost,0,sizeof(cost));
        for(int i = 0;i < len;++i){
            G[fromJunc[i]][toJunc[i]]=juncLen[i];
        }
        for(int j=0;j<50;++j){//动态规划得出res
            if(G[row][j]!=0){
               pre[j]=row;
               res[j]=res[pre[j]]+G[row][j];
               row=j;
            }
            if(j==49){//回溯
                if(row == 0){
                    break;
                }
                j=row;
                row=pre[j];
            }
        }
        for(int i=0;i<50;++i){//res中求最大值
            if(maxr < res[i]){
                maxr = res[i];
                flag = i;
            }
        }
        min=maxr;//求整条最大分支路径出来,min=最大距离,从G删除最大路径
        while(flag!=0){
            G[pre[flag]][flag]=0;
            flag=pre[flag];
        }
        for(int i=0;i<50;++i){//剩余的小分支长度全部翻倍加到min
            for(int j=0;j<50;++j){
                if(G[i][j]!=0){
                    min+=2*G[i][j];
                }
            }
        }
        return min;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物像数据,这些数据包括健康植物的像以及受不同病虫害影响的植物像。 2. **像预处理**:对收集到的像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值