【HDU - 2066】:一个人的旅行(Dijkstra算法)

题干:

虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。

Input

输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个; 
 接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路) 
接着的第T+1行有S个数,表示和草儿家相连的城市; 
 接着的第T+2行有D个数,表示草儿想去地方。

Output

输出草儿能去某个喜欢的城市的最短时间。

Sample Input

6 2 3
1 3 5
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2
8 9 10

Sample Output

9

解题报告:

       把草儿的家当成下标为0的,然后在车站和草儿中间加边成边权为0的,然后跑一边DIjkstra即可。不需要多遍。。。

 

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
struct Node {int to;int w;int ne;
} e[MAX];struct point {int pos;int c;point(){}//没有此构造函数不能写  node t  这样point(int pos,int c):pos(pos),c(c){}//可以写node(pos,cost)这样bool operator <(const point & b) const {return c>b.c;}
};int head[MAX];
int vis[MAX];
int cnt = 0;
int maze[1005][1005];
int n,m;
int dis[MAX];//表示从出发点开始到该点的最短距离。 
void add(int u,int v,int w) {e[cnt].to = v;e[cnt].w = w;e[cnt].ne = head[u];head[u] = cnt;cnt++;
}
void Dijkstra(int u,int v) {priority_queue<point> pq; dis[u] = 0;point cur = point(u,0);pq.push(cur);
//	vis[u] = 1;while(!pq.empty()) {point now = pq.top();pq.pop();
//		if(vis[now.pos] == 1) continue;vis[now.pos] = 1;for(int i = head[now.pos]; i!=-1; i=e[i].ne) {if(  dis[e[i].to] > dis[now.pos] + e[i].w ) {dis[e[i].to] = dis[now.pos] + e[i].w;pq.push(point(e[i].to,dis[e[i].to] ) );	}} }
} 
void init() {cnt = 0;memset(head,-1,sizeof(head) );memset(dis,INF,sizeof(dis) ) ;memset(maze,INF,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;
}
int main()
{//	freopen("in.txt","r",stdin);int a,b,w;int t,s,d;int ed[10000];while(~scanf("%d%d%d",&t,&s,&d) ) {init();for(int i = 1; i<=t; i++) {scanf("%d%d%d",&a,&b,&w);if(w<maze[a][b]) {maze[a][b] = maze[b][a] = w;add(a,b,w);add(b,a,w);}}for(int i = 1; i<=s; i++) {scanf("%d",&a);maze[a][0] = maze[0][a] = 0;add(a,0,0);add(0,a,0);}for(int i = 0; i<d; i++) {scanf("%d",&ed[i]);}Dijkstra(0,0); int minn = INF;for(int i = 0; i<d; i++) {minn = min(minn,dis[ed[i]]);}printf("%d\n",minn);}return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441957.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

for相关 java_Java学习之for循环相关知识梳理

for循环是编程语言中一种循环语句&#xff0c;是Java程序员日常工作中的重要组成部分。循环语句由循环体及循环的判定条件两部分组成&#xff0c;其表达式为&#xff1a;for(单次表达式;条件表达式;末尾循环体){中间循环体&#xff1b;}。拉勾IT课小编为大家分析如何使用这一属…

【HDU - 3790】最短路径问题(DIjkstra算法 双权值)

题干&#xff1a; 给你n个点&#xff0c;m条无向边&#xff0c;每条边都有长度d和花费p&#xff0c;给你起点s终点t&#xff0c;要求输出起点到终点的最短距离及其花费&#xff0c;如果最短距离有多条路线&#xff0c;则输出花费最少的。 Input 输入n,m&#xff0c;点的编号…

【POJ - 3037】Skiing (Dijkstra算法)

题干&#xff1a; Bessie and the rest of Farmer Johns cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left corner of an R (1 < R < 100) by C (1 < C < 100) grid of elevations E (-25 < E < 25). In or…

java web权限设计数据权限范围_JavaWeb 角色权限控制——数据库设计

相信各位读者对于角色权限管理这个需求并不陌生。那么是怎么实现的呢&#xff1f;今天小编来说道说道&#xff01;1、首先我们来进行数据库的设计&#xff0c;如何设计数据库是实现权限控制的关键&#xff1a;1)用户表&#xff1a;id&#xff1a;主键、自增、intname&#xff1…

modbus与硬件对接Java_java中modbus协议连接

modbus在java中的使用&#xff0c;首先maven的pom中引入modbus4j包com.infiniteautomationmodbus4j3.0.32. 我们创建类&#xff1a;ModBus4JTCPClient&#xff0c;创建ModbusMaster连接对象&#xff0c;以及读取寄存器方法package io.powerx.test;import org.apache.commons.la…

【51Nod-1100】 斜率最大(贪心)☆双排序

题干&#xff1a; 平面上有N个点&#xff0c;任意2个点确定一条直线&#xff0c;求出所有这些直线中&#xff0c;斜率最大的那条直线所通过的两个点。 &#xff08;点的编号为1-N&#xff0c;如果有多条直线斜率相等&#xff0c;则输出所有结果&#xff0c;按照点的X轴坐标排…

【HDU - 3714 】Error Curves (三分)

题干&#xff1a; Josephina is a clever girl and addicted to Machine Learning recently. She pays much attention to a method called Linear Discriminant Analysis, which has many interesting properties. In order to test the algorithms efficiency, she colle…

java中JLabel添加监听事件_[求助]关于JLabel添加监听器的问题。请各位帮忙!!

[求助]关于JLabel添加监听器的问题。请各位帮忙&#xff01;&#xff01;如图&#xff0c;我想在左边的JLabel上添加事件监听器&#xff0c;然后再去右边的JPane上进行绘制图形&#xff0c;请问这个事件监听器改怎么加&#xff0c;好象不能加ActionListener&#xff0c;要加什么…

指数循环节证明

还有关键的一步忘写了phi(m)>r的注意因为ma^r*m‘’所以phi(m)>phi(a^r)>r,所以就相当于phi(m)为循环节&#xff0c;不过如果指数小于phi(m)只能直接算了。。 注意这里的m与a^r是互质的上面忘写了。。 转自https://blog.csdn.net/guoshiyuan484/article/details/787…

java语言中的类可以_java 语言中的类

类一、类类是具有相同性质的一类事物的总称, 它是一个抽象的概念。它封装了一类对象的状态和方法, 是创建对象的模板。类的实现包括两部分: 类声明和类体类的声明类声明的基本格式为:[ 访问权限修饰符]c l a s s类名[extends超类][ implments实现的接口列表]{}说 明:① 访问权限…

【HDU - 1546】 Idiomatic Phrases Game(Dijkstra,可选map处理字符串)

题干&#xff1a; Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters and has a certain meaning. This game will give Tom two idioms. He should build a list of idioms and the list starts and ends with the two…

Java迭代器修改链表_Java恼人的迭代器不会返回链表中的元素

给出以下代码&#xff1a;public void insertIntoQueue(float length,int xElement,int yElement,int whichElement){Dot dot new Dot(xElement,yElement);GeometricElement element null;// some codeint robotX,robotY;boolean flag false;for (Iterator i robotList.ite…

(精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目

POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj…

java输入正确的信息_判断用户输入的信息是否正确

package com.Embed.util;import java.sql.Connection;import java.sql.DriverManager;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Common {// 判断用户输入的时间格式是否正确publ…

【UVA - 10815】 Andy's First Dictionary(STL+字符处理)

题干&#xff1a; Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. F…

java里dir是什么意思_关于文件系统:为什么user.dir系统属性在Java中工作?

我读过的几乎每篇文章都告诉我&#xff0c;你不能用Java创建chdir。 这个问题的公认答案说你不能用Java做到这一点。但是&#xff0c;这里有一些我尝试过的东西&#xff1a;geocodebox:~$ java -versionjava version"1.6.0_14"Java(TM) SE Runtime Environment (buil…

【Uva - 10935】 Throwing cards away I (既然是I,看来还有Ⅱ、Ⅲ、Ⅳ?)(站队问题队列问题)

题干&#xff1a; Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move the card that …

腾讯 tars java_腾讯TARS开源团队郑苏波:腾讯微服务开发框架的源码剖析

郑苏波&#xff1a;大家下午好&#xff01;我是腾讯微服务的郑苏波。先做一个框架的介绍&#xff0c;Tars是一个支持多语言内嵌服务治理功能的框槛&#xff0c;能跟DevOps比较好的协同开发。这个框架四大特点&#xff1a;一是对用户透明实现&#xff0c;让业务可以聚焦自己的逻…

【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)

题干&#xff1a; An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a path in the graph where every node is either on this path or a neighbor of a node on the path. This path is called the spine of the caterpillar …

*【POJ - 1860】Currency Exchange (单源最长路---Bellman_Ford算法判正环)

题干&#xff1a; Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specia…