HDU 4109 Instrction Arrangement

題目鏈接:https://vjudge.net/problem/HDU-4109

題目大意

有 N 個指令,標號從 0 ~ N - 1,和 M 個指令間的先后關系,每個關系都有一個權值 w,表示后一個指令在前一個指令開始時間之后 w 納秒才開始執行。現在要并發執行這些指令,問最少要多長時間才能執行完?

分析

關鍵路徑模板題,有多個源點和匯點,答案為所有匯點的最早開始時間的最大值。

代碼如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
 21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 刪去 x 中所有 c 
 22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
 23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
 24  
 25 #define ms0(a) memset(a,0,sizeof(a))
 26 #define msI(a) memset(a,0x7f,sizeof(a))
 27 #define msM(a) memset(a,-1,sizeof(a))
 28 
 29 #define MP make_pair
 30 #define PB push_back
 31 #define ft first
 32 #define sd second
 33  
 34 template<typename T1, typename T2>
 35 istream &operator>>(istream &in, pair<T1, T2> &p) {
 36     in >> p.first >> p.second;
 37     return in;
 38 }
 39  
 40 template<typename T>
 41 istream &operator>>(istream &in, vector<T> &v) {
 42     for (auto &x: v)
 43         in >> x;
 44     return in;
 45 }
 46 
 47 template<typename T>
 48 ostream &operator<<(ostream &out, vector<T> &v) {
 49     Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
 50     return out;
 51 }
 52  
 53 template<typename T1, typename T2>
 54 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 55     out << "[" << p.first << ", " << p.second << "]" << "\n";
 56     return out;
 57 }
 58 
 59 inline int gc(){
 60     static const int BUF = 1e7;
 61     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 62     
 63     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 64     return *bg++;
 65 } 
 66 
 67 inline int ri(){
 68     int x = 0, f = 1, c = gc();
 69     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 70     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 71     return x*f;
 72 }
 73 
 74 template<class T>
 75 inline string toString(T x) {
 76     ostringstream sout;
 77     sout << x;
 78     return sout.str();
 79 }
 80 
 81 inline int toInt(string s) {
 82     int v;
 83     istringstream sin(s);
 84     sin >> v;
 85     return v;
 86 }
 87 
 88 //min <= aim <= max
 89 template<typename T>
 90 inline bool BETWEEN(const T aim, const T min, const T max) {
 91     return min <= aim && aim <= max;
 92 }
 93  
 94 typedef long long LL;
 95 typedef unsigned long long uLL;
 96 typedef vector< int > VI;
 97 typedef vector< bool > VB;
 98 typedef vector< char > VC;
 99 typedef vector< double > VD;
100 typedef vector< string > VS;
101 typedef vector< LL > VL;
102 typedef vector< VI > VVI;
103 typedef vector< VB > VVB;
104 typedef vector< VS > VVS;
105 typedef vector< VL > VVL;
106 typedef vector< VVI > VVVI;
107 typedef vector< VVL > VVVL;
108 typedef pair< int, int > PII;
109 typedef pair< LL, LL > PLL;
110 typedef pair< int, string > PIS;
111 typedef pair< string, int > PSI;
112 typedef pair< string, string > PSS;
113 typedef pair< double, double > PDD;
114 typedef vector< PII > VPII;
115 typedef vector< PLL > VPLL;
116 typedef vector< VPII > VVPII;
117 typedef vector< VPLL > VVPLL;
118 typedef vector< VS > VVS;
119 typedef map< int, int > MII;
120 typedef unordered_map< int, int > uMII;
121 typedef map< LL, LL > MLL;
122 typedef map< string, int > MSI;
123 typedef map< int, string > MIS;
124 typedef set< int > SI;
125 typedef stack< int > SKI;
126 typedef queue< int > QI;
127 typedef priority_queue< int > PQIMax;
128 typedef priority_queue< int, VI, greater< int > > PQIMin;
129 const double EPS = 1e-8;
130 const LL inf = 0x7fffffff;
131 const LL infLL = 0x7fffffffffffffffLL;
132 const LL mod = 1e9 + 7;
133 const int maxN = 1e3 + 7;
134 const LL ONE = 1;
135 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
136 const LL oddBits = 0x5555555555555555;
137 
138 struct Edge{
139     int from, to, w;
140     int et, lt;
141 };
142 
143 istream& operator>> (istream& in, Edge &x) {
144     in >> x.from >> x.to >> x.w;
145     return in;
146 }
147 
148 struct Vertex{
149     int in, out, value;
150     VI next, prev;
151     int et, lt;
152     
153     void clear() {
154         value = in = out = 0;
155         next.clear();
156         prev.clear();
157         et = 0;
158         lt = inf;
159     }
160 };
161 
162 int N, M, ans;
163 Vertex V[maxN];
164 vector< Edge > E;
165 VI topo;
166 
167 void addEdge(Edge &x) {
168     V[x.from].next.PB(E.size());
169     V[x.to].prev.PB(E.size());
170     ++V[x.to].in;
171     ++V[x.from].out;
172     E.PB(x);
173 }
174 
175 void init() {
176     For(i, 1, N) V[i].clear();
177     E.clear();
178     topo.clear();
179     ans = 0;
180 }
181 
182 void TopoSort() {
183     SKI sk;
184     For(i, 1, N) if(!V[i].in) sk.push(i);
185     
186     while(!sk.empty()) {
187         int tmp = sk.top(); sk.pop();
188         
189         topo.PB(tmp);
190         Rep(i, V[tmp].next.size()) {
191             Edge &e = E[V[tmp].next[i]];
192             if(!--V[e.to].in) sk.push(e.to);
193             V[e.to].et = max(V[e.to].et, V[e.from].et + e.w); // 在拓撲排序的同時計算每個節點的最早發生時間 
194         }
195     }
196 }
197 
198 int main(){
199     //freopen("MyOutput.txt","w",stdout);
200     //freopen("input.txt","r",stdin);
201     INIT();
202     while(cin >> N >> M) {
203         init();
204         Rep(i, M) {
205             Edge t;
206             cin >> t;
207             ++t.from;
208             ++t.to;
209             addEdge(t);
210         }
211         TopoSort();
212         For(i, 1, N) if(!V[i].out) ans = max(ans, V[i].et);
213         cout << ans + 1 << endl;
214     }
215     return 0;
216 }
View Code

