说明:大多数网上说这个题目是stl跟bfs 这里纠正一下除了一开始建一个图之外 其余跟图没有半点关系,stl也只用了一个队列,手写完全没有问题,如果你看到的博客说是stl或者bfs的话,大概率他没实现 或者没写对 或者超时。 正解是写一个结构体存时间 对其排序 插入块或者查找之前对队列中之前的时间的任务都取出执行
原代码思想:用priority_queue对 执行时间进行排序 但是超时
参考代码:改成普通队列就可以过 但是根据分析t 不是严格递增的 这样bfs中的判断是有问题的 但是竟然可以过。感觉测试数据不严谨
#include
using namespace std;
const int N = 1e3+10, M = 2e5+10;
int h[N],ep, vis[N];
struct Edge{
int to,nxt;
}e[M];
int hh[N],vp;
struct node{
int val, len, nxt;
}v[30010];
int n, m, T;
struct Node{
int t, a, b;
bool operator<(const Node &b)const
{
if(t != b.t) return t > b.t;
}
};
queue q;
void add(int a,int b)
{
e[ep].to = b, e[ep].nxt = h[a]; h[a] = ep ++;
}
void addnode(int u,int ch)
{
v[++ vp].val = ch, v[vp].len = v[hh[u]].len + 1, v[vp].nxt = hh[u]; hh[u] = vp;
}
void print(int u)
{
vector ans;
for(int i = hh[u]; ~i; i = v[i].nxt) ans.push_back(v[i].val);
printf("%d", ans.size());
for(int i = ans.size() - 1; i >= 0; -- i) printf(" %d", ans[i]);
puts("");
}
void bfs(int cur)
{
while(q.size() && q.front().t <= cur)
{
auto nod = q.front(); q.pop();
int u = nod.a, ch = nod.b, t = nod.t;
if(v[u].len > v[hh[ch]].len || (v[u].len == v[hh[ch]].len && v[u].val < v[hh[ch]].val))
{
hh[ch] = u;
for(int i = h[ch]; ~i; i = e[i].nxt) q.push({t + T, hh[ch], e[i].to});
}
}
}
int main()
{
//freopen("1.txt","r",stdin);
memset(h, -1, sizeof h);
memset(hh, 0, sizeof hh);
v[vp].val = 0, v[vp].len = 1; v[vp].nxt = -1;
scanf("%d%d",&n,&m);
for(int i = 0; i < m; ++i)
{
int a, b; scanf("%d%d",&a, &b);
add(a, b); add(b, a);
}
int k, num = 0;
scanf("%d%d",&T,&k);
while(k --)
{
int u, cur, ch = 0; scanf("%d%d",&u, &cur);
char c = getchar();
bfs(cur);
if(c != '\n')
{
scanf("%d",&ch);
addnode(u, ch);
for(int i = h[u]; ~i; i = e[i].nxt) q.push({cur + T, hh[u], e[i].to});
}
else print(u);
}
}