2013
11-28

# A Walk Through the Forest

Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.

Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.

For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define inf 0x3fffffff

int N,M;//N represents the number of points,and M stands for the number of edges;

int map[1024][1024];

int cnt;

int ans;

int dis[1024];

int visit[1024];

const int sta=1;

const int end=2;

struct Edge{
int v;
int val;
int next;
}e[1111111];

int idx;

int dijkstra()
{
memset(visit,0,sizeof(visit));
for(int i=1;i<=N;i++)
dis[i]=inf;
dis[sta]=0;
for(int j=1;j<=N;j++)
{
int t=inf;
int pos;
for(int i=1;i<=N;i++)
{
if(!visit[i]&&t>dis[i])
{
t=dis[i];
pos=i;
}
}
visit[pos]=1;
for(i=1;i<=N;i++)
{
if(!visit[i]&&dis[i]>dis[pos]+map[pos][i]&&map[pos][i]!=0x3f3f3f3f)
{
dis[i]=dis[pos]+map[pos][i];
}
}
}
if(dis[end]==inf)
return -1;
else
return dis[end];
}

{
e[idx].v=b;
e[idx].val=val;
}

void DFS(int index,int val)
{
//	printf("%d___%d___\n",index,val);
if(index==end)
{
if(val==ans)
{
cnt++;
return;
}
else
return;
}
if(val>=ans)
return;
else
{
{
int v=e[p].v;
if(!visit[v])
{
visit[v]=1;
DFS(v,val+e[p].val);
visit[v]=0;
}
}
}
}

int main()
{
int aa,bb,cc;
while(scanf("%d",&N)!=EOF,N)
{
memset(map,0x3f,sizeof(map));
scanf("%d",&M);
int t=inf;
idx=0;
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&aa,&bb,&cc);
if(map[aa][bb]>cc)//判断重边
{
map[aa][bb]=cc;
map[bb][aa]=cc;
}
}
ans=dijkstra();
//	printf("%d____\n",ans);
if(ans==-1)
printf("0\n");
else
{
cnt=0;
memset(visit,0,sizeof(visit));
visit[sta]=1;
DFS(sta,0);
printf("%d\n",cnt);
}
}
return 0;
}

#include<stdio.h>
#include<stdlib.h>
int m,n,map[1024][1024],des[1024],dis[1024],dep[1024];
int inf = 0x7fffffff;
int Dijkstra( int p )
{
dis[p] = 0;
for( int i = 1; i <= n; ++i )
{
int min = inf, pos = 0,t;
for( int j = 1; j <= n; ++j )
{
if( !des[j] )
if( dis[j] < min )
{
min = dis[j];
pos = j;
}
}
des[pos] = 1;
for( int j = 1; j <= n; ++j )
{
if( !des[j] )
if( map[pos][j] != inf )
if( ( t = map[pos][j] + dis[pos] ) < dis[j] )
dis[j] = t;
}
}
return 0;
}
int DFS( int x )
{

if( dep[x] )//记忆化搜索
return dep[x];
if( x == 2 )
return 1;
for( int i = 1; i <= n ; ++i )
{
if( map[x][i] != inf )
if( dis[i] < dis[x] )
dep[x] += DFS( i );
}
return dep[x];
}
int main( )
{
while( scanf( "%d",&n ),n )
{

scanf( "%d",&m );
for( int i = 0; i <= n; ++i )
{
for( int j = 0; j <= n; ++j )
map[i][j] = inf;
des[i] = 0;
dis[i] = inf;
dep[i] = 0;
}
for( int i = 1; i <= m; ++i )
{
int x,y,v;
scanf( "%d%d%d",&x,&y,&v );
map[x][y] = map[y][x] = v;
}
Dijkstra( 2 );
int res = DFS( 1 );
printf( "%d\n",res );
}
// system ("pause");
return 0;
}

1. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;