选择最佳路径

有一天,琪琪想乘坐公交车去拜访她的一位朋友。

由于琪琪非常容易晕车,所以她想尽快到达朋友家。

现在给定你一张城市交通路线图,上面包含城市的公交站台以及公交线路的具体分布。

已知城市中共包含 n 个车站(编号1~n)以及 m 条公交线路。

每条公交线路都是 单向的,从一个车站出发直接到达另一个车站,两个车站之间可能存在多条公交线路。

琪琪的朋友住在 s 号车站附近。

琪琪可以在任何车站选择换乘其它公共汽车。

请找出琪琪到达她的朋友家(附近的公交车站)需要花费的最少时间。

输入格式
输入包含多组测试数据。

每组测试数据第一行包含三个整数 n,m,s,分别表示车站数量,公交线路数量以及朋友家附近车站的编号。

接下来 m 行,每行包含三个整数 p,q,t,表示存在一条线路从车站 p 到达车站 q,用时为 t。

接下来一行,包含一个整数 w,表示琪琪家附近共有 w 个车站,她可以在这 w 个车站中选择一个车站作为始发站。

再一行,包含 w 个整数,表示琪琪家附近的 w 个车站的编号。

输出格式
每个测试数据输出一个整数作为结果,表示所需花费的最少时间。

如果无法达到朋友家的车站,则输出 -1。

每个结果占一行。
n≤1000,m≤20000 ,
1≤s≤n,
0<w<n,
0<t≤1000

思路:
解法一:反向建图,跑一遍最短路即可。缺点:无法满足多源点,多汇点情况。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxn=1e3+10;
int n,m,s,w;

unordered_map<int,bool> vis;
bool st[maxn];

void bfs(vector<vector<PII>>& g ){
	memset(st,0,sizeof(st));
	priority_queue<PII,vector<PII>,greater<PII> > p;
	p.push(PII(0,s));
	//st[s]=1;
	int ans=1e9;
	while(p.size()){
		PII temp=p.top();
		p.pop();
		if(st[temp.second]) continue;
		st[temp.second]=1;
		if(vis.find(temp.second)!=vis.end()){
			ans=min(ans,temp.first);
		}
		int u=temp.second;
		for(int i=0;i<g[u].size();i++){
			if(!st[g[u][i].first]){
				p.push(PII(temp.first+g[u][i].second,g[u][i].first));
				//st[g[u][i].first]=1;
			}
		}
	}
	
	if(ans==1e9) cout<<-1<<endl;
	else cout<<ans<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin>>n>>m>>s){
		vector<vector<PII> > g(maxn);
		vis.clear();
		int u,v,val;
		for(int i=1;i<=m;i++){
			cin>>u>>v>>val;
			g[v].push_back(PII(u,val));	
		}
		cin>>w;
		for(int i=1;i<=w;i++){
			int temp;
			cin>>temp;
			vis[temp]=1;
		}
		bfs(g);
		
	}
}

解法二:建立一个超级源点,其到每个起点距离为0,求该超级源点到终点的距离即可。
当多源点多汇点情况时候,可以建立两个超级源点,求这两个超级源点之间的最短距离即可。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxn=1e3+10;
int n,m,s,w;
int dist[maxn];

Dij(vector<vector<PII>>& g){
	memset(dist,0x3f,sizeof(dist));
	priority_queue<PII,vector<PII>,greater<PII>> p;
	p.push(PII(0,0));
	while(!p.empty()){
		PII temp=p.top();
		p.pop();
		if(temp.first>dist[temp.second]) continue;
		int u=temp.second;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i].first;
			int val=g[u][i].second;
			if(dist[v]>dist[u]+val){
				dist[v]=dist[u]+val;
				p.push(PII(dist[v],v));
			}
		}
	}
	if(dist[s]==0x3f3f3f3f) cout<<-1<<endl;
	else cout<<dist[s]<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	while(cin>>n>>m>>s){
		vector<vector<PII> > g(maxn);
		int u,v,val;
		for(int i=1;i<=m;i++){
			cin>>u>>v>>val;
			g[u].push_back(PII(v,val));	
		}
		cin>>w;
		for(int i=1;i<=w;i++){
			int temp;
			cin>>temp;
			g[0].push_back(PII(temp,0));
		}
		Dij(g);
		
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值