HDU - 2680 -- Choose the best route 【Dijkstra+最优路径选取+有向图】
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
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
题意:
小明想要去朋友家玩,因为晕车他想要尽快的到达朋友家,他的朋友家在 End 车站附近,而小明的家附近有不止一个车站。那么问题来了,为了尽快的到达
朋友家里,小明可以走的最短路径是多少
思路:
首先明确该图是个有向图。
他需要在给定的几个出发点出发,到给定的点结束,那么可以逆向思维。只要找出从终点出发,记录终点到其他点的最短路,然后遍历最短路数组,找出所给的
这些起始点中,到终点最短的一条输出即可。。。。。
【开始只死板硬套。竟然循环调用 Dijkstra】TLE 。。。兼职智障
代码:
#include <cstdio> #include <vector> #include <queue> #include <cstring> #define mem(a,b) memset(a,b,sizeof(b)) #define INF 0x3f3f3f3f #define MAX 1010 using namespace std; typedef pair<int ,int> Pair; struct Node{ int to,cost; }; int Vex,Edge,End; vector<Node> graph[MAX]; int dis[MAX];
int Dijkstra(int x) { priority_queue<Pair,vector<Pair> ,greater<Pair> > que; fill(dis,dis+Vex+2,INF); dis[x] = 0; que.push(Pair(0,x)); while(!que.empty()){ Pair p = que.top(); que.pop(); int v = p.second; if(dis[v] < p.first) continue; for(int i = 0;i < graph[v].size();i++){ Node node = graph[v][i]; if(dis[node.to] > dis[v]+node.cost){ dis[node.to] = dis[v] + node.cost; que.push(Pair(dis[node.to],node.to)); } } } return dis[End]; } int main() { int from,to,value,w; Node tmp_node; while(~scanf("%d%d%d",&Vex,&Edge,&End)){ for(int i = 0;i <= Vex;i++)graph[i].clear(); for(int i = 0;i < Edge;i++){ scanf("%d%d%d",&from,&to,&value); tmp_node.cost = value; tmp_node.to = from; graph[to].push_back(tmp_node); } scanf("%d",&w); int ansans = INF; Dijkstra(End); int start; for(int i = 0;i < w;i++){ scanf("%d",&start); ansans = min(ansans,dis[start]); } if(ansans == INF)printf("-1\n"); else printf("%d\n",ansans); } return 0; }

浙公网安备 33010602011771号