TJU 1325/PKU 2607 Fire Station

Time Limit: 3.0 Seconds   Memory Limit: 65536K
Total Runs: 285   Accepted Runs: 80    Multiple test files

    本题应该说是不难,但我却花费了一个上午。本题的思想就是最短路。值得注意的是有很多人都弄错了题意,本题的题意就是找到一个合适的点来安置Fire Station使得所有的intersections到离他们最近的Fire Station最长的那一个最短(原题是: minimize the maximum distance from any intersection to the nearest fire station.)

    所有本题的思想就是用已给出的有Fire Station进行最短路求解,进行f 次之后算出每一个intersections到最近的Fire Station的距离。然后再从小到大一个一个的尝试,求出能最小化最大距离的那一个。

    注意:由于本题可能有500个intersection所以如果用二维数组存储,有可能会超时,建议使用邻接表。

   代码:

  此代码在北大,和天津上均可过,下面还有一种简单的代码在北大上可过,但在天津上超时。

  
    
1 #include < stdio.h >
2 #include < stdlib.h >
3 #include < string .h >
4 #include < queue >
5   using namespace std;
6 typedef struct t
7 {
8 int data;
9 int s;
10 struct t * next;
11 }T;
12   struct node
13 {
14 int data;
15 T * next;
16 }g[ 505 ];
17 typedef struct qnode
18 {
19 int s,data;
20 bool operator < ( const qnode & a) const
21 {
22 return a.s < s;
23 }
24 }NODE;
25   int Index,zmin,n,Min[ 505 ];
26   void dijkstra( int v0, int t)
27 {
28 priority_queue < NODE > qu;
29 NODE cur,next;
30 int i,max,dist[ 505 ],visit[ 505 ],mark = 0 ;
31 cur.data = v0;cur.s = 0 ;
32 qu.push (cur);
33 T * p;
34 memset(dist, - 1 , sizeof (dist));
35 memset(visit, 0 , sizeof (visit));
36 while ( ! qu.empty ())
37 {
38 cur = qu.top ();
39 while (visit[cur.data])
40 {
41 qu.pop();
42 if (qu.empty ())
43 {
44 mark = 1 ;
45 break ;
46 }
47 cur = qu.top();
48 }
49 if (mark)
50 break ;
51 qu.pop ();
52 visit[cur.data] = 1 ;
53 p = g[cur.data].next;
54 while (p)
55 {
56 next.data = p -> data;
57 if ( ! visit[next.data] && (dist[next.data] ==- 1 || dist[next.data] > p -> s + cur.s))
58 dist[next.data] = next.s = p -> s + cur.s;
59 else
60 next.s = dist[next.data];
61 if ( ! visit[p -> data])
62 qu.push(next);
63 p = p -> next;
64 }
65 }
66 if (t == 0 )
67 {
68 for (i = 1 ;i <= n;i ++ )
69 {
70 if (Min[i] > dist[i])
71 Min[i] = dist[i];
72 }
73 }
74 else if (t == 1 )
75 {
76 dist[v0] = 0 ;
77 for (i = 1 ;i <= n;i ++ )
78 {
79 if (dist[i] > Min[i])
80 dist[i] = Min[i];
81 if (dist[i] > max)
82 max = dist[i];
83 }
84 if (max < zmin)
85 {
86 Index = v0;
87 zmin = max;
88 }
89 }
90 }
91 int main()
92 {
93 int f,i,x,y,s,mark[ 505 ] = { 0 },a[ 105 ] = { 0 };
94 scanf( " %d%d " , & f, & n);
95 for (i = 1 ;i <= f;i ++ )
96 scanf( " %d " , & a[i]);
97 for (i = 1 ;i <= n;i ++ )
98 {
99 g[i].next = NULL;
100 Min[i] = 0xfffffff ;
101 }
102 T * p, * q;
103 zmin = 0 ;
104 while (scanf( " %d%d%d " , & x, & y, & s) != EOF)
105 {
106 p = (T * )malloc( sizeof (T));
107 q = (T * )malloc( sizeof (T));
108 p -> data = y;
109 p -> s = s;
110 p -> next = g[x].next;
111 g[x].next = p;
112 g[x].data = x;
113 q -> data = x;
114 q -> s = s;
115 q -> next = g[y].next;
116 g[y].next = q;
117 g[y].data = y;
118 }
119 for (i = 1 ;i <= f;i ++ )
120 {
121 if (mark[a[i]])
122 continue ;
123 mark[a[i]] = 1 ;
124 dijkstra(a[i], 0 );
125
126 }
127 for (i = 1 ;i <= n;i ++ )
128 {
129 if (Min[i] > zmin)
130 zmin = Min[i];
131 }
132 Index = 1 ;
133 for (i = 1 ;i <= n;i ++ )
134 {
135 if (mark[i])
136 continue ;
137 dijkstra(i, 1 );
138 }
139 printf( " %d\n " ,Index);
140 return 0 ;
141 }

 

