Topcoder SRM 144 Div2 1100 (树形dp)
2014-09-08 23:27
483 查看
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, andductLength 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 connectingfromJunction[i] andtoJunction[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 | ||||||||||||
| ||||||||||||
Limits | ||||||||||||
| ||||||||||||
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, andductLength 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) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
//Hello. I'm Peter. #include<cstdio> #include<iostream> #include<sstream> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<functional> #include<cctype> #include<ctime> #include<stack> #include<queue> #include<vector> #include<set> #include<map> using namespace std; typedef long long ll; typedef long double ld; #define peter cout<<"i am peter"<<endl #define input freopen("data.txt","r",stdin) #define INT (0x3f3f3f3f)*2 #define LL (0x3f3f3f3f3f3f3f3f)*2 #define gsize(a) (int)a.size() #define len(a) (int)strlen(a) #define slen(s) (int)s.length() #define pb(a) push_back(a) #define clr(a) memset(a,0,sizeof(a)) #define clr_minus1(a) memset(a,-1,sizeof(a)) #define clr_INT(a) memset(a,INT,sizeof(a)) #define clr_true(a) memset(a,true,sizeof(a)) #define clr_false(a) memset(a,false,sizeof(a)) #define clr_queue(q) while(!q.empty()) q.pop() #define clr_stack(s) while(!s.empty()) s.pop() #define rep(i, a, b) for (int i = a; i < b; i++) #define dep(i, a, b) for (int i = a; i > b; i--) #define repin(i, a, b) for (int i = a; i <= b; i++) #define depin(i, a, b) for (int i = a; i >= b; i--) #define pi 3.1415926535898 #define eps 1e-6 #define MOD 1000000007 #define MAXN #define N 110 #define M 1100 struct Edge { int to,next,from; int val; }edge[2*M]; int head[M],w,from; void add_edge(int from,int to,int val) { w++; edge[w].from=from; edge[w].to=to; edge[w].val=val; edge[w].next=head[from]; head[from]=w; } int dp [2]; void dfs(int now,int father) { int i,to,val,son=0; for(i=head[now];i!=-1;i=edge[i].next) { to=edge[i].to; if(to==father) continue; son++; dfs(to,now); } if(!son) { dp[now][0]=0; dp[now][1]=0; return; } int sum; //for dp[now][0] sum=0; for(i=head[now];i!=-1;i=edge[i].next) { to=edge[i].to; if(to==father) continue; val=edge[i].val; sum+=dp[to][0]+2*val; } dp[now][0]=sum; //for dp[now][1] int j,jto,jval,mini=INT; for(i=head[now];i!=-1;i=edge[i].next) { to=edge[i].to; if(to==father) continue; val=edge[i].val; sum=dp[to][1]+val; for(j=head[now];j!=-1;j=edge[j].next) { jto=edge[j].to; if(jto==to || jto==father) continue; jval=edge[j].val; sum+=dp[jto][0]+2*jval; } mini=min(mini,sum); } dp[now][1]=mini; } class PowerOutage { public: int estimateTimeOut(vector <int> fromJunction, vector <int> toJunction, vector <int> ductLength) { int res=0,to,val; int len=gsize(fromJunction); w=0; clr_minus1(head); clr(edge); rep(i,0,len) { from=fromJunction[i]; to=toJunction[i]; val=ductLength[i]; add_edge(from,to,val); add_edge(to,from,val); } clr(dp); dfs(0,-1); res=dp[0][1]; return res; } };
相关文章推荐
- 小朋友学TopCoder(4):SRM144 DIV2 1100-point
- 小朋友学TopCoder(6):SRM144 DIV1 1100-point
- topcoder SRM 666 DIV2 CollectingTokens 树形dp
- Topcoder SRM 144 Div1 550(数学和dp问题求方案数,很有意思)
- Topcoder SRM144 div2 1100
- TopCoder SRM 666 Div2 Problem 999 - WalkOverATreeDiv2 (树形DP)
- Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划
- Topcoder SRM 663 Div2 Hard: CheeseRolling(状压DP)
- TopCoder 300 points 1-SRM 144 DIV 1 90/300 30%
- TopCoder SRM 144 DIV2(200-point)
- TopCoder SRM 653 Div2 Problem 500 - RockPaperScissorsMagicEasy (DP)
- 小朋友学TopCoder(2):第一个TopCoder程序--SRM144 DIV2 200-point
- TopCoder SRM 144 DIV2(550-point)
- TopCoder_SRM_144_DIV2_250_Time
- topcoder SRM 144 DIV 1 by First_Blood
- TopCoder 200 points 2-SRM 144 DIV 2 152.06/200 76.03%
- 小朋友学TopCoder(3):SRM144 DIV2 550-point
- TopCoder SRM 667 Div1 Problem 250 - OrderOfOperations (状压dp)
- Topcoder SRM 651 div1 250 题解 (概率dp)
- TopCoder SRM 672 Div2 Problem 1000 - Tdetectived2 (状压dp)