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; }
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]; }
// find top-k CC using boost graph representation PQ find_top_CC(DeterministicGraph& PW, int k) { typename graph_traits <DeterministicGraph>::out_edge_iterator out1, out2; PQ pq; // priority queue for top-k CC int V = num_vertices(PW); bool *explored = new bool[V+1]; for (int i = 0; i < V; ++i) explored[i] = false; int cc_number = 0; int unit_sized = 0; for (int i = 0; i < V; ++i) { if (!explored[i]) { // perform BFS for vertex i vector<int> CC; CC.push_back(i); explored[i] = true; vector<int>::size_type ix = 0; while (ix < CC.size()) { Vertex u = vertex(CC[ix], PW); for (tie(out1, out2) = out_edges(u, PW); out1 != out2; ++out1) { Vertex v = target(*out1, PW); if (!explored[v]) { CC.push_back(v); explored[v] = true; } } ix++; } // if (CC.size() > 5) // cout << cc_number << ": " << CC.size() << endl; if (CC.size() == 1) { unit_sized++; } cc_number++; // maintain CC priority queue int pq_size = pq.size(); if (pq_size == k) { vector<int> top = pq.top(); if (top.size() < CC.size()) { pq.pop(); pq.push(CC); } } else pq.push(CC); } } // cout << "Total CCs: " << cc_number << endl; // cout << "Unit CCs: " << unit_sized << endl; return pq; }
// find top-k CCs using vectors graph representation PQ find_top_CC2(vector<vector<int> >& PW, int V, int k) { PQ pq; // priority queue for top-k CC map<int, bool> explored; for (int i = 0; i < V; ++i) explored[i] = false; int cc_number = 0; int unit_sized = 0; for (int i = 0; i < V; ++i) { if (!explored[i]) { // perform BFS for vertex i vector<int> CC; CC.push_back(i); explored[i] = true; vector<int>::size_type ix = 0; while (ix < CC.size()) { int pw_size = PW[CC[ix]].size(); for (int j = 0; j < pw_size; ++j) { int v = PW[CC[ix]][j]; if (!explored[v]) { CC.push_back(v); explored[v] = true; } } ix++; } // if (CC.size() > 5) // cout << cc_number << ": " << CC.size() << endl; if (CC.size() == 1) { unit_sized++; } cc_number++; // maintain CC priority queue int pq_size = pq.size(); if (pq_size == k) { vector<int> top = pq.top(); if (top.size() < CC.size()) { pq.pop(); pq.push(CC); } } else pq.push(CC); } } // cout << "Total CCs: " << cc_number << endl; // cout << "Unit CCs: " << unit_sized << endl; return pq; }
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(); } }
VectorXf GeodesicDistance::compute(int nd) const { PQ q; q.push( Node( nd, 0 ) ); VectorXf d = 1e10*VectorXf::Ones( N_ ); updatePQ( d, q ); return d; }
int main(){ int n,m; cin>>n>>m; while(m--){ int u,v; ll c; cin>>u>>v>>c; pq.push({c,pii(u,v)}); E.eb(c,pii(u,v)); } djs.init(1006); int pointsssss=1; ll firstttttt=0; while(pq.size()){ if(!djs.C(pq.top().Y.X,pq.top().Y.Y)){ G[pq.top().Y.X].eb(pq.top().Y.Y,pq.top().X); G[pq.top().Y.Y].eb(pq.top().Y.X,pq.top().X); ++pointsssss; firstttttt+=pq.top().X; djs.U(pq.top().Y.X,pq.top().Y.Y); } pq.pop(); } if(pointsssss<n)return cout<<"-1 -1"<<endl,0; if(E.size()==n-1)return cout<<firstttttt<<" -1"<<endl,0; dfs(1,1,1); PDE1(firstttttt); ll diffffffff=1000000000000000000ll; for(auto i:E){ if(p[0][i.Y.X]==i.Y.Y || p[0][i.Y.Y]==i.Y.X)continue; PDE1(i); diffffffff=min(diffffffff,i.X-lca(i.Y.X,i.Y.Y)); } cout<<firstttttt<<" "<<firstttttt+diffffffff<<endl; }
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; }
GeodesicDistance& GeodesicDistance::update( const VectorXf &new_min ) { PQ q; for( int i=0; i<N_; i++ ) if( new_min[i] < d_[i] ) q.push( Node( i,new_min[i] ) ); updatePQ( d_, q ); return *this; }
/*! Updates the queue with the given distance and point \param dist Distance of point to be added \param p index of point to be added \return True if a point was added to the queue */ bool update(double dist, long int p) { if(size() < K) { q_intelement tq(dist, p); pq.push(tq); return true; } else if(topdist() > dist) { pq.pop(); q_intelement tq(dist, p); pq.push(tq); return true; } return false; }
VectorXf GeodesicDistance::compute(const VectorXf &start) const { PQ q; for( int i=0; i<N_; i++ ) if( start[i] < 1e5 ) q.push( Node( i, start[i] ) ); VectorXf d = 1e10*VectorXf::Ones( N_ ); updatePQ( d, q ); return d; }
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 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(){ while(scanf("%d%d",&n,&m)!=EOF,n){ init(); for(int i=0,a,b,p,t;i<m;i++){ a=rit(),b=rit(),p=rit(),t=rit(); g[a][b]={t,p}; } nb=rit(),ne=rit(); rT(STst),rT(STed); ts=getT(STst);te=getT(STed); PQ<_pq,vector<_pq>,greater<_pq>> pq; pq.push({nb,ts}); for(int _i=0;_i<n;++_i){ while(S(pq)&&v[pq.top().pt]){ pq.pop(); } if(S(pq)==0)break; v[pq.top().pt]=1; d[pq.top().pt]=pq.top().tm; int np=pq.top().pt,nt=pq.top().tm; for(int i=1;i<=n;++i){ // waiting time if(g[np][i].lo!=0 && d[i]>d[np]+g[np][i].lo+(g[np][i].ev-(d[np]%g[np][i].ev)%g[np][i].ev)){ d[i]=d[np]+g[np][i].lo+(g[np][i].ev-(d[np]%g[np][i].ev)%g[np][i].ev); pq.push({i,d[np]+g[np][i].lo+(g[np][i].ev-(d[np]%g[np][i].ev)%g[np][i].ev)}); } } } cout<<ts<<" "<<te<<endl; if(d[ne]==0x7f7f7f7f){ printf("No way\n"); continue; } else{ printf("--%d--\n",d[ne]); } } }
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(){ // freopen("in","r",stdin); // freopen("out","w",stdout); int ks=0,n,m;while(rit(n,m),n){ while(m--){ int a,b,l;rit(a,b,l); G[a].pb({b,l}); G[b].pb({a,l}); } pq.push({1,0}); while(pq.size()){ while(v[pq.top().X])pq.pop(); v[pq.top().X]=1; } } }
int main(){ ll n,r,avg; cin>>n>>r>>avg; ll totgn=0,need=n*avg; for(int i=0;i<n;++i)cin>>a[i]>>b[i],totgn+=a[i],pq.push({b[i],r-a[i]}); ll ans=0; while(totgn<need){ // PDE2(pq.top().es,pq.top().lft); if(pq.top().lft>=need-totgn){ ans+=pq.top().es*(need-totgn); break; } ans+=pq.top().es*pq.top().lft; totgn+=pq.top().lft; pq.pop(); } cout<<ans<<endl; }
GeodesicDistance& GeodesicDistance::update( int nd, float v ) { PQ q; q.push( Node( nd, v ) ); updatePQ( d_, q ); return *this; }