一个人的旅行

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50701    Accepted Submission(s): 16857

Problem Description
虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^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

思路:最短路的题目,用floyd 会超时的,用dijkstra就可以,只需要把家看作0,并且将相邻的那些火车站的cost边权看为0就好了(cost[0][u]=0)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = 1e3+;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
int cost[maxn][maxn];
int d[maxn];
bool used[maxn];
int T,n;
void dijk(int s)
{
fill(d,d+,INF);
fill(used,used+,false);
d[s]=; while(true)
{
int v=-;
for(int u=;u<=n;u++)
{
if(!used[u]&&(v==-||d[u]<d[v]))
v=u;
}
if(v==-)
break;
used[v]=true;
for(int u=;u<=n;u++)
d[u]=min(d[u],d[v]+cost[v][u]);
}
}
int main()
{
int S,D;
while(cin>>T>>S>>D)
{
memset(cost,INF,sizeof(cost));
for(int i=;i<;i++)
cost[i][i]=;
memset(d,INF,sizeof(d));
n=;
while(T--)
{
int a,b,l;
cin>>a>>b>>l;
n=max(max(a,b),n);
if(l<cost[a][b])
{
cost[a][b]=l;
cost[b][a]=l;
}
}
while(S--)
{
int a;
cin>>a;
cost[][a]=;
cost[a][]=;
}
dijk();
int ans=INF;
for(int i=;i<D;i++)
{
int end;
cin>>end;
ans=min(ans,d[end]);
}
cout<<ans<<endl;
}
}

最新文章

  1. spring3.0使用annotation完全代替XML(续)
  2. C#和C实现通过CRC-16 (Modbus)获取CRC值并校验数据(代码)
  3. C# 定时器运用
  4. js_实用
  5. RadASM中文乱码问题解决方法!!
  6. ListView usage in ERP-DEV
  7. 连接SQLServer时提示“但是在登录前的握手期间发生错误。 (provider: SSL Provider, error: 0 - 等待的操作过时”解决办法
  8. 动态 SQL
  9. How to detect and avoid memory and resources leaks in .NET applications
  10. WordPress下载安装简单配置实例
  11. Java 之 HTML
  12. python 脚本在linux环境下运行
  13. pycharm安装和首次使用
  14. Ubuntu16.04更新源
  15. cookie和session的讲解
  16. Pandas 处理丢失数据
  17. c#获取网页代码、数据、资源
  18. dfs找負環
  19. ①Jenkins集成—入门安装使用
  20. 深入了解java虚拟机(JVM) 第十二章 类加载器

热门文章

  1. Golang: runnerw.exe: CreateProcess failed with error 216 (no message available)
  2. 16.Ubuntu LTS 16.04安装搜狗输入法全过程记录(纯新手)
  3. AGC015 C Nuske vs Phantom Thnook(前缀和)
  4. html 02-认识html
  5. javascript之常用正则表达式
  6. linux书籍推荐
  7. 初学基础python记录
  8. linux 命令——15 tail (转)
  9. codeforce Gym 100425E The Street Escalator(期望,线性递推)
  10. Centos 编译安装bind错误