在P4779中第一篇题解
Dijkstra优化的代码部分是这样的(本人写的)
struct node {
int u, dis;
bool operator < (const node &p) const {
return dis > p.dis;
}
};
priority_queue <node> q;
int dis[maxn];
int vis[maxn];
void dijkstra (int s) {
memset (dis, 0x3f3f3f3f, sizeof dis);
q.push((node){s, 0});
dis[s] = 0;
while (!q.empty()) {
node t = q.top();
q.pop();
if (vis[t.u]) continue;
vis[t.u] = 1;
for (int i = p[t.u]; ~i; i = e[i].next) {
int to = e[i].to;
if (dis[to] > dis[t.u] + e[i].w) {
dis[to] = dis[t.u] + e[i].w;
q.push((node){to, dis[to]});
}
}
}
return ;
}
(大概没写错吧)
但是中间有两行:
if (vis[t.u]) continue;
vis[t.u] = 1;
有的时候不加这两行才对,加了就是错的(虽然不加跑得很慢)
请问一下是什么原因呢?是因为负权边吗?