博客
关于我
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 sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql sysbench测试安装及命令
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
MySQL Troubleshoting:Waiting on query cache mutex
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
MySQL Workbench 数据库建模详解:从设计到实践
查看>>
MySQL Workbench 数据建模全解析:从基础到实践
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>