PKU 1949 Chores
[åé¡æ]
1949 -- Chores
Nåã®ä»äºããããããããã«ãããæéã¨ããã®ä»äºããå¿
ãå
ã«çµããããªããã°ãªããªãä»äºkåã®æ
å ±ãä¸ããããã
ä»äºã¯ä¸¦åã§è¡ããã¨ãã§ãããNåã®ä»äºãçµããããã®ã«ãããæéãæå°åããã
[解æ³]
ãããã¸ã«ã«ã½ã¼ã + dp
ä»»æã®ä»äºxã«å¯¾ãã¦ãããããå
ã«çµããããªããã°ãªããªãä»äº{Pi|(0 <= i <= k)}ãããã¨ãããä»äºãç¯ç¹ã¨ããåPiãããä»äºxã«æå辺ãå¼ããä»äºxã«éè·¯ãããã¨ä»®å®ããã¨é¡æããçç¾ãçããã®ã§ãã©ã®ä»äºã«ãéè·¯ããªããã¤ã¾ãDAGã«ãªãã
ãã¨ã¯ããªãã¹ãä»äºããããã¸ã«ã«ã½ã¼ããã¦dpããã°ãO(|E|)ã§è§£ããã
(ã¡ãªã¿ã«ãééã£ãæç´è§£ã§ããªããACããããã¾ã...ã§)
const int MAX_N = 20000; int N; vector<int> E[MAX_N]; vector<int> RevE[MAX_N]; bool used[MAX_N]; vector<int>order; void dfs(int v){ used[v] = true; rep(i,E[v].size()) if(!used[E[v][i]]) dfs(E[v][i]); order.pb(v); } void topological_sort(){ rep(i,N) if(!used[i]) dfs(i); } int dp[MAX_N]; int T[MAX_N]; int main(){ while(scanf("%d",&N) != EOF){ rep(i,N){ int cost,c; scanf("%d %d",&cost,&c); T[i] = cost; rep(j,c){ int must; scanf("%d",&must); --must; E[must].push_back(i); RevE[i].push_back(must); } } topological_sort(); dp[*(order.rbegin())] = T[*(order.rbegin())]; for(int i = order.size() - 2; i >= 0; i--){ int s = order[i]; rep(i,RevE[s].size()) dp[s] = max(dp[s],dp[RevE[s][i]]); dp[s] += T[s]; } int ans = *(max_element(dp,dp + N)); printf("%d\n",ans); } }