博客
关于我
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/

你可能感兴趣的文章
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>