HDU2680 Choose the best route (最短路)(Dijkstra算法)

xiaoxiao2021-02-28  15

Choose the best route

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

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

 

 

   题意:给出n,m,s,代表有n个车站,m条路线,s为终点,然后m行代表路线和花费,注意是单向边,最后给出k个数代表能选择      的出发点。其实就是多个源头的Dijkstea算法,但是每次都D,1000次应该会超时,所以,可以考虑把所有的起点连接到一个共同的起点,假设为0点,这样的话0到这些起点的距离是0,这样就可以改成0到任何点的最短路了,只要想明白这一点就好做了。

#include<stdio.h> #include<iostream> #include <algorithm> #include<string.h> #include<vector> #include<math.h> #include<queue> #include<set> #define LL long long #define INF 0x3f3f3f3f using namespace std; int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1) return KGCD(a, b>>1); if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);} int LCM(int a,int b){ return a/KGCD(a,b)*b; } int dir[5][2]={0,1,0,-1,1,0,-1,0}; using namespace std; int n,m; int map[1005][1005];//地图 int map1[1005];//单元最短路 int vis[1005];//记录访问 void dijkstra() { int k; memset(vis,0,sizeof(vis)); vis[0]=1;//起点标记下 for(int i=0;i<n;i++)//每个点作为起点搜索一边所有的最短路 { int min=INF; k=1; for(int j=0;j<=n;j++) { if(!vis[j] && min > map1[j]) { min=map1[j]; k=j; } } vis[k]=1; for(int j=0;j<=n;j++) { if(!vis[j] && map1[j] > map1[k] + map[k][j]) map1[j]=map1[k]+map[k][j]; } } } int main() { int x,y,z,t; while(scanf("%d%d%d",&n,&m,&t)!=EOF) { for(int i=0;i<=n;i++)//先整理出来地图 { for(int j=0;j<=n;j++) map[i][j]=INF; } for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); if(z<map[x][y]) //避免重遍 只要最小的 map[x][y]=z; } scanf("%d",&y); for(int i=0;i<y;i++)//构造出来共同起点 { scanf("%d",&x); map[0][x]=0; } for(int i=0;i<=n;i++) { map1[i]=map[0][i]; } dijkstra(); if(map1[t]==INF)//如果都没办法到达那个点 printf("-1\n"); else printf("%d\n",map1[t]); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-1400286.html

最新回复(0)