当前位置: 首页>>代码示例>>C++>>正文


C++ dijkstra函数代码示例

本文整理汇总了C++中dijkstra函数的典型用法代码示例。如果您正苦于以下问题:C++ dijkstra函数的具体用法?C++ dijkstra怎么用?C++ dijkstra使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了dijkstra函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: solve

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);
  }
}
开发者ID:nphuc,项目名称:alg,代码行数:17,代码来源:graph.c

示例2: main

/*主程式*/ 
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();    
}
开发者ID:ChenBoTang,项目名称:Samples,代码行数:18,代码来源:dijkstra.c

示例3: main

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;
}
开发者ID:xwaynec,项目名称:eplab,代码行数:46,代码来源:main.c

示例4: maximum_flow

	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);
	}
开发者ID:MutesK,项目名称:algospot,代码行数:17,代码来源:CLEANING.cpp

示例5: main

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;
}
开发者ID:PrayStarJirachi,项目名称:Exercise-Code,代码行数:46,代码来源:B.cpp

示例6: main

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;
}
开发者ID:TachoMex,项目名称:Algoritmos-y-estructuras-de-datos-basicas,代码行数:17,代码来源:Dijkstra.cpp

示例7: main

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;
}
开发者ID:Siddharthpratapsingh,项目名称:Siddharth-Singh,代码行数:17,代码来源:dijalgo.c

示例8: startWorking

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;
}
开发者ID:cota,项目名称:parmibench,代码行数:17,代码来源:dijkstra_parallel_squeue.c

示例9: main

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;
}
开发者ID:Sriraman0307,项目名称:Graph-Algorithms,代码行数:17,代码来源:Dijkstra.cpp

示例10: dijkstra

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);
}
开发者ID:pandix73,项目名称:Advanced_Programming,代码行数:17,代码来源:103062135_318.c

示例11: main

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;
}
开发者ID:caiograg,项目名称:grafalgo,代码行数:45,代码来源:sptUpdate.cpp

示例12: main

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;
    }
}
开发者ID:alemic,项目名称:datastruct,代码行数:45,代码来源:shortest.c

示例13: main

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;
}
开发者ID:KimBoWoon,项目名称:C_Practice,代码行数:18,代码来源:DijkstrasAlgorithm.c

示例14: dijkstra_shortest_path_distance

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);
}
开发者ID:ahhz,项目名称:resumable_dijkstra,代码行数:18,代码来源:dijkstra_functions.hpp

示例15: main

// 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;
}
开发者ID:chanthunder,项目名称:prog,代码行数:19,代码来源:dijaksta.c


注:本文中的dijkstra函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。