kuangbin_ShortPath O (LightOJ 1074)

这是什么鬼OJ啊都没见过害的我还交错语言CE了一发摔

想着懒得重写了直接把上一题的dij改了改就交了 然后RE

反应过来这题有负环 想着怎么标记负环同时不直接结束spfa

看了别人的代码感叹了一下我还是太弱 多学习吧 =.=

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;

int busy[210], val[210][210];
int t, n, m, q, dist[210];
bool cir[210];

void spfa(int s)
{
    int time[210];
    bool vis[210];
    queue<int> q;
    memset(vis, 0, sizeof vis);
    memset(time, 0, sizeof time);
    memset(dist, 0x3f, sizeof dist);
    
    dist[s] = 0;
    vis[s] = true;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        if(cir[u]) continue;
        for(int i = 1; i <= n; i++){
            if(dist[i] > dist[u] + val[u][i]){
                dist[i] = dist[u] + val[u][i];
                if(!vis[i]){
                    q.push(i);
                    if(time[i]++ > n) cir[i] = true;
                }
            }
        }
    }
}

int main()
{
    scanf("%d", &t);
    for(int i = 1; i <= t; i++){
        memset(val, 0x3f, sizeof val);
        memset(cir, 0, sizeof cir);
        
        scanf("%d", &n);
        for(int j = 1; j <= n; j++){
            scanf("%d", &busy[j]);
        }
        
        scanf("%d", &m);
        for(int j = 1; j <= m; j++){
            int s, d;
            scanf("%d%d", &s, &d);
            val[s][d] = (int)pow(busy[d] - busy[s], 3);
        }
        
        spfa(1);
        scanf("%d", &q);
        printf("Case %d:
", i);
        while(q--){
            int d;
            scanf("%d", &d);
            if(cir[d] || dist[d] < 3 || dist[d] == INF) puts("?");
            else printf("%d
", dist[d]);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/quasar/p/5128929.html