Domino Effect ZOJ 1298 POJ 1135 Dijkstra

这题WA很久,然后找不出原因,后来看了书上的程序(图论理论算法实现及应用神马的),然后感觉不出来自己哪里错了,后来改了改,把无穷大的定义改小了点,再放到ZOJ上,成功AC了,我都不知道为什么,也许还改了其他地方,我已经糊涂了,然后又把它放POJ 1135上又WA了,我都憔悴了,最后我想起以前刷水题时要把double用%f输出,我试了一下,果然AC了,求解释`````

贴代码:

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAXN 505
 4 #define INF 1000000
 5 int n;
 6 int edge[MAXN][MAXN];
 7 bool s[MAXN];
 8 int time[MAXN];
 9 void Dijkstra()
10 {
11     int i,j;
12     for(i=1; i<=n; i++)
13         time[i] = edge[1][i];
14     memset(s,false,sizeof(s));
15     s[1] = true;
16     for(i=1; i<n; i++)
17     {
18         int min = 1000000;
19         int v = 1 ;
20         for(j = 1; j<=n; j++)
21         {
22             if(!s[j] &&time[j] < min)
23             {
24                 min = time[j];
25                 v = j;
26             }
27         }
28         s[v] = true;//表明v算过了
29         for(j=1; j<=n ; j++)
30         {
31             if(!s[j] && edge[v][j] < INF && time[v] + edge[v][j] < time[j])//该点没被算过,v到j有边存在,距离更短
32                 time[j] = time[v] + edge[v][j] ;
33         }
34     }
35     double max1 = -1;
36     int po;
37     time[1] = 0;
38     for(i=1; i<=n; i++)
39     {
40         if(time[i] > max1)
41         {
42             max1 = time[i];
43             po = i;
44         }
45     }
46     double max2 = -1;
47     int pox,poy;
48     for(i=1; i<=n; i++)
49     {
50         for(j=1; j<=n; j++)
51         {
52             int t;
53             if(edge[i][j] == INF) continue;
54             double temp = (time[i] + time[j] + edge[i][j])/2.0 ;
55             if(temp > max2 )
56             {
57                 max2 = temp;
58                 pox = i;
59                 poy = j;
60             }
61         }
62     }
63     if(max2 > max1)
64     {
65         printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n\n",max2,pox,poy);
66     }
67     else
68     {
69         printf("The last domino falls after %.1f seconds, at key domino %d.\n\n",max1,po);
70     }
71 }
72 int main()
73 {
74 //    freopen("in.cpp","r",stdin);
75     int m;
76     int ser  = 0;
77     while(~scanf("%d%d",&n,&m))
78     {
79         if(n==0 && m == 0) break;
80         int i,j;
81         for(i=1; i<=n; i++)
82             for(j=1; j<=n; j++)
83                 edge[i][j] = INF;
84         for(i=0; i<m; i++)
85         {
86             int u,v,w;
87             scanf("%d%d%d",&u,&v,&w);
88             edge[u][v] = edge[v][u] = w;
89         }
90         printf("System #%d\n",++ser);
91         Dijkstra();
92     }
93     return 0;
94 }

 

posted on 2013-03-31 23:04  allh123  阅读(173)  评论(0编辑  收藏  举报

导航