# 华为实验室研究岗社招机试题

### 第一题

#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<queue>
using namespace std;

struct Edge {
int next, w;
};

const int maxn = 100+10;
int n, m, t, s, e;
int times[maxn], vals[maxn];
vector<Edge>edges[maxn];

Edge prenode[maxn], nxt[maxn];
void dijsktra(int n, int s, int e) {

vector<int> dist(n+1, INT_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
dist[s] = 0;
pq.push({0, s});
while(!pq.empty()) {
int u = pq.top().second, d = pq.top().first;
pq.pop();
if(dist[u] < d) continue;   // 剪枝，已经求得更小的了
if(u == e) break;
for(auto& node : edges[u]) {
int v = node.next, d = node.w;
if(dist[v] > dist[u] + d) {
dist[v] = dist[u] + d;
prenode[v] = {u, d};
pq.push({dist[v], v});
}
}
}
int cur = e;
while(cur != s) {
nxt[prenode[cur].next] = {cur, prenode[cur].w};
cur = prenode[cur].next;
}

while(cur != e) {
cout << "cur: " << cur << endl;
cur = nxt[cur].next;
}
cout << "cur: " << cur << endl;

return;
}

/*
4 4 22 0 3
1 1  1 1
5 7 9 12
0 1 10
0 2 10
1 3 10
2 3 10

21
*/

// 走到s点，还剩余时间t
int maxval = -1;
void dfs(int s, int t, int val, int pre) {
cout << "dfs: " <<  s << " " << t << " " << val << endl;
if(t < 0) return;
// 该点不玩
if(s == e) {
if(t >= times[s]) val += vals[s];
if(val > maxval) maxval = val;
return;
}

int v = nxt[s].next, w = nxt[s].w;
dfs(v, t-w, val, pre);

// 该点完
if(vals[s] < pre)  return;  // 大于前面的开心值才能玩
t -= times[s];
val += vals[s];
dfs(v, t-w, val, vals[s]);
}

int main() {
cin >> n >> m >> t >> s >> e;
for(int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
times[i] = tmp;
}
for(int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
vals[i] = tmp;
}
for(int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
Edge e1 = {v, w};
Edge e2 = {u, w};
edges[u].push_back(e1);
edges[v].push_back(e2);
}
dijsktra(n, s, e);
dfs(s, t, 0, -1);
cout << maxval << endl;
}

### 第二题

posted @ 2021-12-28 12:39  Rogn  阅读(111)  评论(0编辑  收藏  举报