2025年C++數(shù)據(jù)結(jié)構(gòu)機試題及答案_第1頁
2025年C++數(shù)據(jù)結(jié)構(gòu)機試題及答案_第2頁
2025年C++數(shù)據(jù)結(jié)構(gòu)機試題及答案_第3頁
2025年C++數(shù)據(jù)結(jié)構(gòu)機試題及答案_第4頁
2025年C++數(shù)據(jù)結(jié)構(gòu)機試題及答案_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

2025年C++數(shù)據(jù)結(jié)構(gòu)機試題及答案【問題描述】任務(wù)調(diào)度系統(tǒng)需要處理動態(tài)添加的任務(wù)及其依賴關(guān)系,并支持查詢從指定起始任務(wù)開始的最短完成總時間(即關(guān)鍵路徑長度)。每個任務(wù)有唯一的ID和執(zhí)行時間,任務(wù)執(zhí)行前需完成所有前置任務(wù)。系統(tǒng)需支持以下兩種操作:1.添加任務(wù):格式為"ADD<任務(wù)ID><執(zhí)行時間><前置任務(wù)數(shù)><前置任務(wù)ID列表>"(若前置任務(wù)數(shù)為0,則列表為空)2.查詢?nèi)蝿?wù):格式為"QUERY<起始任務(wù)ID>",輸出從該起始任務(wù)開始,完成所有可執(zhí)行任務(wù)的最短總時間(即所有可能路徑中的最長路徑長度)輸入保證:-所有任務(wù)ID為字符串,由字母和數(shù)字組成,長度不超過10-執(zhí)行時間為正整數(shù)-添加任務(wù)時,所有前置任務(wù)已存在-任務(wù)依賴關(guān)系構(gòu)成有向無環(huán)圖(DAG)【輸入樣例】5ADDA30ADDB21AADDC41AADDD52BCQUERYA【輸出樣例】12【答案】```cppinclude<iostream>include<unordered_map>include<vector>include<queue>include<string>include<algorithm>include<climits>usingnamespacestd;structTask{inttime;vector<string>preds;vector<string>succs;};unordered_map<string,Task>tasks;vector<string>get_reachable(conststring&start){vector<string>reachable;if(tasks.find(start)==tasks.end())returnreachable;unordered_map<string,bool>visited;queue<string>q;q.push(start);visited[start]=true;reachable.push_back(start);while(!q.empty()){stringu=q.front();q.pop();for(conststring&v:tasks[u].succs){if(!visited[v]){visited[v]=true;reachable.push_back(v);q.push(v);}}}returnreachable;}vector<string>topological_sort(constvector<string>&nodes){unordered_map<string,int>in_degree;unordered_map<string,vector<string>>adj;for(conststring&u:nodes){adj[u]=tasks[u].succs;in_degree[u]=0;for(conststring&p:tasks[u].preds){if(find(nodes.begin(),nodes.end(),p)!=nodes.end()){in_degree[u]++;}}}queue<string>q;for(conststring&u:nodes){if(in_degree[u]==0){q.push(u);}}vector<string>topo;while(!q.empty()){stringu=q.front();q.pop();topo.push_back(u);for(conststring&v:adj[u]){if(find(nodes.begin(),nodes.end(),v)!=nodes.end()){in_degree[v]--;if(in_degree[v]==0){q.push(v);}}}}returntopo;}intquery(conststring&start){vector<string>reachable=get_reachable(start);if(reachable.empty())return0;vector<string>topo=topological_sort(reachable);if(topo.size()!=reachable.size())return0;unordered_map<string,int>dp;intmax_time=0;for(conststring&u:reachable){dp[u]=INT_MIN;}dp[start]=tasks[start].time;max_time=dp[start];for(conststring&u:topo){for(conststring&v:tasks[u].succs){if(find(reachable.begin(),reachable.end(),v)!=reachable.end()){if(dp[v]<dp[u]+tasks[v].time){dp[v]=dp[u]+tasks[v].time;if(dp[v]>max_time){max_time=dp[v];}}}}}returnmax_time;}intmain(){intn;cin>>n;stringop;while(n--){cin>>op;if(op=="ADD"){stringid;intt,k;cin>>id>>t>>k;Tasktask;task.time=t;for(inti=0;i<k;++i){stringpred;cin>>pred;task.preds.push_back(pred);tasks[pred].succs.push_back(id);}

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論