​点击打开链接​

因为图中每一个点被访问过后权值就消失了 所以不能用最短路直接求 先缩点 然后得一DAG图 求最长路即可

if语句后边多了一个分号 调了一晚上 真的舒服

#include <cstdio>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 0x3f3f3f3f

struct node1
{
int v;
int next;
};

struct node2
{
int v;
int w;
int next;
};

stack <int> stk;
queue <int> que;
node1 edge1[20010];
node2 edge2[20010];
int first1[2010],first2[2010],per[2010],dfn[2010],low[2010],book[2010],belong[2010],val[2010];
int pre[2010],dis[2010];
int n,m,num,tot;
char mp[50][50];

void addedgeI(int u,int v)
{
edge1[num].v=v;
edge1[num].next=first1[u];
first1[u]=num++;
return;
}

void addedgeII(int u,int v,int w)
{
edge2[num].v=v;
edge2[num].w=w;
edge2[num].next=first2[u];
first2[u]=num++;
return;
}

void build()
{
int next[2][2]={0,1,1,0};
int i,j,k,tx,ty,cnt;
memset(first1,-1,sizeof(first1));
num=0,cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
for(k=0;k<2;k++)
{
tx=i+next[k][0];
ty=j+next[k][1];
if(tx<0||tx>n-1||ty<0||ty>m-1||mp[tx][ty]=='#') continue;
addedgeI(i*m+j+1,tx*m+ty+1);
}
if('0'<=mp[i][j]&&mp[i][j]<='9')
{
per[i*m+j+1]=mp[i][j]-'0';
}
else if(mp[i][j]=='#')
{
per[i*m+j+1]=-1;
}
else
{
per[i*m+j+1]=0;
cnt++;
if(pre[cnt]!=-1) addedgeI(i*m+j+1,pre[cnt]);
}
}
}
return;
}

void dfs(int cur)
{
int i,v;
stk.push(cur);
num++;
dfn[cur]=num,low[cur]=num,book[cur]=1;
for(i=first1[cur];i!=-1;i=edge1[i].next)
{
v=edge1[i].v;
if(!dfn[v])
{
dfs(v);
low[cur]=min(low[cur],low[v]);
}
else if(book[v])
{
low[cur]=min(low[cur],dfn[v]);
}
}
if(dfn[cur]==low[cur])
{
tot++;
while(!stk.empty())
{
v=stk.top();
stk.pop();
book[v]=0;
belong[v]=tot;
val[tot]+=per[v];
if(v==cur) break;
}
}
return;
}

void tarjan()
{
int i,u,v;
while(!stk.empty()) stk.pop();
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(book,0,sizeof(book));
memset(belong,0,sizeof(belong));
memset(val,0,sizeof(val));
num=0,tot=0;
for(i=1;i<=n*m;i++)
{
if(!dfn[i]&&per[i]!=-1) dfs(i);
}
memset(first2,-1,sizeof(first2));
num=0;
for(u=1;u<=n*m;u++)
{
for(i=first1[u];i!=-1;i=edge1[i].next)
{
v=edge1[i].v;
if(belong[u]!=belong[v])
{
addedgeII(belong[u],belong[v],val[belong[v]]);
}
}
}
return;
}

int spfa()
{
int i,u,v,w,maxx;
while(!que.empty()) que.pop();
for(i=1;i<=tot;i++)
{
dis[i]=-N;
book[i]=0;
}
que.push(belong[1]);
dis[belong[1]]=val[belong[1]];
book[belong[1]]=1;

while(!que.empty())
{
u=que.front();
que.pop();
book[u]=0;
for(i=first2[u];i!=-1;i=edge2[i].next)
{
v=edge2[i].v,w=edge2[i].w;
if(dis[v]<dis[u]+w)
{
dis[v]=dis[u]+w;
if(!book[v])
{
que.push(v);
book[v]=1;
}
}
}
}
maxx=0;
for(i=1;i<=tot;i++)
{
maxx=max(maxx,dis[i]);
}
return maxx;
}

int main()
{
int t,i,j,cnt,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",mp[i]);
}
cnt=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mp[i][j]=='*') cnt++;
}
}
for(i=1;i<=cnt;i++)
{
scanf("%d%d",&x,&y);
if(mp[x][y]!='#') pre[i]=x*m+y+1;
else pre[i]=-1;
}
build();
tarjan();
printf("%d\n",spfa());
}
return 0;
}