Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 02-23-2004, 10:27 PM   #1 (permalink)
Upright
 
[C/C++] Theory Question

Hi, I'm writing a programming competition tomorrow (yikes!), and was just wondering about a common type of problem that always seems to show up. I can *kind* of get my head around it, but not fully. This is not homework, and I'm allowed to bring in any materials I choose to the contest.

Basically, the problem looks like this:

Quote:
You are a salesperson selling trucks which can carry trucks which can carry trucks. Needless to say, your trucks are heavy. Also needless to say, you have to drive one of these trucks across a wide wet domain, and since it is wet, you need to drive over some bridges. In fact, on every road between two cities, there is a bridge but there is not a direct road between every pair of cities. Each bridge can support a certain maximum weight. This maximum weight is an integer from 0 to 100000.

You have been given a list of cities where there are customers who are eager to view one of your trucks. These cities are called destination cities. Since you must decide which truck you will drive through these cities, you will have to answer the following problem: what is the maximum weight that can be driven through these destination cities? You are to write a program to solve this problem.

The first line of input will contain three positive integers: c, r and d specifying the number of cities (in total), number of roads between cities and number of destination cities, respectively. The cities are numbered from 1 to c. There are at most 1000 cities and at most 10000 roads. The next r lines contain triples x y w indicating that this road runs between city x and city y and it has a maximum weight capacity of w. The next d lines give the destination cities you must visit with your truck. There will be at least one destination city.

You can assume that you are starting in city 1 and that city 1 is not a destination city. You can visit the d destination cities in any order, but you must visit all d destination cities. The output from your program is a single integer, the largest weight that can be driven through all d destination cities.
I understand what it's asking for, I'm just not quite sure on the correct method to do it. I don't need code, but a general push in the right direction will be very useful.

I'll post what I've figured out so far (it might be completely wrong)

Set up some sort of linked list representing the cities. Each node in the linked list will contain (in addition to the *next pointer), an array of pointers to other nodes. These will be matched with an array of integers, representing the greatest amount of weight going from one city to the next.

I know that can represent the data, but it doesn't seem like a very efficient way to do so (we are also marked on efficiency). However, I am unaware of any structures that could fit this type of problem better (or maybe I'm just not seeing it correctly). I also can't figure out how to properly traverse that linked list so as to find the greatest weight to be driven thru all cities.

Here is a sample input/output:
Quote:
Sample Input
5 7 3
1 2 20
1 3 50
1 4 70
1 5 90
2 3 30
3 4 40
4 5 60
2
4
5

Output for Sample Input 30
mgcloud is offline  
Old 02-25-2004, 01:09 AM   #2 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
I don't really understand the question. In particular, I'm confused by the combination of "trucks which can carry trucks which can carry trucks. Needless to say, your trucks are heavy" and "you must decide which truck you will drive through these cities." These seem to contradict each other and makes it seem like one detail has nothing to do with the problem.

So, can you put the problem into other words?

I think the problem may be asking for the path that traverses all the destination cities with the highest minimum of bridge capacities. Is this correct? If so, the problem is not so hard...
KnifeMissile is offline  
Old 02-25-2004, 09:56 PM   #3 (permalink)
Upright
 
Quote:
Originally posted by KnifeMissle

I think the problem may be asking for the path that traverses all the destination cities with the highest minimum of bridge capacities. Is this correct? If so, the problem is not so hard...
Yep, that's correct. =)
mgcloud is offline  
Old 03-02-2004, 10:35 AM   #4 (permalink)
Insane
 
Location: Belgium
u should read up about some graph algorithms ...


maybe dijkstra can help you
__________________
Let's GO
Cuball is offline  
Old 03-02-2004, 12:24 PM   #5 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
Yep, it's a graph search, It's also a subset of the traveling salesman problem...

Representation is important, I would consider and array of linked lists
Each element in the array represents all the roads leading out of that city.

This also falls under the domain of AI searching. You are not interested in the path cost per se, you are interested in picking a path that maximizes a certain value, specifically the maximum weight that can be driven over the roads traveled to reach that city.

The link list only represents path costs, you also need to create a graph class to deal with actually traveling down different routes.

I suggest you look up IDA*, it's ideal for this situation. H(N) in this case is the maximum weight value along this path. (normalized so that the fringe is sorted correctly)

It's deliciously neat so you could code this in a reasonable amount of time. Your allowed reference materials correct? You should definitely bring Artificial Intelligence A modern Approach - Second edition Stuart Russel and Peter Norvig.

This problem isn't solvable with plain BFS because the search space is way too big and you would have to search all of it to be sure you have the best possible solution.

Last time I went to a programming contest my group had a literal library with us for reference it helped us IMMENSELY.
__________________
"It better be funny"
kel is offline  
Old 03-03-2004, 08:43 AM   #6 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
Errr... I think I'm wrong, this problem sounds simpler then I initially thought.
__________________
"It better be funny"

Last edited by kel; 03-03-2004 at 08:48 AM..
kel is offline  
Old 03-04-2004, 11:59 PM   #7 (permalink)
Upright
 
Cool, thanks! I'll definitely be checking a lot of these algorithms out, as my knowledge of algorithms in computer science pretty much extends to the scope of ap comp sci ab (basic searches (linear, binary)/sorts (heapsort, quicksort, merge sort, etc)).
mgcloud is offline  
Old 03-13-2004, 07:05 PM   #8 (permalink)
Junkie
 
This sounds to me like a maximum cost spanning tree (look up minimum cost spanning tree). This can easily be solved with a greedy algorithm.
Rekna is offline  
Old 03-13-2004, 07:31 PM   #9 (permalink)
Wehret Den Anfängen!
 
Location: Ontario, Canada
*nod*.

Build up your graph (city nodes with bridge edges between them). Almost any implimentation would work.

Then, you want to "paint" the graph with the max weight of a truck that can reach each city. Flow out greedily from city zero.

How to flow: keep track of which cities you already know how much weight can reach. Keep track of all bridges out of the known territory: store max (bridge capacity, capacity of the city it comes from), and keep this list sorted by this effective capacity.

Select the highest effective capacity bridge. Bring the city it goes to into the fold, and add the bridges out of that city to your sorted bridge list.

Repeat until bridge list empty.

You can short cut it once you have every city that you want to visit in your known capacity set.

Now, find the min capacity city you have to visit.

Will take O((n lg(n)) time at worst, n = cities + bridges.

Other than the fact both are graph problems, this really isn't the travelling salesman problem.

Oh yes, and all of the above gave good, if less explicit, advice.

General programming contest advice:
Learn "dynamic programming".

Figure out this cute problem: "How can we fit the most amount of cars on a two-lane ferry? Does always putting the next car on the side with the most remaining room solve the problem? Can we exploit the fact that the sum of accumulated car lengths on each lane of the ferry is always an integer?" (from http://www.cs.sunysb.edu/~skiena/392/lectures/week11/ )

Learn a hell of alot of graph algorithms.

Learn some numeric computation: I got burned not knowing shit about this once.
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest.
Yakk is offline  
Old 03-13-2004, 07:41 PM   #10 (permalink)
Junkie
 
Here is some more good advice for a programming contest, some friends of mine got burned on this part

Do not try to do equality on floats. Instead declare a variable epsilon that is very small .0001 or less. Then when you want to see if two floats are equal see if they are within +- epsilon of eachother.
Rekna is offline  
 

Tags
c or c, question, theory


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:37 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360