Choose the best route HDU - 2680 (还是用vector存图哒~ Dijkstra 最短路)

本文介绍了一种解决公交路线最短路径问题的方法,通过添加虚拟起点并利用Dijkstra算法来寻找从多个可能的起点到单一终点的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

老坑坑的一题 , 还以为是无向图呢,然后是有向图 , 而且俩点之间有多条路径,虽然对vector没有影响,因为在松弛操作过程中会把长的边略去。但是!!!!我不知道啊 ~~

V[k].push_back(1) 指的是V[k]下标为0处存1

思路一:因为有多个起点可以选择,但只有一个终点,所以自己可以再加上一个点作为起点,编号为0,这个点和题中给的那些起点之间的距离为0。这样题目就转化为了求单源最短路径问题。

思路二:反向图。把终点作为反向图的起点,题中的起点作为要到达的终点,每条路的起点和终点交换。然后用Dijkstra求单源最短路,记录最小值即可。

代码:

#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
#include <vector>
#define INF 0x3f3f3f3f
const int maxn = 20008;
int vis[maxn] , dis[maxn] , want[maxn] , near[maxn];
int n , m , s , u , minn ;
struct node
{
	int to;
	int cost;
	node(int _to , int _cost):to(_to) , cost(_cost){}
};
vector<node>V[maxn];
void Dijkstra()
{
	for(int i = 0 ; i <= n-1 ; i++)
	{
		minn = INF;
		for(int j = 0 ; j <= n ; j++)
		{
			if(vis[j] == 0 && dis[j] < minn)
			{
				minn = dis[j];
				u = j;
			}
		}
		vis[u] = 1;
		for(int k = 0 ; k < V[u].size() ; k++)
		{
			int v = V[u][k].to;
			int w = V[u][k].cost;
			if(dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
			}
		}
	}
}
int main()
{
	while(~scanf("%d %d %d" , &n , &m , &s))
	{
		int u , v , cost , w , w1;
		memset(vis , 0 , sizeof(vis));
		for(int i = 0 ; i < maxn ; i++)V[i].clear();
		while(m--)
		{
			scanf("%d %d %d" , &u , &v , &cost);
			V[u].push_back(node(v , cost));
		//	V[v].push_back(node(u , cost));
		}
		scanf("%d" , &w);
		while(w--)
		{
			scanf("%d" , &w1);
		//	V[w1].push_back(node(0 , 0));
			V[0].push_back(node(w1 , 0));
		}
		vis[0] = 1;
		memset(dis , INF , sizeof(dis));
		for(int i = 0 ; i < V[0].size() ; i++)
		{
			dis[V[0][i].to]  = V[0][i].cost;
		}
		dis[0] = 0;
		Dijkstra();
		if(dis[s] < INF)
		{
			printf("%d\n" , dis[s]);
		}
		else
		{
			printf("-1\n");
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值