hdu1142

题意看错 晕呐

 1 /***hdu1142
 2 dijskra+dfs
 3 ***/
 4 #include<stdio.h>
 5 #include<string.h>
 6 #define maxint 99999999
 7 int map[1003][1003],n,m,flag;
 8 int mark[1003],dis[1003];
 9 int dijskra(int s)
10 {
11     int i,j,pos;
12     int vis[1000];
13     memset(vis,0,sizeof(vis));
14     for(i=1;i<=n;i++)
15     {
16         dis[i]=map[s][i];
17     }
18     dis[s]=0;
19     vis[s]=1;
20     pos=s;
21     for(i=1;i<n;i++)
22     {
23         pos=s;
24         int minn=maxint;
25         for(j=1;j<=n;j++)
26         {
27             if(!vis[j]&&minn>dis[j])
28             {
29                 minn=dis[j];
30                 pos=j;
31             }
32         }
33         vis[pos]=1;
34         for(j=1;j<=n;j++)
35         {
36             if(!vis[j]&&(dis[j]>map[pos][j]+dis[pos]))
37             {
38                 dis[j]=map[pos][j]+dis[pos];
39             }
40         }
41     }
42     return dis[1];
43 }
44 int dfs(int x)
45 {
46     if(x==2)
47     {
48         return 1;
49     }
50     if(mark[x])  return mark[x];
51     for(int i=1;i<=n;i++)
52     {
53
54         if(i!=x&&dis[i]<dis[x]&&map[x][i]!=maxint)
55         {
56             mark[x]+=dfs(i);//mark[]存储当前几个点
57         }
58     }
59     return mark[x];
60 }
61 int main()
62 {
63     int i,j;
64     while(scanf("%d%d",&n,&m)!=EOF)
65     {
66         if(!n)
67             break;
68         for(j=0;j<=n;j++)
69             for(i=0;i<=n;i++)
70             {
71                 if(i==j)map[i][j]=0;
72                 else
73                 {
74                     map[j][i]=maxint;
75                     map[i][j]=maxint;
76                 }
77             }
78         for(i=0;i<m;i++)
79         {
80             int x,y,z;
81             scanf("%d%d%d",&x,&y,&z);
82             map[x][y]=z;map[y][x]=z;
83         }
84         memset(mark,0,sizeof(mark));
85         dijskra(2);//因为题意:若B到终点的距离小于A到终点的距离,那么就走B,所以要倒着找出他们的路径
86         int ans=dfs(1);
87         printf("%d\n",ans);
88     }
89 }
时间: 2024-10-07 00:51:38

hdu1142的相关文章

HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5688    Accepted Submission(s): 2089 Problem Description Jimmy experiences a lot of stress at work these days, especial

*HDU1142 最短路+记忆化dfs

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7995    Accepted Submission(s): 2943 Problem Description Jimmy experiences a lot of stress at work these days, especiall

hdu1142(最短路+记忆化搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目意思挺模糊 大致思路,以终点为源点,做一次单源最短路 深搜一遍图(下一步到达的位置  比现在位置  离终点更近) 记录每个节点上可行数 1 #include <bits/stdc++.h> 2 3 struct Edge 4 { 5 int v, w; 6 Edge(int _v, int _w): v(_v), w(_w){} 7 }; 8 9 const int MAXN = 1000 + 1

HDU1142 A Walk Through the Forest(dijkstra)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7733    Accepted Submission(s): 2851 Problem Description Jimmy experiences a lot of stress at work these days, especiall

HDU1142 A Walk Through the Forest

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6935    Accepted Submission(s): 2548 Problem Description Jimmy experiences a lot of stress at work these days, especiall

hdu1142(spfa+DFS记忆化)

这道题因为英语渣半天没看懂题意,后来在网上找了一些题解发现,好多人题意理解错了,但是代码交上去能过. 最后终于找到了一个能说明白的题解. 链接:http://www.cnblogs.com/ruihua852/archive/2012/08/27/2658910.html 下面是粘贴的链接中的题意: 题目意思是说一个人要从上班的地方回到家里,途中会经过一些地方,按下面规则问他回家会有几种路线: 题目已经定义好1为上班地方2为家,每个地点之间的距离都已经知道,哪么如果从A到B的一条路,可以走的条件