博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hubust 1339Touring (最短路Dijkstra算法)
阅读量:4917 次
发布时间:2019-06-11

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

Description:

The best friends Mr. Li and Mr. Liu are touring in beautiful country M.

M has n cities and m two-way roads in total. Each road connects two cities with fixed length.We assume that the cost of car traveling on the road is only related to the length of road,the longer road the more money to pay. 

Now,both Mr. Li and Mr. Liu are in the city C,they have chosen to travel separately by the next time.

Mr. Li chooses city A with beautiful scenery to be next place, Mr. Liu goes to city B with ancient temples.

You are their friend with clever minds,just tell them how to arrive the target places make total costs of them minimum

 

Input:

The input file contains sevearl test cases.The first line of each case are two positive integers n,and m(3<=n<=5000, 1<=m<=10000). The cities are named from 1 to n.Three positive integers C, A, B are follwing.Then,m lines are given,each line contains three integers i,j and k,indicating one road between i and j is exists,and should pay cost k by the car.

 

Process to the end of file.

Output:

For each test case, first print a line saying "Scenario #p", where p is the number of the test case.Then,if both Mr. Li and Mr. Liu can manage to arrive their cities,output the minimum cost they will spend,otherwise output "Can not reah!", in one line.Print a blank line after each test case, even after the last one.

Sample Input:

4 5

1 3 4

1 2 100

1 3 200

1 4 300

2 3 50

2 4 100

4 6

1 3 4

1 2 100

1 3 200

1 4 300

2 3 50

2 4 100

3 4 50

Sample Output:

Scenario #1

250

Scenario #2

200

 

思路:

恩,,,大概意思说就是两个人都从C地出发,分别到达A,B两地,当两人共同走的路只用给一次车费,求最小花费···

其实想通之后觉得很好理解哒,,,就是在所有点中找到一个点,让它到A,B,C三个点的花费和最小,,,

然后怎么做捏,,,其实也很简单嘛,,,

算到两个点之间的最短路的时候,调用一次,那现在有三个点,调用三次就好啦,然后分别用lowa,lowb,lowc来记录,最后求最小的和就阔以咯···

 

代码:

#include
#include
#include
using namespace std; #define INF 0x1f1f1f1f //定义的优先队列 struct str{ int num; int cost; str(int n,int c):num(n),cost(c){} str(){} friend bool operator <(str s1,str s2){ return s1.cost>s2.cost; }//嘛嘛,,,其实一直觉得这里是很奇怪的,像这样子的友元函数实现的功能是从小到大排序···所以就记着函数体里面是“>”咯 }; struct Arc{ int next_arc; int point; int cost; }; Arc arc[20005]; int head[5005]; bool fl[5005]; int lowa[5005]; int lowb[5005]; int lowc[5005]; int c,a,b; //这个函数主要是分别求出a,b,c到每一个它所能到达的点的最短距离 void dij(int src,int n,int *low){ //src 表示的分别是a,b,c点,,, memset(fl,0,sizeof(fl)); priority_queue
q; //好吧,,,又是,第一次自己写结构体来定义优先队列; q.push(str(src,0));//加入源点a/b/c int kk=0; while(kk

转载于:https://www.cnblogs.com/pylbyurway/p/4567151.html

你可能感兴趣的文章
MSMQ的简单使用
查看>>
cocos2dx移植android平台
查看>>
回应“主流WebGIS实现的原理.矢量地图”
查看>>
笔记50 | Android自定义View(一)
查看>>
aspectj 获取 连接点 方法!
查看>>
35个seo优化技巧
查看>>
poi横纵导出
查看>>
JAVA中Comparator的使用
查看>>
使用 Cosmos DB 创建和查询 NoSQL 表
查看>>
PAT1043 Is It a Binary Search Tree
查看>>
1044 Shopping in Mars
查看>>
Django 2 数据库配置
查看>>
weka文本分类之二 批量过滤
查看>>
SCM_SVN_CVS
查看>>
设计抗混叠滤波器的三大指导原则(转载)
查看>>
join() 和 sleep() 区别
查看>>
MySQL 'localhost' (10061)解决方法
查看>>
信息安全-1:python之playfair密码算法详解[原创]
查看>>
Linq
查看>>
OC中新增的数据类型
查看>>