标签:des style blog class code c
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 5306 Accepted
Submission(s): 1939
开始题意理解错了,以为求最短路径的数目。其实求的是在点1到点2的路径中,经过的路段Vij要求d[i]>d[j](d[i]为点i到到点2的最短路),求满足要求的路径数。
最短路径:
先求出点2到其他店的最短路,然后记忆化搜索得解。
1 //31MS 820K 1533B G++ 2 #include<iostream> 3 #include<vector> 4 #include<queue> 5 #define N 1005 6 #define inf 0x7fffffff 7 using namespace std; 8 struct node{ 9 int v,d; 10 node(int a,int b){ 11 v=a;d=b; 12 } 13 }; 14 vector<node>V[N]; 15 int vis[N],d[N]; 16 int v; 17 int n,m; 18 int ans[N]; 19 void dij(int s) 20 { 21 memset(vis,0,sizeof(vis)); 22 for(int i=0;i<=n;i++) 23 d[i]=inf; 24 d[s]=0; 25 queue<int>Q; 26 Q.push(s); 27 vis[s]=1; 28 while(!Q.empty()){ 29 int u=Q.front(); 30 Q.pop(); 31 vis[u]=0; 32 int m=V[u].size(); 33 for(int i=0;i<m;i++){ 34 int v=V[u][i].v; 35 int w=V[u][i].d; 36 if(d[v]>d[u]+w){ 37 d[v]=d[u]+w; 38 if(!vis[v]){ 39 vis[v]=1; 40 Q.push(v); 41 } 42 } 43 } 44 } 45 } 46 int dfs(int u) 47 { 48 if(u==2) return 1; 49 if(ans[u]!=0) return ans[u]; 50 int m=V[u].size(); 51 int cnt=0; 52 for(int i=0;i<m;i++){ 53 int v=V[u][i].v; 54 int w=V[u][i].d; 55 if(d[v]<d[u]) 56 cnt+=dfs(v); 57 } 58 return ans[u]=cnt; 59 } 60 int main(void) 61 { 62 int a,b,c; 63 while(scanf("%d",&n),n) 64 { 65 scanf("%d",&m); 66 memset(ans,0,sizeof(ans)); 67 for(int i=0;i<=n;i++) V[i].clear(); 68 for(int i=0;i<m;i++){ 69 scanf("%d%d%d",&a,&b,&c); 70 V[a].push_back(node(b,c)); 71 V[b].push_back(node(a,c)); 72 } 73 dij(2); 74 printf("%d\n",dfs(1)); 75 } 76 return 0; 77 }
hdu 1142 A Walk Through the Forest (最短路径),布布扣,bubuko.com
hdu 1142 A Walk Through the Forest (最短路径)
标签:des style blog class code c
原文地址:http://www.cnblogs.com/GO-NO-1/p/3731985.html