博客
关于我
Dijkstra算法-最短路径问题+例题
阅读量:160 次
发布时间:2019-02-28

本文共 3686 字,大约阅读时间需要 12 分钟。

Background

Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

背景

Hugo Heavy很高兴。
在Cargolifter项目破裂后,他现在可以扩展业务。
但他需要一个聪明的人告诉他,他的客户是否真的有办法将他的巨型钢制起重机建造到需要所有街道都能承受重量的地方。
幸运的是,他已经有了所有街道和桥梁以及所有允许重量的城市规划。不幸的是,他不知道如何找到最大重量能力,以告诉他的客户起重机有多重。
但你肯定知道。

Problem

You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.

问题

您将获得城市的计划,由街道(具有重量限制)描述的交叉点之间的编号从1到n。你的任务是找到从1号(Hugo的地方)到n号(客户的地方)可以运输的最大重量。您可以假设至少有一条路径。所有街道都可以双向旅行。

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

输入

第一行包含场景数量(城市规划)。对于每个城市,街道交叉口的数量n(1 <= n <= 1000)和街道的数量m在第一行给出。以下m行包含指定街道开始和结束交叉的整数三元组和允许的最大权重,它是正数且不大于1000000.每对交叉点之间最多只有一条街道。

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

输出

每个方案的输出都以包含“Scenario #i:”的行开头,其中i是从1开始的方案编号。然后打印一行,其中包含Hugo可以传输给客户的最大允许重量。使用空行终止方案的输出。

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4
个人思路:dijkstra算法是求最短路径的一种,Dijkstra算法采用的是一种贪心的策略,声明一个数组dis来保存源点到各个顶点的最短距离和一个保存已经找到了最短路径的顶点的集合:
输入流处理

for (i = 1; i <= m; i++)		{			scanf("%d%d%d", &u, &v, &w);   //依次输入节点,节点,这两个节点之间的权重			map[u][v] = map[v][u] = w;		}

调用函数:

Dijkstra(1);                    //从第几号节点开始

ac代码:

#include
#include
#include
#define N 1100#define INF 0x3f3f3fint map[N][N];int d[N];int visit[N];int n;int min(int a, int b){ if (a > b) a = b; return a;}void Dijkstra(int x){ memset(visit, 0, sizeof(visit)); int i, j; for (i = 1; i <= n; i++) { d[i] = map[x][i]; } for (i = 1; i <= n; i++) { int min_cost = -1; int index; for (j = 1; j <= n; j++) { if (visit[j] == 0 && d[j] > min_cost) { min_cost = d[j]; // printf("%d\n",d[j]); index = j; } } visit[index] = 1; // printf("%d\n\n\n",index); for (j = 1; j <= n; j++) { if (visit[j] == 0 && d[j] < min(map[index][j], min_cost)) { d[j] = min(map[index][j], min_cost); } } } printf("%d\n\n", d[n]); //输出最终节点与首节点之间的最小距离}int main(){ int i, j, m, k, l, u, v, w; int T, t = 0; scanf("%d", &T); //T个测试 while (T--) { t++; //输出例子 scanf("%d%d", &n, &m); //交叉口,和最大承重量。实际上就是节点和边的权重 memset(map, 0, sizeof(map)); //清空背包 for (i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); //依次输入节点,节点,这两个节点之间的权重 map[u][v] = map[v][u] = w; } printf("Scenario #%d:\n", t); Dijkstra(1); //从第几号节点开始 } return 0;}

转载地址:http://jztj.baihongyu.com/

你可能感兴趣的文章
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
MySQL 常见的 9 种优化方法
查看>>
MySQL 常见的开放性问题
查看>>
Mysql 常见错误
查看>>