find the mincost route

2024-02-03 23:08
文章标签 find route mincost

本文主要是介绍find the mincost route,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


 find the mincost route

Crawling in process... Crawling failed      Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u  

    

Description

杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。

Input

第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。

Output

对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It's impossible.".
 

Sample Input

     
3 3 1 2 1 2 3 1 1 3 1 3 3 1 2 1 1 2 3 2 3 1
 

Sample Output

     
3 It's impossible.


对于i到j的最短路径有两种形式,①经过k,最短路径就是i到k加上k到j的距离;②不过k,最短路径就是i 到j的距离。

求无向图最小环,枚举最大环的连接点,更新环,用dist[i][j]表示i到j的最短路径,当前最小环的权用minn表示,则对于每一个接点k (1到k - 1中间结点的最短路径都已经确定), 有minn = ( dist[i][j] + maze[j][k] + maze[k][i] , minn ),即环的权 = i到j的最短路径(1 < i,j <= k - 1) + jk边 + ki边。


代码:

#include"cstdio"
#include"cstring"
#include"iostream"
#include"algorithm"using namespace std;#define INF 100000000int n,m;
int maze[105][105];
int dist[105][105];void initial()
{for(int i = 0;i < 105;i++){for(int j = 0;j < 105;j++){maze[i][j] = INF;dist[i][j] = INF;}}
}void Floyd()
{int minn = INF;for(int k = 1;k <= n;k++)  //对于每一个k值,1到k - 1中间结点的最短路径都已经确定{for(int i = 1;i < k;i++){for(int j = i + 1;j < k;j++){minn = min(dist[i][j] + maze[j][k] + maze[k][i],minn); //更新环的权值}}for(int i = 1;i <= n;i++){for(int j = 1;j <= n;j++){dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]); //保存i到j之间最短路径}}}if(minn == INF){printf("It's impossible.\n");}else{printf("%d\n",minn);}
}int main()
{while(~scanf("%d%d",&n,&m)){initial();int a,b,c;while(m--){scanf("%d%d%d",&a,&b,&c);if(a != b && maze[a][b] > c){maze[a][b] = c;maze[b][a] = c;dist[a][b] = c;dist[b][a] = c;}}Floyd();}return 0;
}


这篇关于find the mincost route的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/675698

相关文章

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:

Linux 中的 find 命令介绍以及使用

文章目录 Linux 中的 `find` 命令详解及使用示例`find` 命令的基本语法常用的 `find` 命令选项按文件名搜索:`-name`按文件类型搜索:`-type`按文件大小搜索:`-size`按修改时间搜索:`-mtime`按权限搜索:`-perm`按所有者搜索:`-user` 和 `-group` `find` 命令的常见操作删除找到的文件:`-exec` 和 `rm`查找并

mysql中find_in_set()函数

1.场景 假设有一个user用户表,表字段分别为:id(主键),name(姓名),age(年龄),hobby(爱好)。而一个人可能有好几个爱好,游泳啊篮球啊乒乓球啊等等。数据库里hobby字段存的是:游泳,篮球,乒乓球 而要想查所有喜欢游泳的人,就可以用find_in_set函数了 todo:贴图 2.使用 select *from user where find_in_set("游泳"

Failed to find style 'vpiCirclePageIndicatorStyle' in current theme

使用 ViewPagerIndicator 时 , 布局文件报的错 Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style refe

[LeetCode] 438. Find All Anagrams in a String

题:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ 题目 Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowerca

最佳优先搜索best-find search

目录 1. 问题 2. 算法 3.代码 1. 问题 考虑下面这个问题:  我们要找到从Arad到Bucharest的路,最好是最短的路: 2. 算法 这是一个无向有环图, 可以采用最佳优先搜索: 最佳优先搜索的算法可以参考维基百科: 伪代码如下: // Pseudocode for Best First SearchBest-First-Search(Gr