• 题目大意
    求有向图中经过某一点k的最大环(数据规模不支持floyd)。

  • 题解
    以k为起点在正向图中spfa求单源最短路。再在反向图中spfa求单源最短路。

    枚举除k外的每个点i。假设有一个同一时候包括i与k的环ans=max{ans,dist[i]+invdist[i]}

  • Code

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1010, maxm = 200010, nil = 0, oo = 1061109567;
int n, m, k;
int pnt[maxn], nxt[maxm], u[maxm], v[maxm], w[maxm], e;
int d[maxn], invd[maxn];
bool vis[maxn], other[maxm];
void addedge(int x, int y, int z)
{
    u[++e] = x; v[e] = y; w[e] = z;
    nxt[e] = pnt[x]; pnt[x] = e; other[e] = false;
    u[++e] = y; v[e] = x; w[e] = z;
    nxt[e] = pnt[y]; pnt[y] = e; other[e] = true;
}
void init()
{
    int x, y, z;
    scanf("%d%d%d", &n, &m, &k);
    for(int i = 1; i <= m; ++i)
    {
        scanf("%d%d%d", &x, &y, &z);
        addedge(x, y, z);
    }
}
void work()
{
    queue <int> q;
    //下面为反向spfa
    memset(invd, 0x3f, sizeof(invd));
    memset(vis, 0, sizeof(vis));
    invd[k] = 0; vis[k] = true;
    q.push(k);
    while(!q.empty())
    {
        int tmp = q.front();
        q.pop();
        vis[tmp] = false;
        for(int j = pnt[tmp]; j != nil; j = nxt[j])
        {
            if(other[j] && invd[v[j]] > invd[tmp] + w[j])
            {
                invd[v[j]] = invd[tmp] + w[j];
                vis[v[j]] = true;
                q.push(v[j]);
            }
        }
    }
    //下面为正向spfa
    memset(d, 0x3f, sizeof(d));
    memset(vis, 0, sizeof(vis));
    d[k] = 0; vis[k] = true;
    q.push(k);
    while(!q.empty())
    {
        int tmp = q.front();
        q.pop();
        vis[tmp] = false;
        for(int j = pnt[tmp]; j != nil; j = nxt[j])
        {
            if((!other[j]) && d[v[j]] > d[tmp] + w[j])
            {
                d[v[j]] = d[tmp] + w[j];
                vis[v[j]] = true;
                q.push(v[j]);
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i)
    {
        if(d[i] != oo && invd[i] != oo)
        {
            ans = max(ans, d[i] + invd[i]);
        }
    }
    printf("%d\n", ans);
}
int main()
{
    init();
    work();
    return 0;
}