http://poj.org/problem?id=3592提交了30多次了 受不了了 两份的代码基本上一样了 一个AC一个WA 木办法 贴份别人的吧 改得跟我得一样 人家能A  我是WA。。

强连通分量缩点 用spfa算出最长路 注意算强连通时加过的值就不再加了

poj3592Instantaneous Transference(tarjan+spfa)_#includepoj3592Instantaneous Transference(tarjan+spfa)_强连通_02
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <vector>
  5 #include <queue>
  6 #include<stack>
  7 using namespace std;
  8 const int NN=1606;
  9 const int MM=1000000;
 10 const int INF=0x3fffffff;
 11 
 12 int n,m,w[NN],val[NN];
 13 char c[42][42];
 14 vector<int> adj[NN];
 15 
 16 struct Edge{
 17     int u,v,next;
 18 }edge[MM];
 19 int t,head[NN];
 20 void addedge(int u,int v)
 21 {
 22     edge[t].u=u;
 23     edge[t].v=v;
 24     edge[t].next=head[u];
 25     head[u]=t++;
 26 }
 27 
 28 int bcnt,top,depth,dfn[NN],low[NN],belong[NN],vis[NN],instack[NN];
 29 //bool instack[NN];
 30 stack<int>s;
 31 void init()
 32 {
 33     t=0;
 34     memset(head,-1,sizeof(head));
 35 }
 36 void tarjan(int u)
 37 {
 38     dfn[u]=low[u]=++depth;
 39     s.push(u);
 40     instack[u] = 1;
 41     int v;
 42     for (int i=head[u]; i!=-1; i=edge[i].next)
 43     {
 44         v=edge[i].v;
 45         if (!dfn[v])
 46         {
 47             tarjan(v);
 48             low[u]=min(low[u],low[v]);
 49         }
 50         else if (instack[v])
 51             low[u]=min(low[u],dfn[v]);
 52     }
 53     if (low[u]==dfn[u])
 54     {bcnt++;
 55         val[bcnt] = 0;
 56         adj[bcnt].clear();
 57         for(;;)
 58         {
 59             v=s.top();
 60             s.pop();
 61             belong[v]=bcnt;
 62             val[bcnt]+=w[v];instack[v] = 0;
 63             if(v==u)
 64             break;
 65         }
 66 
 67     }
 68 }
 69 int dis[NN];
 70 int lpfa(int S)
 71 {
 72     for (int i=1; i<=bcnt; i++) dis[i]=-INF;
 73     memset(vis,0,sizeof(vis));
 74     dis[S]=0;
 75     queue<int> q;
 76     q.push(S);
 77     while (!q.empty())
 78     {
 79         int u=q.front();
 80         vis[u]=0;
 81         q.pop();
 82         for (int i=0; i<adj[u].size(); i++)
 83         {
 84             int v=adj[u][i];
 85             if (dis[v]<dis[u]+val[v])
 86             {
 87                 dis[v]=dis[u]+val[v];
 88                 if (!vis[v])
 89                 {
 90                     vis[v]=1;
 91                     q.push(v);
 92                 }
 93             }
 94         }
 95     }
 96     int ret=0;
 97     for (int i=1; i<=bcnt; i++) if (dis[i]>ret) ret=dis[i];
 98     return ret+val[S];
 99 }
100 void find(int n)
101 {
102    memset(dfn,0,sizeof(dfn));
103    memset(instack,0,sizeof(instack));
104    memset(val,0,sizeof(val));
105    memset(low,0,sizeof(low));
106    bcnt=depth=0;
107    for(int i = 0 ; i < n ; i++)
108    if (!dfn[i]) tarjan(i);
109 }
110 int main()
111 {
112     int cas;
113     scanf("%d",&cas);
114     while (cas--)
115     {
116         scanf("%d%d",&n,&m);
117         for (int i=0; i<n; i++)
118         {
119             getchar();
120             for (int j=0; j<m; j++) c[i][j]=getchar();
121         }
122         init();
123         int x,y;
124         for (int i=0; i<n; i++)
125           for (int j=0; j<m; j++)
126           {
127               int u= i*m+j;
128               if (c[i][j]=='*')
129               {
130                   w[u]=0;
131                   scanf("%d%d",&x,&y);
132                   addedge(u,x*m+y);
133               }
134               else w[u]=c[i][j]-'0';
135               if (c[i][j]=='#') { w[u]=0; continue; }
136               if (i!=n-1 && c[i+1][j]!='#') addedge(u,u+m);
137               if (j!=m-1 && c[i][j+1]!='#') addedge(u,u+1);
138           }
139         find(n*m);
140         int u,v;
141         for (int i=0; i<t; i++)
142         {
143             u=edge[i].u; v=edge[i].v;
144             if (belong[u]!=belong[v]) adj[belong[u]].push_back(belong[v]);
145         }
146         printf("%d\n",lpfa(belong[0]));
147     }
148     return 0;
149 }
View Code