当前位置: 首页 > article >正文

代码随想录训练营第66天|Floyd

97. 小明逛公园

#include <iostream>
#include <vector>
#include <list>
using namespace std;

int main() {
    int n, m, p1, p2, val;
    cin >> n >> m;

    vector<vector<vector<int>>> grid(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 10005)));  // 因为边的最大距离是10^4
    for(int i = 0; i < m; i++){
        cin >> p1 >> p2 >> val;
        grid[p1][p2][0] = val;
        grid[p2][p1][0] = val; // 注意这里是双向图

    }
    // 开始 floyd
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                grid[i][j][k] = min(grid[i][j][k-1], grid[i][k][k-1] + grid[k][j][k-1]);
            }
        }
    }
    // 输出结果
    int z, start, end;
    cin >> z;
    while (z--) {
        cin >> start >> end;
        if (grid[start][end][n] == 10005) cout << -1 << endl;
        else cout << grid[start][end][n] << endl;
    }
}

127. 骑士的攻击

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int moves[1001][1001];
int dir[8][2]={-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2};
int b1, b2;
// F = G + H
// G = 从起点到该节点路径消耗
// H = 该节点到终点的预估消耗

struct Knight{
    int x,y;
    int g,h,f;
    bool operator < (const Knight & k) const{  // 重载运算符, 从小到大排序
     return k.f < f;
    }
};

priority_queue<Knight> que;

int Heuristic(const Knight& k) { // 欧拉距离
    return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2); // 统一不开根号,这样可以提高精度
}
void astar(const Knight& k)
{
    Knight cur, next;
	que.push(k);
	while(!que.empty())
	{
		cur=que.top(); que.pop();
		if(cur.x == b1 && cur.y == b2)
		break;
		for(int i = 0; i < 8; i++)
		{
			next.x = cur.x + dir[i][0];
			next.y = cur.y + dir[i][1];
			if(next.x < 1 || next.x > 1000 || next.y < 1 || next.y > 1000)
			continue;
			if(!moves[next.x][next.y])
			{
				moves[next.x][next.y] = moves[cur.x][cur.y] + 1;

                // 开始计算F
				next.g = cur.g + 5; // 统一不开根号,这样可以提高精度,马走日,1 * 1 + 2 * 2 = 5
                next.h = Heuristic(next);
                next.f = next.g + next.h;
                que.push(next);
			}
		}
	}
}

int main()
{
    int n, a1, a2;
    cin >> n;
    while (n--) {
        cin >> a1 >> a2 >> b1 >> b2;
        memset(moves,0,sizeof(moves));
        Knight start;
        start.x = a1;
        start.y = a2;
        start.g = 0;
        start.h = Heuristic(start);
        start.f = start.g + start.h;
		astar(start);
        while(!que.empty()) que.pop(); // 队列清空
		cout << moves[b1][b2] << endl;
	}
	return 0;
}

小结:

如果遇到单源且边为正数,直接Dijkstra

如果遇到单源边可为负数,直接 Bellman-Ford

如果有负权回路,优先 Bellman-Ford, 如果是有限节点最短路 也优先 Bellman-Ford

如果是遇到多源点求最短路,直接 Floyd


http://www.kler.cn/news/361245.html

相关文章:

  • UNIAPP弹窗跳转页面无法滚动bug
  • 通过梧桐数据库分析客户价值
  • Unable to open nested entry ‘********.jar‘ 问题解决
  • taro底部导航,Tabbar
  • 信发软件之展示excel文档——未来之窗行业应用跨平台架构
  • 得物App3D创新应用引关注,世界设计之都大会启幕
  • Golang笔记_day09
  • 【云从】十、常见安全问题与云计算的计费模式
  • WebForms Hashtable
  • 大模拟训练计划
  • tsconfig.json 内容解读
  • 解决“程序包com.alibaba.fastjson不存在”的错误 (导入瑞吉外卖项目)
  • 深入解析 Go 语言接口:多接口实现与接口组合的实际应用
  • 在 Vue 3 中实现电子签名组件
  • C语言初阶小练习4(不用临时变量交换数值)
  • Ubuntu(22.04)本地部署Appsmith
  • Flink Taskmanager 内存模型详解
  • 大数据新视界 --大数据大厂之大数据与区块链双链驱动:构建可信数据生态
  • Android EditText调起键盘,阻止Recyclerview调整大小方法
  • 【Python】Playwright:环境配置与自动生成代码