?

轉載于:https://www.cnblogs.com/zaq19970105/p/11319841.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247212.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247212.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247212.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

養成一個習慣需要多久?

有人說養成一個習慣需要21天&#xff0c;有人說30天……養成習慣到底需要多少天&#xff1f; _hzw 2012-11-09 18:04又快到年末了。 年初時&#xff0c;你曾許諾要養成一些好習慣&#xff0c;比如要常運動、多讀書、少微博。有人說說養成一個習慣只需要堅持21天&#xff1b;21天…

vue3 eslint The template root requires exactly one element問題

vue項目中手動引入eslint npm install eslint --save-dev eslint初始化 npx eslint --init 運行時npm run lint 簡單語法問題--fix會自動修復 但是vue3項目出現只能有一個根節點的問題 原因在于 eslintrc.js里extends中plugin:vue/essential的問題 看eslint-plugin-vue官網…

你所知道的學習方法,都是錯的!

你所知道的學習方法&#xff0c;都是錯的&#xff01; 上課的時候記筆記&#xff1f;哪門功課不行&#xff0c;就集中精力專項突擊&#xff1f;自習的時候不要晃&#xff0c;選好一個地兒安安穩穩地待那兒學習&#xff1f;你還在這樣學習嗎&#xff1f;不要被騙了&#xff1a;這…

BZOJ 1683.City skyline 城市地平線

傳送門 從左到右掃一遍&#xff0c;考慮什么時候會和之前形成同一幢房子從而不用統計 顯然是當前的高度和之前某個點高度相同&#xff0c;并且它們之間沒有更矮的建筑 考慮用一個單調棧維護一個單調上升的房子輪廓&#xff0c;然后對于掃到的每一個高度&#xff0c;看看棧里有沒…

Vue3 eslint 編譯器宏和 defineProps,defineEmits,no-undef 規則警告

