P14418 无脑图论建模跑最短路
这题做了我们学校某场比赛 T2 ,赛时被邪恶的埃弛·艾斯贼教练做局,评测机波动了惊人的 325ms 刚刚好给我卡 TLE 了,比暴力分还低,我无疑是悲愤的。
无脑的图论建模做法,用 \((u,t)\) 代表 \(t\) 时刻在公交站 \(u\) 上,作为图上的一个节点。
考虑建边,对于 \(m\) 辆公交车,我们将 \((A_i,X_i)\) 与 \((B_i,Y_i)\) 反向连边,并在 \(A_i\) 和 \(B_i\) 上分别记录 \(X_i\) 和 \(Y_i\) 的时间信息,最后在每个公交站上把记录的时间排个序,然后将时间小的节点和时间大的节点反向连边即可,会建 \(O(m)\) 条边。
容易发现每个询问 \(L_i\) 的答案就是 \((n,L_i)\) 可以走到的 \((1,x)\) 点中 \(x\) 的最大值,可以用 bfs 或者记忆化搜索预处理一下,询问的时候二分出 \(n\) 公交站上记录过的小于等于 \(L_i\) 的最大时刻 \(p\),输出 \((n,p)\) 的答案即可。
实现上因为点对 \((u,t)\) 不连续,需要用 map 映射成节点,所以总复杂度是 \(O(m\log m+q\log m)\) 的。
赛时写法如下,自认为码风优良:
#include<bits/stdc++.h>
using namespace std;
int n,m,q;
vector<int> e[1000005];
vector<int> t[100005];
__umap_hashtable<int,int> mp[100005];
int tot;
int mx[1000005];
pair<int,int> ori[1000005];
bool vis[1000005];
int getid(int u,int t)
{
if(!mp[u][t]) {mp[u][t] = ++tot,ori[tot] = {u,t};return tot;}
return mp[u][t];
}
main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int a,b,c,d;
cin >> a >> b >> c >> d;
t[a].emplace_back(c);
t[b].emplace_back(d);
int x = getid(a,c),y = getid(b,d);
e[y].emplace_back(x);
}
for(int i = 1; i <= n; i++)
{
sort(t[i].begin(),t[i].end());
for(int j = 1; j < t[i].size(); j++)
{
int x = getid(i,t[i][j - 1]),y = getid(i,t[i][j]);
e[y].emplace_back(x);
}
}
memset(mx,-1,sizeof mx);
for(int x : t[n])
{
queue<int> q;
x = getid(n,x);
q.push(x);
vis[x] = 1;
while(q.size())
{
int u = q.front();
q.pop();
if(ori[u].first == 1) mx[x] = max(mx[x],ori[u].second);
for(int v : e[u])
if(!vis[v])
vis[v] = 1,q.push(v);
}
}
for(int i = 1; i < t[n].size(); i++)
{
int x = getid(n,t[n][i - 1]),y = getid(n,t[n][i]);
mx[y] = max(mx[y],mx[x]);
}
cin >> q;
while(q--)
{
int x;
cin >> x;
int ps = upper_bound(t[n].begin(),t[n].end(),x) - t[n].begin() - 1;
if(ps < 0) cout << -1 << '\n';
else
{
ps = t[n][ps];
ps = getid(n,ps);
cout << mx[ps] << '\n';
}
}
}
这里再附上记忆化搜索的写法:
#include<bits/stdc++.h>
using namespace std;
int n,m,q;
vector<int> e[1000005];
vector<int> t[100005];
__umap_hashtable<int,int> mp[100005];
int tot;
int mx[1000005];
pair<int,int> ori[1000005];
bool vis[1000005];
void dfs(int u)
{
if(vis[u]) return;
vis[u] = 1;
if(ori[u].first == 1) mx[u] = ori[u].second;
for(int v : e[u])
{
dfs(v);
mx[u] = max(mx[u],mx[v]);
}
}
int getid(int u,int t)
{
if(!mp[u][t]) {mp[u][t] = ++tot,ori[tot] = {u,t};return tot;}
return mp[u][t];
}
main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int a,b,c,d;
cin >> a >> b >> c >> d;
t[a].emplace_back(c),t[b].emplace_back(d);
e[getid(b,d)].emplace_back(getid(a,c));
}
for(int i = 1; i <= n; i++)
{
sort(t[i].begin(),t[i].end());
for(int j = 1; j < t[i].size(); j++) e[getid(i,t[i][j])].emplace_back(getid(i,t[i][j - 1]));
}
memset(mx,-1,sizeof mx);
dfs(getid(n,t[n].back()));
cin >> q;
while(q--)
{
int x;
cin >> x;
int ps = upper_bound(t[n].begin(),t[n].end(),x) - t[n].begin() - 1;
if(ps < 0) cout << -1 << '\n';
else cout << mx[getid(n,t[n][ps])] << '\n';
}
}

浙公网安备 33010602011771号