代码:

 

代码
   
     
1 #include < stdio.h >
2 #include < string .h >
3 #include < stdlib.h >
4 #define MAX 0xfffffff
5 int Min[ 505 ],n;
6 int rmin,Index;
7 typedef struct t
8 {
9 int data;
10 int s;
11 struct t * next;
12 }T;
13 struct node
14 {
15 int data;
16 T * next;
17 }g[ 505 ];
18 void dijkstra( int v0, int t)
19 {
20 int distance[ 505 ],s[ 505 ],mindis,i,j,u,max = 0 ;
21 memset(distance, - 1 , sizeof (distance));
22 memset(s, 0 , sizeof (s));
23 T * p;
24 p = g[v0].next;
25 while (p)
26 {
27 distance[p -> data] = p -> s;
28 p = p -> next;
29 }
30 s[v0] = 1 ;
31 for (i = 1 ;i < n;i ++ )
32 {
33 mindis = MAX;
34 for (j = 1 ;j <= n;j ++ )
35 {
36 if ( ! s[j] && distance[j] !=- 1 && distance[j] < mindis)
37 {
38 u = j;
39 mindis = distance[j];
40 }
41 }
42 s[u] = 1 ;
43 p = g[u].next;
44 while (p)
45 {
46 if ( ! s[p -> data] && (p -> s + distance[u] < distance[p -> data] || distance[p -> data] ==- 1 ))
47 distance[p -> data] = p -> s + distance[u];
48 p = p -> next;
49 }
50 }
51 if (t == 0 )
52 {
53 for (i = 1 ;i <= n;i ++ )
54 {
55 if (distance[i] < Min[i])
56 Min[i] = distance[i];
57 }
58 }
59 else if (t == 1 )
60 {
61 distance[v0] = 0 ;
62 for (i = 1 ;i <= n;i ++ )
63 {
64 if (distance[i] > Min[i])
65 distance[i] = Min[i];
66 if (distance[i] > max)
67 max = distance[i];
68 }
69 if (max < rmin)
70 {
71 Index = v0;
72 rmin = max;
73 }
74
75 }
76 }
77 int main()
78 {
79 int a[ 105 ] = { 0 },i,b,c,s,f,mark[ 505 ];
80 memset(mark, 0 , sizeof (mark));
81 scanf( " %d%d " , & f, & n);
82 for (i = 1 ;i <= f;i ++ )
83 {
84 scanf( " %d " , & a[i]);
85 }
86 for (i = 1 ;i <= n;i ++ )
87 {
88 g[i].next = NULL;
89 Min[i] = 0xfffffff ;
90 }
91 rmin = 0 ;
92 T * p, * q;
93 while (scanf( " %d%d%d " , & b, & c, & s) != EOF)
94 {
95 p = (T * )malloc( sizeof (T));
96 q = (T * )malloc( sizeof (T));
97 p -> data = c;
98 p -> s = s;
99 p -> next = g[b].next;
100 g[b].next = p;
101 g[b].data = b;
102 q -> data = b;
103 q -> s = s;
104 q -> next = g[c].next;
105 g[c].next = q;
106 g[c].data = c;
107 }
108 for (i = 1 ;i <= f;i ++ )
109 {
110 if (mark[a[i]] == 0 )
111 {
112 mark[a[i]] = 1 ;
113 dijkstra(a[i], 0 );
114 }
115 }
116 for (i = 1 ;i <= n;i ++ )
117 {
118 if (Min[i] > rmin)
119 rmin = Min[i];
120 }
121 Index = 1 ;
122 for (i = 1 ;i <= n;i ++ )
123 {
124 if (mark[i])
125 continue ;
126 dijkstra(i, 1 );
127 }
128 printf( " %d\n " ,Index);
129 return 0 ;
130 }

 

 

你可能感兴趣的:(pku)