您的位置:首页 > 运维架构

Topcoder SRM144 div2 1100

2014-12-05 21:34 393 查看
题目:

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

题意:大概是一个电力公司停电了,要检修下水道里的变压器,每两个变压器有一条路径相通(双向的),然后有花费的时间,然后只有一个检修人员从第一个变压器出发,问遍历所有变压器最少需要多长时间。

题解:典型的树状DP.我们把第一个变压器当做根节点(I AM Groot)然后有N个互不相通的儿子,我们肯定是走N-1个儿子走两次,而1个儿子只走一次。我们可以枚举只走一次的儿子,其他儿子走两次进行深搜DFS就可以了。

// BEGIN CUT HERE

// END CUT HERE
#line 5 "PowerOutage.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(i=0;i<(n);++i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
struct node{
	int a,b,c,next;
}edge[1000];
int head[1000];
int w=0;
int dp[1000][5];
void add(int a,int b,int c){
	w++;
	edge[w].a=a;
	edge[w].b=b;
	edge[w].c=c;
	edge[w].next=head[a];
	head[a]=w;
}
// dp
[0]表示走到这点并且回到这点的最小时间
// dp
[1]表示这点只走一次 
void DFS(int p,int father){
	int i,j,to,s;
	int son=0;
	for (i=head[p];i!=-1;i=edge[i].next){
		to=edge[i].b;
		if (to==father) continue;
		son++;
		DFS(to,p);
	}
	if (!son){
		dp[p][0]=0;
		dp[p][1]=0;
		return;
	}
	int sum=0;
	for (i=head[p];i!=-1;i=edge[i].next){
		to=edge[i].b;
		s=edge[i].c;
		if (to==father) continue;
		sum+=dp[to][0]+2*s;
	}
	dp[p][0]=sum;
	int mm=0x7fffffff;
	for (i=head[p];i!=-1;i=edge[i].next){
		to=edge[i].b;
		if (to==father) continue;
		s=edge[i].c;
		sum=dp[to][1]+s;
		for (j=head[p];j!=-1;j=edge[j].next){
			int go=edge[j].b;
			if (go==to || go==father) continue;
			sum+=dp[go][0]+2*edge[j].c;
		}
		mm=min(sum,mm);
	}
	dp[p][1]=mm;
}
class PowerOutage
{
        public:
        int estimateTimeOut(vector <int> a, vector <int> b, vector <int> c)
        {
        	int i;
            int n=a.size();
            memset(head,-1,sizeof(head));
            memset(edge,0,sizeof(edge));
            memset(dp,0,sizeof(dp));
        	for (i=0;i<n;i++){
        		add(a[i],b[i],c[i]);
        		add(b[i],a[i],c[i]);
        	}
        	DFS(0,-1);
        	int ans=dp[0][1];
        	return ans;
        }
        
// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arr0[] = {0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {10}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 10; verify_case(0, Arg3, estimateTimeOut(Arg0, Arg1, Arg2)); }
	void test_case_1() { int Arr0[] = {0,1,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {10,10,10}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 40; verify_case(1, Arg3, estimateTimeOut(Arg0, Arg1, Arg2)); }
	void test_case_2() { int Arr0[] = {0,0,0,1,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,3,4,2,5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {10,10,100,10,5}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 165; verify_case(2, Arg3, estimateTimeOut(Arg0, Arg1, Arg2)); }
	void test_case_3() { int Arr0[] = {0,0,0,1,4,4,6,7,7,7,20}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,3,4,2,5,6,7,20,9,10,31}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {10,10,100,10,5,1,1,100,1,1,5}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 281; verify_case(3, Arg3, estimateTimeOut(Arg0, Arg1, Arg2)); }
	void test_case_4() { int Arr0[] = {0,0,0,0,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,3,4,5}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {100,200,300,400,500}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2500; verify_case(4, Arg3, estimateTimeOut(Arg0, Arg1, Arg2)); }

// END CUT HERE

};
// BEGIN CUT HERE
int main()
{
        PowerOutage ___test;
        ___test.run_test(-1);
        return 0;
}
// END CUT HERE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: