HDU 1548 A strange lift (Dijkstra算法)
来源:互联网 发布:数据科学家养成手册 编辑:程序博客网 时间:2024/05/21 10:48
Choose the best route
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 62 Accepted Submission(s) : 27
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211
Sample Output
1-1
这道题目WA了很多次,原因就是没用将dist[起点]=0这句话写进去,也就是起点等于终点的情况没用考虑。
下面是AC的代码:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int map[201][201];
int dist[201];
int visited[201];
int m,n,sum,flag;
const int inf=9999999999;
void Dijkstra(int st)
{
memset(visited,0,sizeof(visited));
visited[st]=1;
for(int i=1;i<=n;i++)
dist[i]=map[st][i];
dist[st]=0;
for(int i=1;i<n;i++)
{
int min=inf;
int pos;
for(int j=1;j<=n;j++)
{
if(!visited[j]&&dist[j]<min)
{
min=dist[j];
pos=j;
}
}
if(min==inf) break;
visited[pos]=1;
for(int j=1;j<=n;j++)
{
if(!visited[j]&&dist[j]>map[pos][j]+dist[pos])
dist[j]=map[pos][j]+dist[pos];
}
}
}
int main()
{
while(~scanf("%d",&n),n)
{
int st,end;
scanf("%d%d",&st,&end);
int k[201];
for(int i = 1; i<=n; i++)
for(int j = 1; j<=n; j++)
map[i][j] = inf;
for(int i=1;i<=n;i++)
cin>>k[i];
for(int i=1;i<=n;i++)
{
if(i-k[i]>0)
{
map[i][i-k[i]]=1;
}
if(i+k[i]<=n)
{
map[i][i+k[i]]=1;
}
}
Dijkstra(st);
if(dist[end]<inf) cout<<dist[end]<<endl;
else cout<<"-1"<<endl;
}
return 0;
}
#include<cstdio>
#include<string.h>
using namespace std;
int map[201][201];
int dist[201];
int visited[201];
int m,n,sum,flag;
const int inf=9999999999;
void Dijkstra(int st)
{
memset(visited,0,sizeof(visited));
visited[st]=1;
for(int i=1;i<=n;i++)
dist[i]=map[st][i];
dist[st]=0;
for(int i=1;i<n;i++)
{
int min=inf;
int pos;
for(int j=1;j<=n;j++)
{
if(!visited[j]&&dist[j]<min)
{
min=dist[j];
pos=j;
}
}
if(min==inf) break;
visited[pos]=1;
for(int j=1;j<=n;j++)
{
if(!visited[j]&&dist[j]>map[pos][j]+dist[pos])
dist[j]=map[pos][j]+dist[pos];
}
}
}
int main()
{
while(~scanf("%d",&n),n)
{
int st,end;
scanf("%d%d",&st,&end);
int k[201];
for(int i = 1; i<=n; i++)
for(int j = 1; j<=n; j++)
map[i][j] = inf;
for(int i=1;i<=n;i++)
cin>>k[i];
for(int i=1;i<=n;i++)
{
if(i-k[i]>0)
{
map[i][i-k[i]]=1;
}
if(i+k[i]<=n)
{
map[i][i+k[i]]=1;
}
}
Dijkstra(st);
if(dist[end]<inf) cout<<dist[end]<<endl;
else cout<<"-1"<<endl;
}
return 0;
}
0 0
- hdu 1548 A strange lift (dijkstra算法)
- HDU 1548 A strange lift (Dijkstra算法)
- hdu 1548 A strange lift Dijkstra+SPFA算法AC
- 【BFS/Dijkstra】hdu 1548 A Strange Lift
- hdu 1548 A strange lift (BFS、Dijkstra)
- 【Dijkstra】-HDU-1548-A strange lift
- HDU 1548 A strange lift(Dijkstra)
- HDU - 1548 A strange lift(Dijkstra)
- 关于Dijkstra算法的一点总结(附题:HDU-1548 A strange lift)
- hdu 1548-A strange lift-最短路-dijkstra
- [HDU 1548]A Strange Lift[Dijkstra最短路]
- HDU 1548 A strange lift(Dijkstra、BFS、DP)
- hdu 1548 A strange lift(Dijkstra+合理转换)
- HDU 1548 A strange lift(构造+最短路Dijkstra)
- A strange lift hdu1548 Dijkstra算法
- A strange lift hdu 1548
- HDU 1548 A strange lift
- HDU 1548 A strange lift
- Android瀑布流实例
- Maven学习笔记(2) --- 依赖
- Android——ListView布局+适配器(三)
- 教你一波Lucas(卢卡斯)定理在数论解题中的应用
- hdu 5798 Stabilization(2016多校第六场1006)
- HDU 1548 A strange lift (Dijkstra算法)
- Java 基本数据类型
- 如何将文件夹打包成文件,然后将文件格式化成虚拟文件系统,挂载到某个文件夹
- 缓存文件可以放在哪里?它们各自的特点是什么?
- 如何在Spark中记录日志
- 单例多例的选择
- android json解析及简单例子
- spring中Bean的生命周期总结
- Excel导入性能优化