博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2680 Choose the best route Dijkstra 虚拟点
阅读量:4349 次
发布时间:2019-06-07

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

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3608    Accepted Submission(s): 1143

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
1
 

 

Sample Output
1
-1
 

由于他给了多个起点,却只有一个重点,所以可以设置虚拟的0为起点,0与给出的起点之间的距离为0;
代码:
View Code
1 #include
2 #include
3 #define inf 100000000 4 int map[1010][1010]; //注意这道题是单向边 5 int d[1010]; 6 int s[1010]; 7 int n,m,ss; //ss是终点 8 int dij(int v) 9 {10 int i,j,min,pos;11 for(i=0;i<=n;i++)12 {13 s[i]=0;14 d[i]=map[v][i];15 }16 s[v]=1;17 d[v]=0;18 for(i=1;i<=n;i++) //处理剩余的n 个点 19 {20 min=inf;21 for(j=1;j<=n;j++)22 {23 if(!s[j]&&min>d[j])24 {25 pos=j;26 min=d[j];27 }28 }29 if(pos==ss||min==inf) break; 30 s[pos]=1;31 for(j=1;j<=n;j++)32 {33 if(!s[j]&&d[j]>(d[pos]+map[pos][j]))34 d[j]=d[pos]+map[pos][j];35 }36 }37 return d[ss];38 }39 int main()40 {41 int i,j,k;42 int p,q,t;43 int w,qi; //qi代表起点44 while(scanf("%d%d%d",&n,&m,&ss)!=EOF)45 {46 for(i=0;i<=n;i++)47 for(j=0;j<=n;j++)48 map[i][j]=inf; 49 for(i=1;i<=m;i++)50 {51 scanf("%d%d%d",&p,&q,&t);52 if(map[p][q]>t) 53 map[p][q]=t; /54 }55 scanf("%d",&w);56 for(i=1;i<=w;i++)57 {58 scanf("%d",&qi);59 map[0][qi]=0; // 设置虚拟的点0 ,O到起点的距离为0,这样就直接一遍dijkstra就行了。60 }61 k=dij(0);62 if(k==inf) printf("-1\n");63 else64 printf("%d\n",k);65 }66 return 0;67 }

 

转载于:https://www.cnblogs.com/shenshuyang/archive/2012/08/04/2622705.html

你可能感兴趣的文章
全景图制作详解
查看>>
React之todo-list
查看>>
cocoapods降级版本
查看>>
MYSQL复习笔记4-基本SQL语句
查看>>
C#&java重学笔记(函数)
查看>>
14软件G2班
查看>>
bzoj 1977 [BeiJing2010组队]次小生成树 Tree
查看>>
bzoj 2119 股市的预测——枚举长度的关键点+后缀数组
查看>>
maven:新建的maven工程需要添加一下插件
查看>>
关于iOS自定义控件:在view上实现事件和代理
查看>>
[扫描线]POJ2932 Coneology
查看>>
全局变量与全局静态变量的区别
查看>>
EMC队列 发件人为空 From Address: <>
查看>>
多路复用IO模型 IO multiplexing
查看>>
蒙蒙的Git
查看>>
js方法遇到就记录
查看>>
iReport采用JDBC的方式连接Oracle
查看>>
AOP中的相关概念
查看>>
监控系统信息模块psutil
查看>>
python tokenizer
查看>>