1 题目编号:1010
2 题目内容:
5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0
2 4
3 解题思路形成过程:从终点求一次最短路,然后记忆话搜索求路径条数
4 代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1010;
const int INF=1000000000;
int n,m,num;
int dis[maxn],head[maxn],d[maxn];
bool vis[maxn];
struct node
{
int v,next,f;
}edge[maxn*100];
void init()
{
num=0;
memset(head,-1,sizeof(head));
memset(d,-1,sizeof(d));
}
void add_edge(int x,int y,int f)
{
edge[num].v=y;
edge[num].next=head[x];
edge[num].f=f;
head[x]=num++;
}
void SPFA(int s)
{
for(int i=0;i<=n;i++)
{
dis[i]=INF;
vis[i]=0;
}
queue<int> q;
q.push(s);
vis[s]=1;
dis[s]=0;
while(!q.empty())
{
int u=q.front();q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v,w=edge[i].f;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(!vis[v])
{
q.push(v);
vis[v]=1;
}
}
}
}
}
int DP(int u)
{
int & ans=d[u];
if(ans!=-1)return d[u];
ans=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]<dis[u])
ans+=DP(v);
}
return ans;
}
int main()
{
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF,n)
{
init();
scanf("%d",&m);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
add_edge(b,a,c);
}
SPFA(2);
d[2]=1;
DP(1);
printf("%d\n",d[1]);
}
return 0;
}