void solve(){ fscanf(fi,"%ld%ld",&n,&m); long i; g.s=n; for(i=1;i<=n;++i){ index(g,i).s=0; } long u,v,c; for(i=0;i<m;++i){ fscanf(fi,"%ld%ld%ld",&u,&v,&c); pute(index(g,u),v,c); printf("%ld %ld %ld\n",u,v ,c); } for(i=1;i<=n;++i){ dijkstra(i); } }
/*主程式*/ main() { int Vs = 0; /*Vs為起始頂點*/ int Vi, i; dijkstra(Vs); for(Vi = 0; Vi < V; Vi++){ if (Vi == Vs) continue; printf("V%d到V%d的最短距離為%d,\t路徑為V%d-->",Vs, Vi, distance[Vi], Vi); for(i = previous[Vi]; i != Vs ; i = previous[i]) printf("V%d-->", i); printf("V%d\n", Vs); } getchar(); }
int main() { char i, j , k = 5; //char u = 1, v = 2, w = 4; store_cpu_rate(16); P0_DIR &= ~0x28; P0_ALT &= ~0x28; serial_init(19200); for(n=0;n<6;n++) { blink_led(); mdelay(400); } n = GRAPHSIZE; while(1) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) dist[i][j] = (k++)%30; //n = -1; //for (i = 0; i < 6; i++) { //fscanf(fin, "%d%d%d", &u, &v, &w); // dist[u][v] = w++; // n = MAX(u, MAX(v+w, n)); //} //fclose(fin); for(j=0;j<n;j++) dijkstra(j); dij_counter++; int_print(dij_counter); puts("\r\n"); printD(); } return 0; }
pair<cap_t, cost_t> maximum_flow(double source, double sink) { cap_t total_flow = 0; cost_t total_cost = 0; while (dijkstra(source, sink)) { cap_t f = mincap[sink]; total_flow += f; for (double p = sink; p != source;) { auto &backward = graph[p][from[p]]; auto &forward = graph[backward.target][backward.rev]; forward.cap -= f; backward.cap += f; total_cost += forward.cost * f; p = backward.target; } } return make_pair(total_flow, total_cost); }
int main() { freopen("input.txt", "r", stdin); scanf("%d%d%d", &n, &s, &m); for (int i = 1; i <= s; i++) { int x; scanf("%d", &x); w[x] = true; } for (int i = 1; i <= m; i++) { scanf("%d%d%d", &r[i].x, &r[i].y, &r[i].c); addedge(r[i].x, r[i].y, r[i].c); } dijkstra(); t = 0; std::fill(h + 1, h + n + 1, 0); std::fill(fa + 1, fa + s + 1, 0); for (int i = 1; i <= m; i++) { tree[i] = (Road){c[r[i].x], c[r[i].y], d[r[i].x] + d[r[i].y] + r[i].c}; } std::sort(tree + 1, tree + m + 1); for (int i = 1; i <= m; i++) { int fx = getfa(tree[i].x), fy = getfa(tree[i].y); if (fx == fy) continue; addedge(tree[i].x, tree[i].y, tree[i].c); fa[fx] = fy; } std::fill(v + 1, v + n + 1, false); for (int i = 1; i <= s; i++) if (!v[i]) buildtree(i); scanf("%d", &q); for (int cs = 1; cs <= q; cs++) { int x, y, d; scanf("%d%d%d", &x, &y, &d); x = c[x]; y = c[y]; if (getfa(x) != getfa(y)) { puts("NIE"); continue; } int l = getLca(x, y); long long vmax = -INF; vmax = std::max(vmax, getValue(x, dep[x] - dep[l])); vmax = std::max(vmax, getValue(y, dep[y] - dep[l])); printf("%s\n", (vmax <= d) ? "TAK" : "NIE"); } return 0; }
int main() { scanf("%d %d",&nNodos,&nAristas); for(int i = 0; i < nAristas; i++){ int a,b,v; scanf("%d %d %d",&a,&b,&v); agregaArista(a,b,v); } scanf("%d",&inicio); dijkstra(inicio); for(int i=1;i<=nNodos;i++){ printf("%d %d\n",i,resultado[i]); imprimeTrayecto(i); printf("\n"); } return 0; }
int main() { int G[MAX][MAX],i,j,n,u; printf("Enter no. of vertices:"); scanf("%d",&n); printf("\nEnter the adjacency matrix:\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&G[i][j]); printf("\nEnter the starting node:"); scanf("%d",&u); dijkstra(G,n,u); return 0; }
void* startWorking(void* threadArgs){ int k, i; THREADARGS* args = (THREADARGS*)threadArgs; int myID = args->id; /* Current processor No */ uVertex[myID].iDist=NONE; uVertex[myID].iPID=myID; uVertex[myID].iNID=NONE; /* Step 2: */ for (i=myID*(NUM_NODES/PROCESSORS); (i<(myID+1)*(NUM_NODES/PROCESSORS)+(myID+1==PROCESSORS && NUM_NODES%PROCESSORS!=0 ? NUM_NODES%PROCESSORS:0)); i++) { rgnNodes[i].iDist = AdjMatrix[chStart][i]; rgnNodes[i].iPrev = NONE; } actuateBarrier(&myBarrier); /* Actuate the barrier */ /* Step 3: */ dijkstra(myID); return NULL; }
int main() { int graph[NODES][NODES] = { {0, 22, 9, 12, 0, 0, 0, 0, 0}, {22, 0, 35, 0, 0, 36, 0, 34, 0}, {9, 35, 0, 4, 65, 42, 0, 0, 0}, {12, 0, 4, 0, 33, 0, 0, 0, 30}, {0, 0, 65, 33, 0, 18, 23, 0, 0}, {0, 36, 42, 0, 18, 0, 39, 24, 0}, {0, 0, 0, 0, 23, 39, 0, 25, 21}, {0, 34, 0, 0, 0, 24, 25, 0, 19}, {0, 0, 0, 30, 0, 0, 21, 19, 0} }; //Call dijkstra function dijkstra(graph, 0); std::cout<<"\n\n"; return 0; }
void dijkstra(int n){ int i; int min_now = -1; for(i = 1; i < n; i++){ if(!used[i] && dis1[i] != MAX){ if(min_now == -1) min_now = i; else if (dis1[min_now] > dis1[i]) min_now = i; } } if(min_now == -1)return; used[min_now] = 1; for(i = 1; i < n; i++){ if(dis1[min_now] + dis[min_now][i] < dis1[i]) dis1[i] = dis1[min_now] + dis[min_now][i]; } dijkstra(n); }
main(int argc, char* argv[]) { int i, n, m, maxLen, repCount, retVal, seed; int notZero, minTsiz, maxTsiz, avgTsiz; edge e; vertex u, v; Wdigraph dig, *sptree; if (argc != 6 || sscanf(argv[1],"%d",&n) != 1 || sscanf(argv[2],"%d",&m) != 1 || sscanf(argv[3],"%d",&maxLen) != 1 || sscanf(argv[4],"%d",&repCount) != 1 || sscanf(argv[5],"%d",&seed) != 1) fatal("usage: sptUpdate n m maxLen repCount seed"); srandom(seed); dig.rgraph(n,m); dig.randLength(0,maxLen); vertex *p = new int[n+1]; int *d = new int[n+1]; dijkstra(dig,1,p,d); notZero = 0; minTsiz = dig.n(); maxTsiz = 0; avgTsiz = 0; for (i = 1; i <= repCount; i++) { e = randint(1,dig.m()); retVal = sptUpdate(dig,p,d,e,randint(1,maxLen)); if (retVal > 0) { notZero++; minTsiz = min(retVal,minTsiz); avgTsiz += retVal; maxTsiz = max(retVal,maxTsiz); } /* for verification only sptree = new Wdigraph(dig.n,dig.n-1); for (e = dig.first(); e != 0; e = dig.nextOut(e)) { u = dig.tail(e); v = dig.head(e); if (p[v] == u) sptree->join(u,v,d[v]); } check(1,dig,*sptree); delete sptree; */ } cout << setw(6) << notZero << " " << setw(2) << minTsiz << " " << (notZero > 0 ? double(avgTsiz)/notZero : 0.0) << " " << setw(4) << maxTsiz; }
int main() { int i, j, n, v0, v; n = 7; initCost(); printf("Input v0(-1 for quit): "); scanf("%d", &v0); v = v0 - 1; while(v != -2) { for(i = 0; i < n; i++) path[i] = -1; dijkstra(n, v); for(i = 0; i < n; i++) { if(i != v) { if(miniDistance[i] < MAXCOST) { printf("DIST[%d, %d]=%d\n", v0, i+1, miniDistance[i]); //输出最短路径所经过的顶点 printf(" V%d --> ", v0); for(j = 0; path[j] != i; j++) { printf("V%d", path[j]+1); if(path[j+1] != i) printf(" --> "); } if(path[0] == i) printf("V%d", i+1); else printf(" --> V%d", i+1); printf("\n"); } else { printf("DIST[%d, %d]=NONE!\n", v0, i+1, miniDistance[i]); } } } printf("\nInput v0(-1 for quit): "); scanf("%d", &v0); v = v0 - 1; } }
int main() { int arr[SIZE][SIZE] = { 0, 0, 0, 0, 0, 0, 0, INF, 7, 4, 6, 1, 0, INF, INF, INF, INF, INF, 0, INF, 2, INF, 5, INF, 0, INF, 3, INF, INF, INF, 0, INF, INF, INF, 1, INF }; int touch[SIZE] = { 0 }; int length[SIZE] = { 0 }; Edge selectEdge[VERTEX_COUNT] = { 0, 0, 0 }; dijkstra(arr, touch, length, selectEdge); edgePrint(1, length); return 0; }
std::pair < typename dijkstra_state_helper<Graph, Params>::type , typename distance_visitor_helper_indirect<Graph, Params>::type> dijkstra_shortest_path_distance( const Graph& g, typename boost::graph_traits<Graph>::vertex_descriptor source, DistanceValue target_distance, const Params& params) { typedef typename dijkstra_state_helper<Graph, Params>::type state_type; typedef typename distance_visitor_helper<state_type>::type visitor_type; state_type state = make_dijkstra_state(g, params); visitor_type visitor = make_distance_visitor(state, target_distance); resumable_dijkstra<state_type> dijkstra(state); dijkstra.init_from_source(source, visitor); dijkstra.expand(visitor, visitor); return std::make_pair(state, visitor); }
// driver program to test above function int main() { int i, j; /* Let us create the example graph discussed above */ scanf("%d %d", &x, &y); int graph[x][y]; for (i = 0; i < x; i++) { for (j = 0; j < y; j++) { graph[i][j] = 1; } } while (scanf("%d %d", &i, &j) != EOF) graph[i][j] = 500; dijkstra(graph, 0); return 0; }
// driver program to test above function int main() { /* Let us create the example graph discussed above */ int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 0, 10, 0, 2, 0, 0}, {0, 0, 0, 14, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0} }; dijkstra(graph, 0); return 0; }
std::pair < typename dijkstra_state_helper<Graph, Params>::type , typename nearest_source_helper_indirect<Graph, Params>::visitor_type> dijkstra_shortest_path_nearest_source(const Graph& g, const VerticesRange& sources, const Params& params) { typedef typename dijkstra_state_helper<Graph, Params>::type state_type; typedef typename nearest_source_helper<state_type>::visitor_type visitor_type; state_type state = make_dijkstra_state(g, params); visitor_type visitor = make_nearest_source_visitor(state); resumable_dijkstra<state_type> dijkstra(state); dijkstra.init_from_sources(sources, visitor); dijkstra.expand(default_interruptor(), visitor); return std::make_pair(state, visitor); }
int main() { FILE * f = fopen("matrix2.txt","r"); readFromAdjMatrix(f); printAdjMatrix(); bfs(0); dfs(0); dfsRecurs(0); prim(0); dijkstra(0); bellmanFord(0); kruskal(); return 0; }
void imprime_matriz_distancias(grafo_t *g) { int *distancias; int i, j; for (i = 1; i <= g->nvertices; i++) { distancias = dijkstra(g, g->vertices[i]); for (j = 1; j <= g->nvertices; j++) { if (distancias[j] == INFINITE) printf("- "); else printf("%d ", distancias[j]); } printf("\n"); free(distancias); } }
SpecificWorker::State SpecificWorker::plan() { //Check if the point is accesible from robot position // QVec qposR = inner->transform("laser",qpos,"world"); // if ( checkFreeWay( qposR )) // { // qDebug() << __FUNCTION__ << "Free way found. Leaving for VERIFY_POINT"; // return State::VERIFY_POINT; // } // //set the robot's current position in robotNode map->set(robotNode, inner->transform("world","robot")); //search closes node to roobot float dist = std::numeric_limits< float >::max(), d; for (lemon::ListGraph::NodeIt n(graph); n != lemon::INVALID; ++n) { d = (qpos - map->operator[](n)).norm2(); if( d < dist ) { dist = d; closestNode = n; } } qDebug() << __FUNCTION__ << "dist to new point" << d << "at node with position:" << map->operator[](closestNode); //Search shortest path along graph from closest node to robot if( closestNode != robotNode) { float dd; bool reached = dijkstra(graph,*edgeMap).path(path).dist(dd).run(robotNode,closestNode); qDebug() << reached << d; for( lemon::PathNodeIt<lemon::Path<lemon::ListGraph> > pIt(graph, path); pIt != lemon::INVALID; ++pIt) qDebug() << map->operator[](pIt); qDebug() << "---------------"; } return State::GOTO_POINTS; }
void npc_next_pos_rand_tunnel(dungeon *d, character *c, pair_t next) { pair_t n; union { uint32_t i; uint8_t a[4]; } r; do { n[dim_y] = next[dim_y]; n[dim_x] = next[dim_x]; r.i = rand(); if (r.a[0] > 85 /* 255 / 3 */) { if (r.a[0] & 1) { n[dim_y]--; } else { n[dim_y]++; } } if (r.a[1] > 85 /* 255 / 3 */) { if (r.a[1] & 1) { n[dim_x]--; } else { n[dim_x]++; } } } while (mappair(n) == ter_wall_immutable); if (hardnesspair(n) <= 85) { if (hardnesspair(n)) { hardnesspair(n) = 0; mappair(n) = ter_floor_hall; /* Update distance maps because map has changed. */ dijkstra(d); dijkstra_tunnel(d); } next[dim_x] = n[dim_x]; next[dim_y] = n[dim_y]; } else { hardnesspair(n) -= 85; } }
int main() { int i,j,n; int output[NUM_NODES * NUM_NODES]; int output_count = 0; int check_output[NUM_NODES * NUM_NODES] = { 0, 7, 38, 23, 14, 36, 3, 29, 7, 14, 28, 0, 31, 16, 7, 34, 31, 28, 1, 39, 39, 25, 0, 32, 14, 3, 32, 9, 26, 43, 12, 14, 40, 0, 21, 43, 15, 12, 15, 26, 40, 36, 48, 28, 0, 27, 43, 33, 12, 39, 36, 22, 21, 29, 29, 0, 29, 6, 23, 40, 8, 4, 35, 20, 11, 33, 0, 26, 5, 11, 30, 16, 47, 32, 23, 35, 23, 0, 17, 34, 28, 24, 55, 16, 8, 35, 31, 28, 0, 38, 23, 19, 41, 16, 8, 35, 15, 28, 0, 0}; initialise_trigger(); start_trigger(); /* finds 10 shortest paths between nodes */ for(n = 0; n < REPEAT_FACTOR >> 9; ++n) { output_count = 0; for(j = 0; j < NUM_NODES; j++) { for (i=0; i < NUM_NODES; i++) { output[output_count] = dijkstra(i,j); output_count++; } } } stop_trigger(); int to_return = 0; for (i = 0; i < output_count; i++) { if (output[i] != check_output[i]) { to_return = -1; break; } } return to_return; }
double lap(arrow_problem *problem, int delta, int *x, int *y, int *pi, int *d, int *pred, int *label, arrow_heap *heap) { int i, j, t; int n = problem->size; /* Initialization */ for(i = 0; i < 2 * n; i++) { x[i] = -1; y[i] = -1; pi[i] = 0; } for(i = 0; i < n; i++) { /* Find the shortest path from i to any demand node t */ dijkstra(problem, delta, x, y, pi, i, &t, d, pred, label, heap); /* If we cannot reach a demand node then problem's infeasible */ if(t == -1) return DBL_MAX; /* Update reduced costs */ for(j = 0; j < 2 * n; j++) { if(label[j]) pi[j] = pi[j] - d[j] + d[t]; } /* Augment! */ augment(problem, i, t, pred, x, y); } /* Calculate total cost of assignment, as well as fix final solution. */ double cost = 0.0; for(i = 0; i < n; i++) { x[i] = x[i] - n; cost += problem->get_cost(problem, i, x[i]); } return cost; }
int main(){ int i = 0, j = 0, v = 0, w = 0; int m = 0, k = 0, a = 0, r = 0; double p = 0, P = 0; while(scanf("%d %d %d %lf", &n, &m, &k, &p) != EOF){ for(i = 0; i < n; i++){ peso[i] = 0; for(j = 0; j < n; j++) G[i][j] = 0; } for(i = 0; i < m; i++){ scanf("%d %d", &v, &w); edge(v - 1, w - 1); } scanf("%d", &a); for(i = 0; i < a; i++){ scanf("%d", &r); peso[r - 1]++; } scanf("%d %d", &v, &w); v--; w--; dijkstra(v, w); P = 0; if(dis[w] <= k) P = pot(p, dis[w]); printf("%.3lf\n", P); } return 0; }
int best_route_distance(Flight query, Flight direct_flights[], struct input line) { GRAPH *graph; graph = init_graph(); int i, j, num_df; num_df = line.direct_flights; for(i = 0; i < num_df; i++) { for(j = 0; j < num_df; j++) { joinwt(graph, direct_flights[i].origin.id, direct_flights[i].destination.id, direct_flights[i].distance ); } } return dijkstra(graph, query.origin.id, query.destination.id); }
void simple_query(int fd, query_header qh){ query_simple qs; read(fd, &qs, sizeof(qs)); printf("got query: %d %d %d\n", qs.source_st, qs.dest_st, qs.dep_time); vector<pair<connection, simple_stats> > result; for(int i=0; i<COUNT; i++){ connection conn; simple_stats st; int ret = dijkstra(qs, conn, &st); if(ret == DIJ_NO_CONN) break; else if(ret == DIJ_OK) { result.push_back(make_pair(conn, st)); qs.dep_time = 1 + conn.front().dep_time; } else error(fd, ERR_UNDEFINED, "Dijkstra returned undefined value."); } answer_header ah; ah.answer_type = ANSWER_OK; write(fd, &ah, sizeof(ah)); answer_simple as; as.count = result.size(); write(fd, &as, sizeof(as)); for(vector<pair<connection, simple_stats> >::iterator conn = result.begin(); conn != result.end(); conn++){ answer_simple_conn asc; asc.length = conn->first.size(); write(fd, &asc, sizeof(asc)); for(connection::iterator it = conn->first.begin(); it != conn->first.end(); it++){ answer_simple_elem ase; ase.arr_stop = it->arr_stop; ase.dep_stop = it->dep_stop; write(fd, &ase, sizeof(ase)); } write(fd, &(conn->second), sizeof(conn->second)); } }
int main() { int n,i,val,from1,from2; struct list *temp; printf("Enter number of edge in graph:: "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter two node name space seprated::"); scanf("%d%d",&from1,&from2); printf("Enter distance between two node::"); scanf("%d",&val); addinlist(from1); addinlist(from2); addedge(from1,from2,val); } for(i=1;i<=100;i++) { status[i]=0; pathlength[i]=9999999; } dijkstra(); while(1) { printf("Enter distination vertex for shortest path or -1 to exit:: "); scanf("%d",&i); if(i==-1) break; else { while(1) { printf("%d",i); i=predecessor[i]; if(i==-1) break; printf("<--"); } } printf("\n"); } return 0; }
int main(){ int t; scanf("%d", &t); for(; t > 0; t--){ init(); int target; scanf("%d%d%d%d", &amax, &bmax, &cmax, &target); status[0][0] = 0; possible[0][0] = 0; possible[0][1] = 0; possiblen++; dijkstra(); int ans = 0, anscost = MAX; int i, j; for(i = 0; i <= amax; i++) for(j = 0; j <= bmax; j++) if(status[i][j] != MAX){ if(i == target || j == target || cmax-i-j == target){ if(status[i][j] < anscost || ans != target) anscost = status[i][j]; ans = target; } else if(ans != target){ if(i >= ans && i < target){ if(i > ans || (i == ans && status[i][j] < anscost)) anscost = status[i][j]; ans = i; } if(j >= ans && j < target){ if(j > ans || (j == ans && status[i][j] < anscost)) anscost = status[i][j]; ans = j; } if(cmax-i-j >= ans && cmax-i-j < target){ if(cmax-i-j > ans || (cmax-i-j == ans && status[i][j] < anscost)) anscost = status[i][j]; ans = cmax-i-j; } } } printf("%d %d\n", anscost, ans); } }
/* Das Array 'd' zeigt die Distanz von dem jeweilgen Knoten zum 'dest'-Knoten an. Das Array 'next' zeigt fuer jeden Knoten an, zu welchem naechsten Knoten es weiter gehen muss um zu dem 'dest'-Knoten zu kommen. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int i, e, dest, *next; struct GRAPH graph,fgraph; double *pr, *d; if (nrhs < 2) mexErrMsgTxt("usage: dijkstra_all(matrix, destination)."); if (!mxIsSparse(prhs[0])) mexErrMsgTxt("first parameter must be sparse."); graph.n = mxGetM(prhs[0]); graph.ep = mxGetJc(prhs[0]); graph.edge = mxGetIr(prhs[0]); graph.ew = mxGetPr(prhs[0]); dest = (int)*mxGetPr(prhs[1]) - 1; if (dest < 0 || dest >= graph.n) mexErrMsgTxt("destination node out of range."); /* printf("dest = %d\n", dest); fflush(stdout); */ if (!(d = (double*)mxCalloc((unsigned)graph.n,sizeof(double))) || !(next= (int*) mxCalloc((unsigned)graph.n,sizeof(int))) ) { printf("ERROR...calloc!\n"); return; } if (dijkstra(graph, dest, d, next)) mexErrMsgTxt("allocation error in New."); plhs[0] = mxCreateDoubleMatrix(1, graph.n, mxREAL); pr = mxGetPr(plhs[0]); for (i=0; i<graph.n; i++) pr[i] = ( d[i]==VERY_LARGE ? -1 : d[i]); if (nrhs > 1) { plhs[1] = mxCreateDoubleMatrix(1, graph.n, mxREAL); pr = mxGetPr(plhs[1]); for (i=0; i<graph.n; i++) pr[i] = next[i]+1; } mxFree(d); mxFree(next); return; }
int main(){ int n, m, count = 0; while(scanf("%d%d", &n, &m), n != 0 || m != 0){ int i; set(n); for(i = 0; i < m; i++){ int a, b, c; scanf("%d%d%d", &a, &b, &c); dis[a-1][b-1] = c; dis[b-1][a-1] = c; if(a == 1)dis1[b-1] = c; if(b == 1)dis1[a-1] = c; } dijkstra(n); int vmax = 0, v = 0; double emax = 0; int e1, e2; for(i = 0; i < n; i++){ int j; for(j = i+1; j < n; j++){ if(dis[i][j] != MAX){ double edgedis = (double)(dis1[i] + dis1[j] + dis[i][j])/2; if(edgedis > emax){ emax = edgedis; e1 = i; e2 = j; } } } if(dis1[i] > vmax){ vmax = dis1[i]; v = i; } } printf("System #%d\n", ++count); if((double)vmax >= emax) printf("The last domino falls after %.1f seconds, at key domino %d.\n\n", (double)vmax, v+1); else printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n", emax, e1+1, e2+1); } return 0; }