QAQ
  • 板块灌水区
  • 楼主Buckbeak
  • 当前回复4
  • 已保存回复4
  • 发布时间2021/11/6 18:31
  • 上次更新2023/11/4 01:15:19
查看原帖
QAQ
365551
Buckbeak楼主2021/11/6 18:31
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>

typedef long long LL;

int readInt() {
  int ans = 0, c, f = 1;
  while (!isdigit(c = getchar()))
	if (c == '-') f = -1;
  do ans = ans * 10 + c - '0';
  while (isdigit(c = getchar()));
  return ans * f;
}

const int N = 1e4 + 3, M = 5e5 + 3;
const LL UN = (1 << 31) - 1;

int n, m, s, MAX;
int pre[N], nxt[M], to[M], val[M], cnt;

void addEdge(int x, int y, int w) {
  nxt[++cnt] = pre[x];
  pre[x] = cnt;
  to[cnt] = y;
  val[cnt] = w;
}

int dis[N];
bool vis[N];

inline void Dijkstra() {
  memset(dis, 0x3f3f3f3f, sizeof dis);
  MAX = dis[0];
  std::priority_queue< std::pair<int, int> > q;
  dis[s] = 0;
  q.push(std::make_pair(0, s));
  while (!q.empty()) {
  	std::pair <int, int> u = q.top();
  	q.pop();
  	if (vis[u.second]) continue;
  	vis[u.second] = 1;
  	for (int i = pre[u.second]; i; i = nxt[i])
	  if (!vis[to[i]] && dis[to[i]] > -u.first + val[i]) {
  		dis[to[i]] = -u.first + val[i];
  		q.push(std::make_pair( -dis[to[i]], to[i] ));
	}
  }
}

int main() {
  n = readInt(), m = readInt(), s = readInt();
  /////////////////////////////for (int i = 1; i <= m; ++i) addEdge(readInt(), readInt(), readInt());
  for (int i = 1, u, v, w; i <= m; ++i) {
  	u = readInt();
  	v = readInt();
  	w = readInt();
  	addEdge(u, v, w);
  }
  Dijkstra();
  for (int i = 1; i <= n; ++i)
    if (dis[i] == MAX) printf("%lld ", UN);
    else printf("%d ", dis[i]);
  return 0;
}

请看我注释掉的那行,如果用注释掉的那个循环代替下面的

for (int i = 1, u, v, w; i <= m; ++i) {
  	u = readInt();
  	v = readInt();
  	w = readInt();
  	addEdge(u, v, w);
  }

就会发生很神奇的错误(输出不同),为啥啊qwqqqq

2021/11/6 18:31
加载中...