void testHeap ( int n ) { T* A = new T[2*n/3]; //创建容量为2*n/3的数组,并 for ( int i = 0; i < 2 * n / 3; i++ ) A[i] = dice ( ( T ) 3 * n ); //在其中随机生成2*n/3个词条 /*DSA*/printf ( "%d random keys created:\n", 2 * n / 3 ); /*DSA*/for ( int i = 0; i < 2 * n / 3; i++ ) print ( A[i] ); printf ( "\n" ); PQ heap ( A + n / 6, n / 3 ); //批量建堆(PQ_ComplHeap实现了Robert Floyd算法) delete [] A; /*DSA*/system("cls"); print ( heap ); Sleep(100); while ( heap.size() < n ) { //随机测试 if ( dice ( 100 ) < 70 ) { //70%概率插入新词条 T e = dice ( ( T ) 3 * n ); /*DSA*/printf ( "Inserting" ); print ( e ); printf ( " ...\n" ); heap.insert ( e ); /*DSA*/printf ( "Insertion done\n" ); } else { //30%概率摘除最大词条 if ( !heap.empty() ) { /*DSA*/printf ( "Deleting max ...\n" ); T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" ); } } /*DSA*/system("cls"); print ( heap ); Sleep(100); } while ( !heap.empty() ) { //清空 T e = heap.delMax();/*DSA*/printf ( "Deletion done with" ); print ( e ); printf ( "\n" ); /*DSA*/system("cls"); print ( heap ); Sleep(100); } }
int dijdij(int st,int ed){ v[0].reset(); v[1].reset(); for(int i=0;i<111;++i)d[i].clear(); d[st].pb(0); PQ<pii,vector<pii>,greater<pii>> pq; pq.push({0,st}); while(pq.size()){ while(pq.size() && v[1][pq.top().Y])pq.pop(); if(pq.empty())break; pii now=pq.top(); pq.pop(); int stp=v[0][now.Y]?1:0; PDE3(now,stp,pq); v[stp][now.Y]=1; for(pii e:G[now.Y]){ d[e.X].pb(now.X+e.Y); sort(d[e.X].begin(),d[e.X].end()); d[e.X].resize(unique(d[e.X].begin(),d[e.X].end())-d[e.X].begin()); if(d[e.X].size()>2u){ sort(d[e.X].begin(),d[e.X].end()); d[e.X].pop_back(); } if(now.X+e.Y<=d[e.X].back()){ pq.push({now.X+e.Y,e.X}); } } } if(d[ed].size()<2u)return -1; else return d[ed][1]; }
int main() { int n, m; scanf("%d %d", &n, &m); vector<vector<pii>> edges(n); while(m--) { int u, v, w; scanf("%d %d %d", &u, &v, &w); edges[u].push_back({w, v}); edges[v].push_back({w, u}); } PQ pq; pq.push({0,0}); while(!pq.empty()) { int dist = pq.top().first; int curr = pq.top().second; pq.pop(); if (vis[curr] != false) continue; vis[curr] = true; /* * curr visited in "shortest path" order here */ for(pii it : edges[curr]) { int ndist = it.first + dist; int next = it.second; pq.push({ndist, next}); } } return 0; }
void test2() { PQ p; for (int i = 0;i < 10;i++) { p.push(10-i); } while (p.empty() == false) { cout << p.top() << endl; p.pop(); } }
void test1() { PQ p; p.push(1); p.push(2); p.push(3); p.push(4); p.push(5); while (p.empty() == false) { cout << p.top() << endl; p.pop(); } }
int dijkstra() { REP(i,n) REP(j,c+1) d[i][j] = INF, visited[i][j] = false; d[s][0] = 0; PQ pq; pq.push(State(s, 0, 0)); while(!pq.empty()) { State u = pq.top(); pq.pop(); if(u.node == e and u.fuel == 0) return u.cost; if(visited[u.node][u.fuel]) continue; visited[u.node][u.fuel] = true; /* // Create other states, when I just buy fuel int cost = u.cost; // current cost of fuel for(int f = u.fuel+1; f <= c; f++) { // try to fill until reach capacity c cost += prices[u.node]; int& bcost = d[u.node][f]; if(bcost > cost) { bcost = cost; pq.push(State(u.node, f, cost)); } }*/ FOREACH(el, g[u.node]) { if(el->second <= u.fuel) { int nfuel = u.fuel - el->second; if(u.cost < d[el->first][nfuel]) { d[el->first][nfuel] = u.cost; pq.push(State(el->first, nfuel, u.cost)); } } else if(u.fuel < c) { int cost = u.cost + prices[u.node]; int& bcost = d[u.node][u.fuel+1]; if(bcost > cost) { bcost = cost; pq.push(State(u.node, u.fuel+1, cost)); } } } } return INF; }
void dfs(int rt, int fa) { vis[rt] = true; if (adj[rt].size() == 1 && adj[rt][0] == fa) {f[rt].pb(v[rt]); return;} PQ q; q.push(v[rt]); for (int i=0; i<adj[rt].size(); i++) { int id = adj[rt][i]; if (!vis[id] && id != fa) {dfs(id, rt); for (int j=0; j<f[id].size(); j++) q.push(f[id][j]);} } int cnt = 0; while (!q.empty() && cnt < 3) { f[rt].pb(q.top()); q.pop(); cnt++; } sort(f[rt].begin(), f[rt].end(), greater<int>()); return ; }
int main() { int numbers; int num; PQ myPQ; int sum; int totalCost; while (scanf("%d", &numbers) && numbers != 0) { totalCost = 0; for (int i = 0; i < numbers; i++) { scanf("%d", &num); myPQ.push(num); } while (true) { sum = myPQ.top(); myPQ.pop(); sum += myPQ.top(); myPQ.pop(); totalCost += sum; if (!myPQ.empty()) myPQ.push(sum); else break; } printf("%d\n", totalCost); } return 0; }
int main() { int n, m, l; while (scanf("%d%d%d", &n, &m, &l) == 3) { std::vector<std::vector<int>> graph(n); for (int i = 0, a, b; i < m; ++ i) { scanf("%d%d", &a, &b); a --, b --; graph[a].push_back(b); graph[b].push_back(a); } if (l == 1) { puts("1"); continue; } l --; std::vector<std::vector<Info>> dp(1 << l, std::vector<Info>(n, Info { m + 1, 0 })), merged(n, std::vector<Info>(1 << l, Info { m + 1, 0 })); int root = l; for (int i = 0; i < l; ++ i) { dp[1 << i][i] = { 0, 1 }; } for (int msk = 0; msk < 1 << l; ++ msk) { for (int u = 0; u < n; ++ u) { auto& ref = merged.at(u); for (int subset = msk; subset > 0; subset = subset - 1 & msk) { if (lowbit(subset) == lowbit(msk)) { update(ref.at(msk), add(dp.at(subset).at(u), ref.at(msk ^ subset))); } } } for (int u = 0; u < n; ++ u) { for (int v : graph[u]) { update(dp.at(msk).at(v), add(merged.at(u).at(msk), ONE)); } } auto& ref = dp.at(msk); PQ<std::pair<int, int>> pq; for (int u = 0; u < n; ++ u) { pq.emplace(ref.at(u).first, u); } while (!pq.empty()) { auto top = pq.top(); pq.pop(); int u = top.second; if (top.first == ref.at(u).first) { for (int v : graph.at(u)) { Info todo = add(ref.at(u), ONE); if (todo.first < ref.at(v).first) { pq.emplace(todo.first, v); } update(ref.at(v), todo); } } } for (int u = 0; u < n; ++ u) { update(merged.at(u).at(msk), dp.at(msk).at(u)); // fprintf(stderr, "%s %d %d %d\n", std::bitset<3>(msk).to_string().c_str(), u, dp.at(msk).at(u).first, dp.at(msk).at(u).second); } } printf("%d\n", merged.at(root).at((1 << l) - 1).second); } }
int main(){ CPPinput; int H,n; cin>>H>>n; // for(int i=1;i<=n;++i)cin>>t[i].first>>t[i].second; for(int i=1;i<=n;++i)cin>>t[i].first>>h[i],t[i].second=1e9; PQ<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> keytime; for(int i=1;i<=n;++i)keytime.emplace(t[i].first,i),keytime.emplace(t[i].second,i); if([&]()->bool{for(int i=1;i<=n;++i)if(h[i]==0)return 0;return 1;}())exit((cout<<-1<<endl,0)); double nowh=H,nowt=0; int nowhole=0; multiset<int> activeh; while(nowh>eps){ PDE(nowh,nowt); if(keytime.empty())exit((cout<<-1<<endl,0)); if(nowhole==0){ nowt=keytime.top().first; int i=keytime.top().second; keytime.pop(); if(int(nowt)==t[i].second)continue; if(nowh<=h[i])continue; nowhole++; activeh.insert(h[i]); continue; } double nkt=keytime.top().first; int i=keytime.top().second; // keytime.pop(); double dt=nkt-nowt; double dh=dt*nowhole; int nhh=*prev(activeh.end()); if(nhh!=0){ if(nowh-dh>=nhh){ keytime.pop(); nowt+=dt; nowh-=dh; if(int(nkt)==t[i].first){ if(nowh<=h[i]); else{ nowhole++; activeh.insert(h[i]); } } else{ if(nowh<=h[i]); else{ nowhole--; activeh.erase(activeh.find(h[i])); } } } else{ dh=nowh-nhh; dt=dh/nowhole; nowt+=dt; nowh-=dh; activeh.erase(prev(activeh.end())); nowhole--; } } else{ keytime.pop(); if(dh>nowh)exit((cout<<fixed<<setprecision(14)<<nowt+nowh/nowhole<<endl,0)); else{ nowt+=dt; nowh-=dh; if(int(nkt)==t[i].first){ if(nowh<=h[i]); else{ nowhole++; activeh.insert(h[i]); } } else{ if(nowh<=h[i]); else{ nowhole--; activeh.erase(activeh.find(h[i])); } } } } } cout<<fixed<<setprecision(14)<<nowt<<endl; }