SPFA算法模板
?簡單的一個模板吧,留個底。如果要判負環的話,需要加一個cnt數組記錄入隊次數就可以了。
void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]=0;vis[st]=1;while(!q.empty()) {int cur = q.front();q.pop();vis[cur]=0;int up = vv[cur].size();for(int i = 0; i<up; i++) {Edge now = vv[cur][i];int to = now.to;if(dis[to] > dis[cur] + now.w) {dis[to] = dis[cur]+now.w;if(vis[to] == 0) vis[to]=1,q.push(to);}}} }單源最短路條數:
void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]=0;vis[st]=1;while(!q.empty()) {int cur = q.front();q.pop();vis[cur]=0;int up = vv[cur].size();for(int i = 0; i<up; i++) {Edge now = vv[cur][i];int to = now.to;if(dis[to] > dis[cur] + now.w) {dis[to] = dis[cur]+now.w;dp[to] = dp[cur];if(vis[to] == 0) vis[to]=1,q.push(to);}else if(dis[to] == dis[cur] + now.w) {dp[to] += dp[cur];}}} }?
?
總結
- 上一篇: 银行悄然耍花样!房贷还了7年,本金竟一分
- 下一篇: 【CodeForces - 574B】B