poj 3160 Father Christmas flymouse(强连通缩点+最长路)

题目地址:http://poj.org/problem?id=3160

思路:将所有点权值为负数的点设为0,,同一强连通分量中的点可全部选择,因此将其看做一点。在新图中求最长路径即可。最长路径:由于为给定起点,(1)从所有入度为0的点开始,进行DFS;(2)设置一虚拟节点,将其与入度为0的点相连,SPFA求最长路径。

SPFA版

#include
#include
#include
#include
#include
#include
using  namespace std;
const int  maxn=30000+50;
const int maxm=150000+50;
const int INF=0x3f3f3f3f;
struct Node
{
    int x,y;
};
queue q;
Node e[maxm];
int all,n,m,top,cnt;
int s[100000],d[maxn];
int vw[maxn],v[maxn];
int vis[maxn],sc[maxn];
int dfn[maxn],low[maxn];
int scvw[maxn],dist[maxn];
vector g[maxn],scg[maxn];
void init()
{
    top=all=cnt=0;
    memset(s,0,sizeof(s));
    memset(d,0,sizeof(d));
    memset(sc,0,sizeof(sc));
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(scvw,0,sizeof(scvw));
    for(int i=1; i<=n; i++) g[i].clear(),scg[i].clear();
}
void Tarjan(int u)
{
    all++,dfn[u]=low[u]=all;
    top++,s[top]=u,vis[u]=1;
    for(int i=0; i0?vw[i]:0);
        for(int i=0; i
DFS版

#include
#include
#include
#include
#include
using  namespace std;
const int  maxn=30000+50;
const int maxm=150000+50;
const int INF=0x3f3f3f3f;
struct Node
{
    int x,y;
};
int ans;
Node e[maxm];
int all,n,m,top,cnt;
int s[100000],d[maxn];
int vw[maxn],v[maxn];
int vis[maxn],sc[maxn];
int dfn[maxn],low[maxn];
int scvw[maxn],dist[maxn];
vector g[maxn],scg[maxn];
void init()
{
    ans=-INF;
    top=all=cnt=0;
    memset(s,0,sizeof(s));
    memset(d,0,sizeof(d));
    memset(sc,0,sizeof(sc));
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(scvw,0,sizeof(scvw));
    for(int i=1; i<=n; i++) g[i].clear(),scg[i].clear();
}
void Tarjan(int u)
{
    all++,dfn[u]=low[u]=all;
    top++,s[top]=u,vis[u]=1;
    for(int i=0; i0?vw[i]:0);
            ans=max(ans,scvw[sc[i]]);
        }
        for(int i=0; i



你可能感兴趣的:(OJ_POJ,图论_强连通分量,图论_最短路,ACM)