void Floyd::calculate_APSP() { int i, j, k; int dest_Rank = 0, curr_Done = 0; for (k = 0; k < nodes; k++, curr_Done++) { int tq = nodes / num_Proc + ((nodes - num_Proc * (nodes / num_Proc)) > dest_Rank); if (tq == curr_Done) { curr_Done = 0; dest_Rank++; } if (dest_Rank == global_Rank) MPI_Bcast(&(local_Matrix[k % q][0]), nodes, MPI_INT, dest_Rank, MPI_COMM_WORLD); else MPI_Bcast(&(local_Matrix_K[0]), nodes, MPI_INT, dest_Rank, MPI_COMM_WORLD); for (i = 0; i < q; i++) for (j = 0; j < nodes; j++) if (dest_Rank == global_Rank) relax(local_Matrix[i][j], local_Matrix[i][k], local_Matrix[k % q][j]); else relax(local_Matrix[i][j], local_Matrix[i][k], local_Matrix_K[j]); } }
/* ------ WRITER ------ */ void writer_lock(rw_lock* rw) { int success = 0; // local var int mytid = nf_tid(); while(!success) { while(rw->writer_pending && (mytid != rw->writer_id)) relax(); while(rw->reader_cnt || rw->writing) relax(); nf_lock(rw->lock); if(!(rw->writer_pending)) { rw->writer_pending = 1; rw->writer_id = mytid; } if(!(rw->reader_cnt || rw->writing)) { rw->writing = 1; success = 1; } nf_unlock(rw->lock); } }
/* Funcao que implementa o algoritmo Bellman-Ford Recebe um array de vertices (um grafo) Recebe um inteiro que indica o numero de vertices existentes */ void BellmanFord(vertex graph, int max_vertex) { int i, v, changed; link adj; for (i = 1; i < max_vertex - 1; i++) { changed = 0; for (v = 0; v < max_vertex; v++) { if (graph[v].change != 1) { for (adj = graph[v].edges -> begin; adj != NULL; adj = adj -> next) { relax(graph[v], adj -> vertex, adj); changed = 1; } graph[v].change = 1; } } if (changed == 0) {break;} } for (v = 0; v < max_vertex; v++) { for (adj = graph[v].edges -> begin; adj != NULL; adj = adj -> next) { if ((relax(graph[v], adj -> vertex, adj) == 1) && (graph[v].cycle != 1)) { graph[v].cycle = 1; BFS(graph, graph + v); } } } }
void split( int root, int div, int*L, int*R ){ push( root ); if( root == 0 ) { *R = *L = 0; return; } if( div == 0 ) { *L = 0; *R = root; relax( root ); return; } if( ar[ar[root].l].sz >= div ) { split( ar[root].l, div, L, R ); ar[root].l = *R; *R = root; relax( *R ); relax( *L ); } else { split( ar[root].r, div - ar[ar[root].l].sz - 1, L, R ); ar[root].r = *L; *L = root; relax( *L ); relax( *R ); } }
int bellman_ford(struct Matrix_Graph *mat_graph, \ struct Point_Graph *poi_graph, int source) { int i, j; struct Edge *edge; for (i = 0; i < poi_graph->node_num; i++) for (j = 0; j < poi_graph->node_num; j++) { edge = poi_graph->node[j].start; if (mat_graph->shortest_matrix[source][j] == INT_MAX) continue; while (edge) { relax(mat_graph, source, j, edge); edge = edge->next; } } for (j = 0; j <= poi_graph->node_num; j++) { edge = poi_graph->node[j].start; while (edge) { if (relax(mat_graph, source, j, edge) != -1) return 0; edge = edge->next; } } return 1; }
Node* insert(Node* t, Node* m) { if (t == null || m->y > t->y) { split(t, m->x, m->l, m->r); return relax(m); } if (m->x < t->x) t->l = insert(t->l, m); else t->r = insert(t->r, m); return relax(t); }
// Inserts node m at position x (0-based) Node* insert(Node* t, Node* m, int x) { if (t == null || m->y > t->y) { split(t, x - 1, m->l, m->r); return relax(m); } // apply lazy propagation here to t propagate(t); if (x <= leftCount(t)) t->l = insert(t->l, m, x); else t->r = insert(t->r, m, x - 1 - leftCount(t)); return relax(t); }
// Puts all elements <= x in l and all elements > x in r. void split(Node* t, int x, Node* &l, Node* &r) { if (t == null) l = r = null; else { if (t->x <= x) { split(t->r, x, t->r, r); l = relax(t); } else { split(t->l, x, l, t->l); r = relax(t); } } }
Node* merge(Node* l, Node *r) { if (l == null) return relax(r); if (r == null) return relax(l); if (l->y > r->y) { l->r = merge(l->r, r); return relax(l); } else { r->l = merge(l, r->l); return relax(r); } }
Node* erase(Node* t, int x) { if (t == null) return null; if (t->x == x) { Node *q = merge(t->l, t->r); delete t; return relax(q); } else { if (x < t->x) t->l = erase(t->l, x); else t->r = erase(t->r, x); return relax(t); } }
void mgrid::LinearMultigrid::multigrid() { // Check that source array has been set if (not(sourceIsSet)) return; // Constants const Level finestLevel = solution.finestLevel; const Level coarsestLevel = solution.coarsestLevel; // Initialise right-hand-side for (Level level=finestLevel; level>0; level--) { source.coarsen(level); solution.coarsen(level); } // Solve on coarsest level relax(coarsestLevel, residualTolerance); // Full Multigrid loop for (Level fineLevel=1; fineLevel<=finestLevel; fineLevel++) { // V-cycle loop at each (successively finer) level solution.refine(fineLevel-1); // interpolate to next level for (int cycle=0; cycle < cycleType; cycle++) { // Downstroke of cycle: // -- New residual: r(2h) = 0 (see note below) // -- New rhs: f(2h) = R.f(h) - L.u(2h) // Note that each sucessive level in the downstroke is calculating // a _residual_, not a coarser version of the solution. Each level // therefore needs to be set to zero on the way down. for (Level level=fineLevel; level>0; level--) { relax(level, preRelax); evaluate_residual(level, temp[level]); temp.coarsen(level, source[level-1]); solution[level-1] = 0; // initialise next level's residual } // Solve problem on coarsest level relax(coarsestLevel, residualTolerance); // Upstroke of cycle: // -- Correction: u(h) <- u(h) + I.u(2h) for (Level level=1; level<=fineLevel; level++) { solution.refine(level-1, temp[level]); solution[level] += temp[level]; relax(level, postRelax); } } } // Do final update relax(finestLevel, postRelax); solution[finestLevel].update_boundaries(); }
void split(Node* t, int x, Node* &l, Node* &r) { if (t == null) l = r = null; else { // apply lazy propagation here to t propagate(t); if ( x < leftCount(t) ) { split(t->l, x, l, t->l); r = relax(t); } else { split(t->r, x - 1 - leftCount(t), t->r, r); l = relax(t); } } }
Node* erase(Node* t, int x) { if (t == null) return null; // apply lazy propagation here to t propagate(t); if (leftCount(t) == x) { Node *q = merge(t->l, t->r); delete t; return relax(q); } else { if (x < leftCount(t)) t->l = erase(t->l, x); else t->r = erase(t->r, x - 1 - leftCount(t)); return relax(t); } }
/** * Wait a selected number of timer tick */ void timer_wait(uint_fast32_t ticks) { uint_fast32_t cur_tick = timer_ticks; uint_fast32_t end_tick = cur_tick + ticks; /* Handle overflow at the 2147483648:th (slightly below 25 days at 1000 hz) tick! */ if (unlikely(end_tick < cur_tick)) while (timer_ticks >= cur_tick) relax(); while (timer_ticks < end_tick) relax(); }
int merge( int L, int R ) { push( L ); push( R ); if( L == 0 ) return R; if( R == 0 ) return L; if( ar[L].y < ar[R].y ) { ar[L].r = merge( ar[L].r, R ); relax( L ); return L; } else { ar[R].l = merge( L, ar[R].l ); relax( R ); return R; } }
BellmanFordSP::BellmanFordSP(const EWDiGraph& graph, int source) : distances(graph.getVertexCount(), std::numeric_limits<double>::max()), edges(graph.getVertexCount(), Edge(0, 0, 0.0)) { std::queue<int> considerVertecies; for (int i = 0; i < graph.getVertexCount(); i++) { considerVertecies.push(i); } distances[source] = 0.0; for (int i = 0; i < graph.getVertexCount(); i++) { std::queue<int> nextPassVertecies; while (!considerVertecies.empty()) { if (i == graph.getVertexCount() - 1) findNegativeCycle(considerVertecies.front()); EWDiGraph::EdgesList adjacent = graph.getAdjacentVertex(considerVertecies.front()); for (auto k = adjacent.begin(); k != adjacent.end(); ++k) { relax(*k, nextPassVertecies); } considerVertecies.pop(); } considerVertecies = nextPassVertecies; } }
// Dijkstra's algorithim for computing shortest path void Graph::computeShortestPaths(int aNodeId) { initializeShortestPath(aNodeId); Heap<Node*> heap(numNodes()); // Insert all the nodes into the heap for (const SLLNode<Node*>* curr = mNodes.head(); curr != NULL; curr = curr->next()) { Node* currNode = curr->value(); heap.insertIgnoringHeapOrder(currNode); } // Heapify into a min heap heap.bottomUpMinHeap(); // You can call heap.removeMin() to extract the min while(!heap.isEmpty()){ Node* u = heap.removeMin(); SLinkedList<Edge*> edges = u->getEdges(); for (SLLNode<Edge*>* curr = edges.head(); curr != NULL; curr = curr->next()){ Edge* e = curr->value(); relax(e); } heap.bottomUpMinHeap(); } }
void MCMultiGrid::relax (MultiFab& solL, MultiFab& rhsL, int level, Real eps_rel, Real eps_abs, MCBC_Mode bc_mode) { // // Recursively relax system. Equivalent to multigrid V-cycle. // At coarsest grid, call coarsestSmooth. // if (level < numlevels - 1 ) { for (int i = preSmooth() ; i > 0 ; i--) { Lp.smooth(solL, rhsL, level, bc_mode); } Lp.residual(*res[level], rhsL, solL, level, bc_mode); prepareForLevel(level+1); average(*rhs[level+1], *res[level]); cor[level+1]->setVal(0.0); for (int i = cntRelax(); i > 0 ; i--) { relax(*cor[level+1],*rhs[level+1],level+1,eps_rel,eps_abs,bc_mode); } interpolate(solL, *cor[level+1]); for (int i = postSmooth(); i > 0 ; i--) { Lp.smooth(solL, rhsL, level, bc_mode); } } else { coarsestSmooth(solL, rhsL, level, eps_rel, eps_abs, bc_mode); } }
/* iterates over and relaxs all of node's edges */ void relaxEdges(Heap h, Node n){ Edge e = n->head; while (e != NULL){ relax(h,n,e); e = e->next; } }
void solve2(int size, int* years, int* answer, int cap, int *iterations, int trueAns, float TL) { n = size; c = cap; bestAns = 0; for (int i = 0; i < n; ++i) { arr[i].first = years[i]; arr[i].second = i; } int iters = 0; LL a = clock(); while ((clock() - a) * 1.0 / CLOCKS_PER_SEC < TL) { relax(); if (bestAns < trueAns) { iters++; } else if (bestAns == trueAns) { break; } std::random_shuffle(arr, arr + n); } *iterations = iters; sort(ALL(ans)); for (int i = 0; i < ans.size(); ++i) { answer[ans[i]] = 1; } }
bool spfa(int s, int t){ for(int i = 0; i < N; i++){ pdist[i] = INF; pinq[i] = false; ppre[i] = -1; } pdist[s] = 0; pminflow[s] = INF; int front = 0, rear = 0; pque[0] = s; pinq[s] = true; int u, v, ee; while(front <= rear){ u = pque[front++]; pinq[u] = false; ee = phead[u]; while(ee != -1){ v = pedges[ee].pnt; if(pedges[ee].cns <= 0){ ee = pedges[ee].nxt; continue; } if(relax(u, v, pedges[ee].cot, ee) && !pinq[v]){ pinq[v] = true; pque[++rear] = v; } ee = pedges[ee].nxt; } } return pdist[t] < INF; }
void dijkstra(MGraph *G, int s) { int i, *set, u, v; MinQueue queue; initialize_single_source(G, s); set = malloc(sizeof(int)*G->numVertexes); queue.size = G->numVertexes; queue.heap = malloc(sizeof(int)*(G->numVertexes+1)); //初始化集合S为空 for(i=0;i<G->numVertexes;i++) { set[i] = 0; } set[s] = 1; //初始化队列为空 for(i=0;i<G->numVertexes;i++) { queue.heap[i+1] = i; } //逐步添加最小权值边 while(queue.size>0) { build_min_heap(&queue); u = extract_min(&queue); set[u] = 1; for(i=0;i<G->numVertexes;i++) { if(G->edges[u][i] != INFINITY) { relax(u,i, G->edges[u][i]); } } } }
int main() { long i,t,edge; bool flag; scanf("%ld",&t); while(t--) { scanf("%ld %ld",&n,&edge); init(); for(i=0; i<edge; i++) scanf("%ld %ld %ld",&a[i],&b[i],&w[i]); for(i=0;i<edge;i++) relax(a[i],b[i],w[i]); flag=1; for(i=0;i<edge;i++) if(d[b[i]]>(d[a[i]]+w[i])) { flag=0; break; } if(!flag) printf("possible"); else printf("not possible"); printf("\n"); } return 0; }
Node* merge(Node* l, Node *r) { if (l == null) return relax(r); if (r == null) return relax(l); if (l->y > r->y) { // apply lazy propagation here to l propagate(l); l->r = merge(l->r, r); return relax(l); } else { // apply lazy propagation here to r propagate(r); r->l = merge(l, r->l); return relax(r); } }
int _tmain(int argc, _TCHAR* argv[]) { for (int i = 0; i < 5; i++){ mystruct[i].d = 100; mystruct[i].p = NULL; } mystruct[0].d = 0; mystruct[0].a = 's'; mystruct[1].a = 't'; mystruct[2].a = 'x'; mystruct[3].a = 'y'; mystruct[4].a = 'z'; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) for (int k = 0; k < 5; k++) relax(j, k); for (int i = 1; i < 5; i++) { printf("s到%c距离为%d\n ", mystruct[i].a, mystruct[i].d); MyStruct* m = &mystruct[i]; do{ printf("%c", m->a); m = m->p; } while (m->p != NULL); printf("s"); printf("\n"); } return 0; }
// this preconditioner first initializes phihat to (IA)phihat = rhshat // (diagonization of L -- A is the matrix version of L) // then smooths with a couple of passes of levelGSRB void VCAMRPoissonOp2::preCond(LevelData<FArrayBox>& a_phi, const LevelData<FArrayBox>& a_rhs) { CH_TIME("VCAMRPoissonOp2::preCond"); // diagonal term of this operator in: // // alpha * a(i) // + beta * sum_over_dir (b(i-1/2*e_dir) + b(i+1/2*e_dir)) / (dx*dx) // // The inverse of this is our initial multiplier. int ncomp = a_phi.nComp(); CH_assert(m_lambda.isDefined()); CH_assert(a_rhs.nComp() == ncomp); CH_assert(m_bCoef->nComp() == ncomp); // Recompute the relaxation coefficient if needed. resetLambda(); // don't need to use a Copier -- plain copy will do DataIterator dit = a_phi.dataIterator(); for (dit.begin(); dit.ok(); ++dit) { // also need to average and sum face-centered bCoefs to cell-centers Box gridBox = a_rhs[dit].box(); // approximate inverse a_phi[dit].copy(a_rhs[dit]); a_phi[dit].mult(m_lambda[dit], gridBox, 0, 0, ncomp); } relax(a_phi, a_rhs, 2); }
void dijkstra(ALGraph G,int s,int d[],int pi[],int Q[]) { //Q[]是最小优先队列,Q[1..n]中存放的是图顶点标号,Q[0]中存放堆的大小 //优先队列中有key的概念,这里key可以从d[]中取得。比如说,Q[2]的大小(key)为 d[ Q[2] ] initSingleSource(G,s,d,pi); //1、初始化结点工作 //2、初始化队列 Q[0] = G.vexnum; int i=1; for(;i<=Q[0];i++) { Q[i] = i-1; } Q[1] = s; Q[s+1] = 0; int u; int v; while(Q[0]!=0) { buildMinHeap(Q,d); //3.1、建立最小堆 u = extractMin(Q,d); //3.2、从最小队列中,抽取最小结点 ArcNode* arcNodePt = G.vertices[u].firstarc; while(arcNodePt!=NULL) { v = arcNodePt->adjvex; relax(u,v,G,d,pi); //4、松弛操作。 arcNodePt = arcNodePt->nextarc; } } }
SandPile::SandPile(int dimension, int sidelength, bool initialize): dimension(dimension),sidelength(sidelength),nrOfElements(pow(sidelength,dimension)),zk(2*dimension) { // lattice = std::vector<int> (nrOfElements); lattice.resize(nrOfElements); if(initialize) fillLatticeRand(zk,zk*2); relax(lattice); }
/* Run the Bellman-Ford algorithm from vertex s. Fills in arrays d and pi. */ int bellman_ford(int first[], int node[], int next[], double w[], double d[], int pi[], int s, int n) { int u, v, i, j; initialize_single_source(d, pi, s, n); for (i = 1; i <= n-1; ++i) { for (u = 1; u <= n; ++u) { j = first[u];// pour chaque sommet while (j > 0) { v = node[j]; relax(u, v, w[j], d, pi); j = next[j]; } } } for (u = 1; u <= n; ++u) { j = first[u]; while (j > 0) { v = node[j]; if (d[v] > d[u] + w[j]) return 0; j = next[j]; } } return 1; }
int main () { freopen ("maxsum.in", "rt", stdin); freopen ("maxsum.out", "wt", stdout); int m; scanf ("%d %d %I64d", &n, &m, &k); for (int i = 0; i < n; i++) scanf ("%d", &prize[i]); for (int i = 0; i < m; i++) { int a, b; scanf ("%d %d", &a, &b); add_edge (a - 1, b - 1); } build (); #ifdef _DBG_ for (int i = 0; i < n; i++, printf ("\n")) for (int k = 0; k <= n; k++, printf ("\n")) for (int j = 0; j < n; j++) printf ("%d ", path[i][k][j]); #endif for (int i = 0; i < n; i++) for (int a = 0; a <= n; a++) for (int b = 1; b <= n; b++) for (int c = 0; c <= n; c++) relax (i, a, b, c); printf ("%I64d", ans); return 0; }