油箱POJ3635(tle)

油箱POJ3635(tle)

思路

题干在这:POJ3635
优先队列BFS
但不知道为什么就超时了,可能是有细节没处理好,改天再回来改

tle代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAX 1010
using namespace std;
int n, m;
int price[MAX];
int head[MAX];
int nexte[MAX];
int endp[20010];
int len[20010];
int ptot, etot;
void createdge(int u,int v,int d){
	endp[++etot] = v;
	nexte[etot] = head[u];
	head[u] = etot;
	len[etot] = d;
	endp[++etot] = u;
	nexte[etot] = head[v];
	head[v] = etot;
	len[etot] = d;
}
int c, s, e;
int cost[MAX][110];
int vis[MAX][110];
struct cas {
	int pos;
	int cos;
	int fuel;
	friend bool operator <(const cas& a,const cas& b) {
		return a.cos > b.cos;
	}
};
priority_queue<cas> q;
void bfs() {
	while (q.size())
		q.pop();
	memset(cost, 0, sizeof(cost));
	memset(vis, 0, sizeof(vis));
	vis[s][0] = 1;
	cost[s][0] = 0;
	cas x = { s,0,0 };
	q.push(x);
	while (q.size()) {
		cas work = q.top();
		q.pop();
		if (work.pos == e) {
			printf("%d\n", work.cos);
			return;
		}
		if (work.fuel < c&&!vis[work.pos][work.fuel+1]) {
			cas n;
			n.pos = work.pos;
			n.cos = work.cos + price[work.pos];
			n.fuel = work.fuel + 1;
			vis[n.pos][n.fuel] = 1;
			q.push(n);
		}
		for (int i = head[work.pos]; i != -1; i = nexte[i]) {
			if (work.fuel >= len[i] && !vis[endp[i]][work.fuel - len[i]]) {
				cas n;
				n.pos = endp[i];
				n.cos = work.cos;
				n.fuel = work.fuel - len[i];
				vis[n.pos][n.fuel] = 1;
				q.push(n);
			}
		}
	}
	printf("impossible\n");
	return;
}
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		scanf_s("%d", &price[i]);
	memset(head, -1, sizeof(head));
	for (int i = 1; i <= m; i++) {
		int u, v, d;
		scanf_s("%d%d%d", &u, &v, &d);
		createdge(u, v, d);
	}
	int q;
	cin >> q;
	while (q--) {
		scanf_s("%d%d%d", &c, &s, &e);
		bfs();
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值