标签:style blog http java color os
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
解题:最短距离+记忆化搜索,找出1到终点2上的所有点,假设A,B两点,如果统计d[A] > D[B]这种路径的条数。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #define LL long long 13 #define INF 0x3f3f3f 14 using namespace std; 15 const int maxn = 1010; 16 int mp[maxn][maxn],d[maxn],p[maxn]; 17 int n,m; 18 bool vis[maxn]; 19 void dij(int src){ 20 int i,j,temp,index; 21 for(i = 1; i <= n; i++) 22 d[i] = INF; 23 d[src] = 0; 24 memset(vis,false,sizeof(vis)); 25 for(i = 0; i < n; i++){ 26 temp = INF; 27 for(j = 1; j <= n; j++) 28 if(!vis[j] && d[j] < temp) temp = d[index = j]; 29 vis[index] = true; 30 for(j = 1; j <= n; j++) 31 if(!vis[j] && d[j] > d[index]+mp[index][j]) 32 d[j] = d[index] + mp[index][j]; 33 } 34 } 35 int dfs(int s){ 36 if(p[s]) return p[s]; 37 if(s == 2) return 1; 38 int i,sum = 0; 39 for(i = 1; i <= n; i++){ 40 if(mp[s][i] < INF && d[s] > d[i]) sum += dfs(i); 41 } 42 p[s]+= sum; 43 return p[s]; 44 } 45 int main(){ 46 int i,j,u,v,w; 47 while(scanf("%d",&n),n){ 48 scanf("%d",&m); 49 for(i = 1; i <= n; i++) 50 for(j = 1; j <= n; j++) 51 mp[i][j] = INF; 52 for(i = 0; i < m; i++){ 53 scanf("%d%d%d",&u,&v,&w); 54 mp[u][v] = mp[v][u] = w; 55 } 56 dij(2); 57 memset(p,0,sizeof(p)); 58 printf("%d\n",dfs(1)); 59 } 60 return 0; 61 }
图论trainning-part-1 B. A Walk Through the Forest,布布扣,bubuko.com
图论trainning-part-1 B. A Walk Through the Forest
标签:style blog http java color os
原文地址:http://www.cnblogs.com/crackpotisback/p/3859710.html