.eslintrc.js修改 定義全局globals module.exports {globals: {defineProps: readonly,defineEmits: readonly,defineExpose: readonly,withDefaults: readonly},env: {browser: true,es2021: true},extends: [plugin:vue/vue3-strongly-recommended, standard],parserOptio…

Java生鮮電商平臺-SpringCloud微服務開發中的數據架構設計實戰精講

Java生鮮電商平臺-SpringCloud微服務開發中的數據架構設計實戰精講 Java生鮮電商平臺: 微服務是當前非常流行的技術框架&#xff0c;通過服務的小型化、原子化以及分布式架構的彈性伸縮和高可用性&#xff0c;可以實現業務之間的松耦合、業務的靈活調整組合以及系統的高可用性…

JS兩個對象數組過濾掉相同的對象

let arr1 [{uid:1,name:張三},{uid:2,name:李四}]; let arr2 [{id:1,name:張三},{id:2,name:王五,age:23},{id:3,name:羅翔}]; 去掉arr2中包含的arr1相同name的對象: //ES6的方法&#xff0c;得到新數組 const newArr arr2.filter((item) > {return !arr1.some(ele>…

亞倫?斯沃茨:提升時間的品質

亞倫?斯沃茨&#xff1a;提升時間的品質 不休不眠地工作是低效的&#xff0c;怎樣發揮時間的最大效率&#xff1f; 伏維閣主 2013-01-18 16:45圖片來源&#xff1a;personalitytutor.com &#xff08;亞倫?斯沃茨Aaron Swartz / 文&#xff09;《 怎樣有效利用時間 》中給出的…

useEffect中使用異步函數

useEffect是不能直接用 async await 語法糖的 /* 錯誤用法 &#xff0c;effect不支持直接 async await*/useEffect(async ()>{/* 請求數據 */const res await getData()},[]) useEffect 的回調參數返回的是一個清除副作用的 clean-up 函數。因此無法返回 Promise&#xf…

Flex4_HttpService組件

1、在JavaWeb項目中新建Servlet(FlexLoginServelt) &#xff1a; public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setContentType("text…

想要提高記憶力?做夢去吧!

想要提高記憶力&#xff1f;做夢去吧&#xff01; 豆豆助 2010-11-26 11:47經驗說&#xff1a;不好好學習&#xff0c;就知道睡覺 實驗說&#xff1a;要想記憶好&#xff0c;睡覺不能少。做夢可以增強我們的記憶。 有沒有辦法能讓人不費吹灰之力增強記憶力&#xff1f; 做夢吧你…

This beta version of Typora is expired, please download and install a newer version. 解決方案

原文鏈接&#xff1a;https://blog.csdn.net/yyywxk/article/details/125133205 問題描述 今日打開 Typora 軟件時&#xff0c;突然報錯&#xff0c;提示軟件版本已過期&#xff0c;需要安裝新版本&#xff0c;如下圖所示。 關閉錯誤提示框后會自動跳轉到&#xff1a;https://…

MySQL查詢count(*)、count(1)、count(field)的區別收集

count(*)對行的數目進行計算&#xff0c;包含NULLcount(column)對特定的列的值具有的行數進行計算&#xff0c;不包含NULL值。 count()還有一種使用方式&#xff0c;count(1)這個用法和count(*)的結果是一樣的。 任何情況下select count(*) from tablename是最優選擇&#xff1…

真想學習?扔掉你詳細的計劃吧!

真想學習&#xff1f;扔掉你詳細的計劃吧&#xff01; 該不該定個計劃&#xff1f;詳細的計劃確實可以更高效&#xff0c;也許可以將你減肥的速度加快一倍。但是如果想學習&#xff0c;還是扔掉詳細的計劃吧&#xff01;因為它會占用你太多的認知資源&#xff0c;只顧著悶頭做事…

Swift中文件和圖片上傳處理

前言&#xff1a;基于Alamofire4.5網絡框架開發的Swift項目中上傳文件和圖片代碼參考。 上傳文件 extension HttpRequestService {typealias UploadDoneBlock (_ dict:[String : Any]) -> Void//成功blockfunc uploadEcgFileWithGatherRecord(model :RecordModel, fileName…

下載各種在線視頻及字幕

1. http://en.savefrom.net/ 支持30多個有名的網頁呢 &#xff0c;YouTube等等 Supported resourcesHow to use?filefactory.comsendspace.com youtube.commetacafe.combreak.comdailymotion.comvimeo.comsevenload.commail.rusmotri.comyandex.rurambler.rutvigle.ruintv.ru…

React Icons

發現一個Icon庫,分享下哈; 使用React-Icons輕松地在React項目中包含流行圖標&#xff0c;它利用es6導入&#xff0c;允許只包含項目正在使用的圖標。 Installation (for standard modern project) npm install react-icons --save Usage import { FaBeer } from react-ico…

對flex深入研究一點

flex頂層設計 1.在任何流動的方向上(包括上下左右)都能進行良好的布局 2.可以以逆序 或者 以任意順序排列布局 3.可以線性的沿著主軸一字排開 或者 沿著側軸換行排列 4.可以彈性的在任意的容器中伸縮大小 5.可以使子元素們在容器主軸方向上 或者 在容器側軸方向上 進行對齊 主軸…

那些你可能還不知道的新發明

The Wello WaterWheel 對世界上六分之一的人來說&#xff0c;取水是個苦活&#xff1a;步行數小時&#xff0c;排隊等待&#xff0c;負重運送。 而花費在這個基本需求上的時間讓許多兒童不得不輟學&#xff0c;讓女人不能負起責任來處理家務或者工作以取得收入。在很多欠發達地…

AntD中Form的input正則校驗:不能輸入空格

什么是正則表達式&#xff1f; 在編寫處理字符串的程序或網頁時&#xff0c;經常有查找符合某些復雜規則的字符串的需要。正則表達式就是用于描述這些規則的工具。換句話說&#xff0c;正則表達式就是記錄文本規則的代碼。 正則test() 方法 定義和用法 test() 方法用于檢測一…