结合的好啊。。
1 #include <cstdlib> 2 #include <iostream> 3 #include <queue> 4 #include<vector> 5 6 using namespace std; 7 struct edge 8 { 9 int from,to,cost; 10 }; 11 const int MAXN=1003; 12 const int MAX=2147483647; 13 bool in_queue[MAXN];// 14 bool visited[MAXN];// 15 int dist[MAXN];// 16 //int sum=0;// 17 int dp[MAXN]; 18 vector<edge>v[MAXN];// 19 void spfa(int p) 20 { 21 int i=0; 22 queue<int>q; 23 while(!q.empty()) 24 { 25 q.pop(); 26 } 27 for(i=0;i<=MAXN-1;i++) 28 { 29 dist[i]=MAX; 30 } 31 dist[p]=0; 32 q.push(p); 33 in_queue[p]=1; 34 while(!q.empty()) 35 { 36 int i=0; 37 int cur=q.front(); 38 q.pop(); 39 in_queue[cur]=0; 40 for(i=0;i<=int(v[cur].size())-1;i++) 41 { 42 int cost=v[cur][i].cost+dist[cur]; 43 int to=v[cur][i].to; 44 if(cost<dist[to]) 45 { 46 dist[to]=cost; 47 if(!in_queue[to]) 48 { 49 q.push(to); 50 in_queue[to]=1; 51 } 52 } 53 } 54 } 55 } 56 int dfs(int p) 57 { 58 int i=0; 59 int sum=0; 60 if(dp[p]!=-1) 61 { 62 return dp[p]; 63 } 64 if(p==2) 65 { 66 return dp[p]=1; 67 } 68 for(i=0;i<=int(v[p].size())-1;i++) 69 { 70 int to=v[p][i].to; 71 if((!visited[to])&&(dist[to]<dist[p])) 72 { 73 visited[to]=1; 74 sum=sum+dfs(to); 75 visited[to]=0; 76 } 77 } 78 return dp[p]=sum; 79 } 80 int main(int argc, char *argv[]) 81 { 82 int n,m; 83 while((cin>>n)&&(n!=0)&&(cin>>m)) 84 { 85 int dist2home[MAXN]; 86 int i=0; 87 for(i=0;i<=MAXN-1;i++) 88 { 89 v[i].clear(); 90 } 91 // sum=0; 92 memset(visited,0,sizeof(visited)); 93 memset(in_queue,0,sizeof(in_queue)); 94 memset(dp,-1,sizeof(dp)); 95 while(m--) 96 { 97 int from,to,cost; 98 cin>>from>>to>>cost; 99 edge e={from,to,cost}; 100 v[e.from].push_back(e); 101 swap(e.from,e.to); 102 v[e.from].push_back(e); 103 } 104 spfa(2); 105 106 cout<<dfs(1)<<endl; 107 } 108 //system("PAUSE"); 109 return EXIT_SUCCESS; 110 }