博客
关于我
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 insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>