题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6290
思路
昨天个人赛里的,题目,普通Dijstra直接内存炸了…
以前没写过,堆优化的Dijstra,所以学习了一下大佬的写法!!
博客: https://www.cnblogs.com/luoxiaoyi/p/9726829.html
题解思路我就直接引用这位大佬的了!!
题解讲的很明白,接下来就是操作了!!
代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long long INF = 1e16;
typedef long long ll;
typedef struct node
{
int to;
int a;
int b;
node(int _to, int _a, int _b) : to(_to), a(_a), b(_b) {}
}Edge;
struct lzy { // 实在不晓得取啥名,来个lzy
int from;
ll level;
lzy(int _from = 0, ll _level = 0) : from(_from), level(_level) {}
bool operator < (const lzy &a) const {
return level > a.level;
}
};
vector<vector<Edge> > E;
ll dist[MAXN];
bool vis[MAXN];
void Dijstra(int start, int n) {
fill(vis, vis + n + 5, false);
fill(dist, dist + n + 5, INF);
dist[start] = 1;
priority_queue<lzy> q;
while(!q.empty()) q.pop();
q.push(lzy{start, 1});
while(!q.empty()){
lzy tmp = q.top(); // 取出中转点
q.pop();
int u = tmp.from;
if(vis[u]) continue;
vis[u] = true;
for(auto i : E[u]) {
int v = i.to;
int a = i.a;
int b = i.b;
if(log2((dist[u] + a * 1.0) / dist[u]) < b) continue;
if(!vis[v] && dist[v] > dist[u] + a) { // 如果中转点已经现过或者不满足条件
dist[v] = dist[u] + a; // 取最小的level
q.push(lzy{v, dist[v]});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--){
int n, m;
cin >> n >> m;
E.clear();
E.resize(n + 5);
for(int i = 1; i <= m; i++){
int a, b, c, d;
cin >> a >> b >> c >> d;
E[a].push_back(Edge{b, c, d});
}
Dijstra(1, n);
if(dist[n] == INF) // 如果无法到达
cout << -1 << endl;
else
cout << (int)(log2(dist[n])) << endl;
}
return 0;
}