这是我在一以前写的SPFA上加了一个记忆话搜索救过了,直接上代码吧……
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <memory.h>
#define maxn 1005
#define INF 1<<29
using namespace std;
int max(int a,int b){return a>b?a:b;}
int M,N,X;
struct farm
{
int next,weight;
}tmp,tmp2;
vector<farm>Grap[maxn];
bool flag[maxn];
int dist[maxn];
bool cango[maxn][maxn];
int map[maxn][maxn];
int dp[maxn];
void SPFA(int v0)
{
memset(flag,0,sizeof flag);
dist[v0]=0;
queue<int>Q;
Q.push(v0);
flag[v0]=1;
while(!Q.empty())
{
int temp=Q.front();
Q.pop();
flag[temp]=0;
int END=Grap[temp].size();
for(int i=0;i<END;i++)
{
tmp=Grap[temp][i];
if(dist[temp]<INF&&dist[temp]+tmp.weight<dist[tmp.next])
{
dist[tmp.next]=dist[temp]+tmp.weight;
if(!flag[tmp.next])
{
Q.push(tmp.next);
flag[tmp.next]=1;
}
}
}
}
}
int dfs(int cur)
{
if(dp[cur]!=-1)return dp[cur];
int temp=0;
for(int i=1;i<=N;i++)
if(cango[cur][i])temp+=dfs(i);
return dp[cur]=temp;
}
int main()
{
// freopen("in.txt","r",stdin);
int u,v,w;
while(~scanf("%d",&N)&&N)
{
scanf("%d",&M);
memset(dist,0x3f,sizeof dist);
memset(map,0x3f,sizeof map);
memset(cango,0,sizeof cango);
memset(dp,-1,sizeof dp);
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
tmp.next=v,tmp.weight=w;
tmp2.next=u,tmp2.weight=w;
Grap[v].push_back(tmp2);
Grap[u].push_back(tmp);
map[u][v]=map[v][u]=w;
}
SPFA(2);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(dist[i]>dist[j]&&map[i][j]<1061109567)cango[i][j]=1;
dp[2]=1;
dfs(1);
printf("%d\n",dp[1]);
}
return 0;
}