HDU-2680,Choose the best route(Dijkstra)

news/2024/11/23 3:24:05/

Problem Description:

One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n. 

Input: 

There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations. 

Output: 

The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”. 

Sample Input: 

5 8 5

1 2 2

1 5 3

1 3 4

2 4 7

2 5 6

2 3 5

3 5 1

4 5 1

2

2 3

4 3 4

1 2 3

1 3 4

2 3 2

1

Sample Output: 

1

-1 

程序代码: 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f
int n,m,s;
int map[1001][1001],book[1001],dis[1001];
void Dijkstra(int v)
{int i,j,k,minn,u;memset(book,0,sizeof(book));book[v]=1;for(i=0;i<=n;i++)dis[i]=map[v][i];for(i=0;i<n;i++){minn=INF;for(j=0;j<=n;j++){if(book[j]==0&&dis[j]<minn){u=j;minn=dis[j];}}book[u]=1;for(k=0;k<=n;k++){if(map[u][k]<INF){if(dis[k]>dis[u]+map[u][k])dis[k]=dis[u]+map[u][k];}}}
}
int main()
{int i,j,x,y,t,a,b;while(~scanf("%d %d %d",&n,&m,&s)){for(i=0;i<=n;i++){for(j=0;j<=n;j++){if(i==j)map[i][j]=0;elsemap[i][j]=INF;}}while(m--){scanf("%d %d %d",&x,&y,&t);if(map[x][y]>t)map[x][y]=t;}scanf("%d",&a);while(a--){scanf("%d",&b);map[0][b]=map[b][0]=0;}Dijkstra(0);if(dis[s]==INF)printf("-1\n");elseprintf("%d\n",dis[s]);}return 0;
}

 


http://www.ppmy.cn/news/163477.html

相关文章

微软CVE-2023-28252漏洞

微软内核提权0day漏洞 4月12号&#xff0c;微软发布了最新的漏洞补丁程序&#xff0c;此次公告中的内核提权漏洞CVE-2023-28252&#xff0c;由安恒信息、卡巴斯基及Mandiant公司同时捕获&#xff0c;这也是安恒信息成功捕获的第4枚在野内核提权0day&#xff01; 据卫兵实验室…

服务器型号E52680,e5 2680v3 云服务器

e5 2680v3 云服务器 内容精选 换一换 将云服务器加入云服务器组。添加成功后&#xff0c;该云服务器与云服务器组中的其他成员尽量分散地创建在不同主机上。仅支持添加虚拟化类型为KVM的弹性云服务器。当前只支持反亲和性策略&#xff0c;即同一云服务器组中的弹性云服务器分散…

百练_2680:化验诊断

描述 下表是进行血常规检验的正常值参考范围&#xff0c;及化验值异常的临床意义&#xff1a; 给定一张化验单&#xff0c;判断其所有指标是否正常&#xff0c;如果不正常&#xff0c;统计有几项不正常。化验单上的值必须严格落在正常参考值范围内&#xff0c;才算是正常。正常…

前端开发 统一代码风格

在前端开发中&#xff0c;我们通常需要保证代码风格的一致性&#xff0c;而 husky 是一个可以帮助我们在 Git 提交时运行指定脚本的工具&#xff0c;由此我们可以通过 husky 来自动运行代码格式化工具&#xff0c;保证代码风格的统一。 下面是使用 husky 实现前端代码提交格式…

Hdu-2680 Choose the best route

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2680 题目大意&#xff1a; 给你一个有向图&#xff0c;一个起点集合&#xff0c;一个终点&#xff0c;求最短路。。。。 解题思路&#xff1a; 1.自己多加一个超级源点&#xff0c;把起点集合连接到超级源…

20核服务器项目,详细解答E5-2680v2,20核40线程服务器的具体用途怎么体现出来

由我锐讯网络李明辉给你详细解答E5-2680v2&#xff0c;20核40线程服务器的具体用途怎么体现出来. E5-2680v2 20核40线程服务器 一&#xff0e;配置: CPU: E5-2680v2*2 20核40线程 主频: 2.80 GHz,睿频: 3.60 GHz 20核心40线程 内存: 32G(默认配置) 最大128G 硬盘:SATA 1T(…

2678v3支持内存频率_E52680V2与E52678V3的区别

1、架构不同 E5 2667 V3是Haswell架构&#xff0c;E5 2667 V2是Ivy Bridge EP架构&#xff0c;而Haswell结构相比于Ivy Bridge结构在GPU性能、显示核心以及功耗上都更优秀先进。 选E52680V2还是E52678V3这些点很重要 看过你就懂了 https://list.jd.com/list.html? GPU haswell…

【渝粤题库】国家开放大学2021春2680煤矿机电设备概论题目

试卷代号&#xff1a;2680 2021年春季学期期末统一考试 煤矿机电设备概论 试题 2021年7月 一、单项选择题&#xff08;本题型共15题&#xff0c;每题3分&#xff0c;共45分。以下各题每题只有一个正确答案&#xff0c;将正确答案的代号填入题中的括号内&#xff09; 1.使掘进机…