HDU 2112 - HDU Today (优先队列)

问题

HDU 2112 HDU Today - https://acm.hdu.edu.cn/showproblem.php?pid=2112

分析

  • 题目描述是“从s到e的时间”,但实际代码是“s与e”间,因此是无向图

代码

  • 方法一:邻接矩阵 + 优先队列
#include<bits/stdc++.h>
using namespace std;
#define MXLOC 160
#define MXLEN 35
#define pii pair<int, int>
#define inf 0x7f7f7f7f
int N, s, t, vis[MXLOC], dis[MXLOC], g[MXLOC][MXLOC];
char name[MXLEN];
map<string, int> mp;
struct cmp{
	bool operator()(pii x, pii y){
		return x.second > y.second;
	}
};
priority_queue<pii, vector<pii>, cmp> q;
int read(){
	scanf("%s", name);
	if(!mp.count(name)) mp[name] = mp.size()+1;
	return mp[name];
}
void ext(int start){
	vis[start] = 1;
	for(int i = 1; i < MXLOC; ++i){
		if(vis[i] || g[start][i] == inf) continue;
		q.push({i, dis[start]+g[start][i]});
	}
}
int solve(){
	pii tmp;
	q.push({s, 0});
	while(!q.empty()){
		tmp = q.top();
		q.pop();
		if(vis[tmp.first]) continue;
		dis[tmp.first] = tmp.second;
		if(tmp.first == t) return dis[t];
		ext(tmp.first);
	}
	return -1;
}
int main(){
	int a, b, c, ans;
	while(scanf("%d", &N), ~N){
		memset(vis, 0, sizeof vis);
		memset(dis, 0, sizeof dis);
		memset(g, inf, sizeof g);
		while(!q.empty()) q.pop();
		mp.clear();
		s = read();
		t = read();
		while(N--){
			a = read(), b = read();
			scanf("%d", &c);
			if(c < g[a][b]) g[a][b] = g[b][a] = c;
		}
		ans = solve();
		printf("%d\n", ans);
	}	
    return 0;
}
  • 方法二:链式前向星+优先队列
#include<bits/stdc++.h>
using namespace std;
#define MXR 20010 // 2倍边数
#define MXLOC 160
#define MXLEN 35
#define pii pair<int, int>
#define inf 0x7f7f7f7f
int N, s, t, vis[MXLOC], dis[MXLOC];
int head[MXLOC], cnt;
struct Edge{
	int to, tim, nxt;
}edge[MXR];
char name[MXLEN];
map<string, int> mp;
struct cmp{
	bool operator()(pii x, pii y){
		return x.second > y.second;
	}
};
priority_queue<pii, vector<pii>, cmp> q;
int read(){
	scanf("%s", name);
	if(mp.count(name) == 0) mp[name] = mp.size()+1;
	return mp[name];
}
void ext(int start){
	vis[start] = 1;
	for(int i = head[start]; ~i; i = edge[i].nxt){
		int to = edge[i].to;
		if(vis[to]) continue;
		q.push({to, dis[start]+edge[i].tim});
	}
}
void add(int u, int v, int w){
	edge[cnt].to = v;
	edge[cnt].tim = w;
	edge[cnt].nxt = head[u];
	head[u] = cnt++;
}
int solve(){
	pii tmp;
	q.push({s, 0});
	while(!q.empty()){
		tmp = q.top();
		q.pop();
		if(vis[tmp.first]) continue;
		dis[tmp.first] = tmp.second;
		if(tmp.first == t) return dis[t];
		ext(tmp.first);
	}
	return -1;
}
int main(){
	int a, b, c, ans;
	while(scanf("%d", &N), ~N){
		memset(vis, 0, sizeof vis);
		memset(dis, 0, sizeof dis);
		memset(head, -1, sizeof head), cnt = 1;
		while(!q.empty()) q.pop();
		mp.clear();
		s = read(), t = read();		
		while(N--){
			a = read(), b = read();
			scanf("%d", &c);
			add(a, b, c);
			add(b, a, c);
		}
		ans = solve();
		printf("%d\n", ans);
	}	
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jpphy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值