Skip to content

2000 2099/2045. Second Minimum Time to Reach Destination #4393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions solution/0700-0799/0770.Basic Calculator IV/solutions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;
struct Poly{
map<vector<string>, long> d;
Poly(long v=0){ if(v) d[{}]=v; }
Poly(const string &s){ d[{s}]=1; }
};
Poly add(const Poly &a,const Poly &b){ Poly r=a; for(auto &p:b.d) r.d[p.first]+=p.second; for(auto it=r.d.begin();it!=r.d.end();){ if(it->second==0) r.d.erase(it++); else ++it;} return r; }
Poly sub(const Poly &a,const Poly &b){ Poly r=a; for(auto &p:b.d) r.d[p.first]-=p.second; for(auto it=r.d.begin();it!=r.d.end();){ if(it->second==0) r.d.erase(it++); else ++it;} return r; }
Poly mul(const Poly &a,const Poly &b){ Poly r; for(auto &p:a.d) for(auto &q:b.d){ auto v=p.first; v.insert(v.end(),q.first.begin(),q.first.end()); sort(v.begin(),v.end()); r.d[v]+=p.second*q.second; } for(auto it=r.d.begin();it!=r.d.end();){ if(it->second==0) r.d.erase(it++); else ++it;} return r; }
class Solution {
public:
vector<string> basicCalculatorIV(string expr, vector<string>& evv, vector<int>& evi){
unordered_map<string,long> mp;
for(int i=0;i<evv.size();i++) mp[evv[i]] = evi[i];
vector<string> toks;
string t;
for(char c:expr){
if(c==' '){ if(!t.empty()){ toks.push_back(t); t.clear(); }}
else if(strchr("()+-*",c)){
if(!t.empty()){ toks.push_back(t); t.clear(); }
toks.push_back(string(1,c));
} else t.push_back(c);
}
if(!t.empty()) toks.push_back(t);
int i=0;
function<Poly()> parseE, parseT, parseP;
parseP = [&]{ string s=toks[i++]; if(s=="("){ Poly r = parseE(); i++; return r;} if(isdigit(s[0])) return Poly(stol(s)); return mp.count(s)? Poly(mp[s]) : Poly(s); };
parseT = [&]{ Poly r=parseP(); while(i<toks.size() && toks[i]=="*"){ i++; r = mul(r, parseP()); } return r; };
parseE = [&]{ Poly r=parseT(); while(i<toks.size()&&(toks[i]=="+"||toks[i]=="-")){ string op=toks[i++]; Poly p=parseT(); r = (op=="+"? add(r,p) : sub(r,p)); } return r; };
Poly res = parseE();
vector<pair<vector<string>,long>> v(res.d.begin(), res.d.end());
sort(v.begin(), v.end(), [](auto &a, auto &b){ if(a.first.size()!=b.first.size()) return a.first.size()>b.first.size(); return a.first<b.first; });
vector<string> ans;
for(auto &p:v) if(p.second){
string s = to_string(p.second);
for(auto &var:p.first) s += "*" + var;
ans.push_back(s);
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <array>
#include <climits>
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
vector<vector<int>> adj(n + 1);
for (auto& e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
vector<array<int, 2>> dist(n + 1, {INT_MAX, INT_MAX});
queue<pair<int, int>> q;
dist[1][0] = 0;
q.emplace(1, 0);

while (!q.empty()) {
auto [u, t] = q.front();
q.pop();
for (int v : adj[u]) {
int cycles = t / change;
int wait = (cycles % 2 == 1 ? change - (t % change) : 0);
int t2 = t + wait + time;
if (t2 < dist[v][0]) {
dist[v][1] = dist[v][0];
dist[v][0] = t2;
q.emplace(v, t2);
} else if (t2 > dist[v][0] && t2 < dist[v][1]) {
dist[v][1] = t2;
q.emplace(v, t2);
}
}
}

return dist[n][1];
}
};