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


C++ Dijkstra::dijkstra方法代码示例

本文整理汇总了C++中Dijkstra::dijkstra方法的典型用法代码示例。如果您正苦于以下问题:C++ Dijkstra::dijkstra方法的具体用法?C++ Dijkstra::dijkstra怎么用?C++ Dijkstra::dijkstra使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Dijkstra的用法示例。


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

示例1: main

int main() {
  int r1, c1, r2, c2, kase = 0;
  while(scanf("%d%d%d%d%d%d", &R, &C, &r1, &c1, &r2, &c2) == 6 && R) {
    r1--; c1--; r2--; c2--;
    for(int r = 0; r < R; r++) {
      for(int c = 0; c < C-1; c++)
        grid[r][c][RIGHT] = grid[r][c+1][LEFT] = readint();
      if(r != R-1) for(int c = 0; c < C; c++)
        grid[r][c][DOWN] = grid[r+1][c][UP] = readint();
    }
    solver.init(R*C*5+1);

    n = 0;
    memset(id, 0, sizeof(id));

    // 源点出发的边
    for(int dir = 0; dir < 4; dir++) if(cango(r1, c1, dir)) {
       solver.AddEdge(0, ID(r1+dr[dir], c1+dc[dir], dir), grid[r1][c1][dir]*2); // 开始走下去
       solver.AddEdge(0, ID(r1+dr[dir], c1+dc[dir], 4), grid[r1][c1][dir]*2); // 走一步停下来
    }

    // 计算每个状态(r,c,dir)的后继状态
    for(int r = 0; r < R; r++)
      for(int c = 0; c < C; c++) {
        for(int dir = 0; dir < 4; dir++) if(cango(r, c, inv[dir])) {
          solver.AddEdge(ID(r, c, dir), ID(r, c, 4), grid[r][c][inv[dir]]); // 停下来!
          if(cango(r, c, dir))
            solver.AddEdge(ID(r, c, dir), ID(r+dr[dir], c+dc[dir], dir), grid[r][c][dir]); // 继续走
        }
        for(int dir = 0; dir < 4; dir++) if(cango(r, c, dir)) {
          solver.AddEdge(ID(r, c, 4), ID(r+dr[dir], c+dc[dir], dir), grid[r][c][dir]*2); // 重新开始走
          solver.AddEdge(ID(r, c, 4), ID(r+dr[dir], c+dc[dir], 4), grid[r][c][dir]*2); // 走一步停下来
        }
      }

    // 跑dijkstra
    solver.dijkstra(0);

    // 找最优解
    int ans = solver.d[ID(r2, c2, 4)];

    printf("Case %d: ", ++kase);
    if(ans == INF) printf("Impossible\n"); else printf("%d\n", ans);
  }
  return 0;
}
开发者ID:Chin-Z,项目名称:aoapc-book,代码行数:46,代码来源:la4128.cpp

示例2: main

int main() 
{
  int n, m;
  while(scanf("%d%d", &n, &m) == 2) 
  {
    solver.init(n);
    for(int i = 0; i < m; ++i) 
	{
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c); --a; --b;
      solver.AddEdge(a, b, c);
      solver.AddEdge(b, a, c);
    }

    solver.dijkstra(1); // 家(1)到所有点的距离。因为道路都是双向的,所以把家看作起点也行
    memset(d, -1, sizeof(d));
    printf("%d\n", dp(0)); // 办公室(0)到家的符合条件的路径条数
  }
  return 0;
}
开发者ID:lijiansong,项目名称:acm-algorithms,代码行数:20,代码来源:UVa10917-dp.cpp

示例3: main

int main()
{
    int t;
    scanf("%d",&t);
    for(int i = 1;i <= t;i ++)
    {
        memset(edges,0,sizeof(edges));
        memset(next,0,sizeof(next));
        memset(head,0,sizeof(head));
        maps.clear();
        int tott = 0;
        tot = 0;
        scanf("%d",&n);
        dijkstra.init(n);
        for(int i = 1;i <= n;i ++)
        {
            string s1;
            int ct;
            cin>>s1;
            maps[s1] = ++tott;
            scanf("%d",&ct);
            for(int i = 1;i <= ct;i ++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                build(maps[s1],a,b);
            }
        }
        int q;
        scanf("%d",&q);
        string s1,s2;
        for(int i = 1;i <= q;i ++)
        {
            cin>>s1>>s2;
            dijkstra.dijkstra(maps[s1]);
            printf("%d\n",dijkstra.d[maps[s2]]);
        }
    }
    return 0;
}
开发者ID:esun0087,项目名称:spoj,代码行数:40,代码来源:15_SHPATH.cpp

示例4: main

int main(int argc, char const *argv[])
{
	READ;
	WRITE;
	read();
	Dijkstra dijkstra;
	dijkstra.init();
	for(int i = 0 ; i < 300 ; i ++)
	{
		for(int j = 0 ; j < 300 ; j++)
		{
			if(g[i][j]!=-1)
			{
				// printf("* %c %c %d\n",i,j,g[i][j]);
				dijkstra.AddEdge(i, j, g[i][j]);
			}
		}
	}

	dijkstra.dijkstra('Z');
	return 0;
}
开发者ID:Mantis-Shrimp,项目名称:ACM,代码行数:22,代码来源:USACO_comehome.cpp

示例5: main

int main()
{
	//Get data
	Search dfs;
	dfs.populateMatrix("distanceMatrix.txt");
	

	dfs.displayKey();

	Dijkstra d;
	d.populateMatrix("distanceMatrix.txt");
	
	// Get input
	cout << "Enter the start intersection number: ";
	int begin;
	cin >> begin;
	
	cout << "Enter the end intersection number: ";
	int end;
	cin >> end;

	//Perform DFS algorithm
	dfs.DFS(begin, end);
	cout << "-------------------------" << endl;
	cout << "DFS" << endl;
	dfs.displayList();

	//Perform Dijkstra's algorithm
	d.dijkstra(begin, end);
	cout << "-------------------------" << endl;
	cout << "DIJKSTRA" << endl;
	d.displayList();

	cout << "Press Enter to Exit" << endl;
	cin.ignore(10, '\n');	
	cin.get();
	return 0;
}
开发者ID:joblolin95,项目名称:ImprovedSearchProjectC-,代码行数:38,代码来源:SearchProject.cpp


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